-
Notifications
You must be signed in to change notification settings - Fork 25k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
245 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...lasticsearch/src/main/java/org/elasticsearch/index/query/json/TermsJsonFilterBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Licensed to Elastic Search and Shay Banon under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. Elastic Search licenses this | ||
* file to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.index.query.json; | ||
|
||
import org.elasticsearch.util.json.JsonBuilder; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* @author kimchy (Shay Banon) | ||
*/ | ||
public class TermsJsonFilterBuilder extends BaseJsonFilterBuilder { | ||
|
||
private final String name; | ||
|
||
private final Object[] values; | ||
|
||
public TermsJsonFilterBuilder(String name, String... values) { | ||
this(name, (Object[]) values); | ||
} | ||
|
||
public TermsJsonFilterBuilder(String name, int... values) { | ||
this.name = name; | ||
this.values = new Integer[values.length]; | ||
for (int i = 0; i < values.length; i++) { | ||
this.values[i] = values[i]; | ||
} | ||
} | ||
|
||
public TermsJsonFilterBuilder(String name, long... values) { | ||
this.name = name; | ||
this.values = new Long[values.length]; | ||
for (int i = 0; i < values.length; i++) { | ||
this.values[i] = values[i]; | ||
} | ||
} | ||
|
||
public TermsJsonFilterBuilder(String name, float... values) { | ||
this.name = name; | ||
this.values = new Float[values.length]; | ||
for (int i = 0; i < values.length; i++) { | ||
this.values[i] = values[i]; | ||
} | ||
} | ||
|
||
public TermsJsonFilterBuilder(String name, double... values) { | ||
this.name = name; | ||
this.values = new Double[values.length]; | ||
for (int i = 0; i < values.length; i++) { | ||
this.values[i] = values[i]; | ||
} | ||
} | ||
|
||
private TermsJsonFilterBuilder(String name, Object... values) { | ||
this.name = name; | ||
this.values = values; | ||
} | ||
|
||
@Override public void doJson(JsonBuilder builder) throws IOException { | ||
builder.startObject(TermsJsonFilterParser.NAME); | ||
builder.startArray(name); | ||
for (Object value : values) { | ||
builder.value(value); | ||
} | ||
builder.endArray(); | ||
builder.endObject(); | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
...elasticsearch/src/main/java/org/elasticsearch/index/query/json/TermsJsonFilterParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Licensed to Elastic Search and Shay Banon under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. Elastic Search licenses this | ||
* file to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.index.query.json; | ||
|
||
import com.google.inject.Inject; | ||
import org.apache.lucene.index.Term; | ||
import org.apache.lucene.search.Filter; | ||
import org.apache.lucene.search.TermsFilter; | ||
import org.codehaus.jackson.JsonParser; | ||
import org.codehaus.jackson.JsonToken; | ||
import org.elasticsearch.index.AbstractIndexComponent; | ||
import org.elasticsearch.index.Index; | ||
import org.elasticsearch.index.mapper.FieldMapper; | ||
import org.elasticsearch.index.mapper.MapperService; | ||
import org.elasticsearch.index.query.QueryParsingException; | ||
import org.elasticsearch.index.settings.IndexSettings; | ||
import org.elasticsearch.util.settings.Settings; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.elasticsearch.index.query.support.QueryParsers.*; | ||
|
||
/** | ||
* @author kimchy (Shay Banon) | ||
*/ | ||
public class TermsJsonFilterParser extends AbstractIndexComponent implements JsonFilterParser { | ||
|
||
public static final String NAME = "terms"; | ||
|
||
@Inject public TermsJsonFilterParser(Index index, @IndexSettings Settings settings) { | ||
super(index, settings); | ||
} | ||
|
||
@Override public String name() { | ||
return NAME; | ||
} | ||
|
||
@Override public Filter parse(JsonQueryParseContext parseContext) throws IOException, QueryParsingException { | ||
JsonParser jp = parseContext.jp(); | ||
|
||
JsonToken token = jp.getCurrentToken(); | ||
if (token == JsonToken.START_OBJECT) { | ||
token = jp.nextToken(); | ||
} | ||
assert token == JsonToken.FIELD_NAME; | ||
String fieldName = jp.getCurrentName(); | ||
|
||
FieldMapper fieldMapper = null; | ||
MapperService.SmartNameFieldMappers smartNameFieldMappers = parseContext.smartFieldMappers(fieldName); | ||
if (smartNameFieldMappers != null) { | ||
fieldMapper = smartNameFieldMappers.fieldMappers().mapper(); | ||
if (fieldMapper != null) { | ||
fieldName = fieldMapper.indexName(); | ||
} | ||
} | ||
|
||
token = jp.nextToken(); | ||
if (token != JsonToken.START_ARRAY) { | ||
throw new QueryParsingException(index, "Terms filter must define the terms to filter on as an array"); | ||
} | ||
|
||
TermsFilter termsFilter = new TermsFilter(); | ||
while ((token = jp.nextToken()) != JsonToken.END_ARRAY) { | ||
String value = jp.getText(); | ||
if (value == null) { | ||
throw new QueryParsingException(index, "No value specified for term filter"); | ||
} | ||
if (fieldMapper != null) { | ||
value = fieldMapper.indexedValue(value); | ||
} | ||
termsFilter.addTerm(new Term(fieldName, value)); | ||
} | ||
jp.nextToken(); | ||
|
||
|
||
Filter filter = parseContext.cacheFilterIfPossible(termsFilter); | ||
return wrapSmartNameFilter(filter, smartNameFieldMappers, parseContext.filterCache()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
modules/elasticsearch/src/test/java/org/elasticsearch/index/query/json/terms-filter.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
filteredQuery : { | ||
query : { | ||
term : { "name.first" : "shay" } | ||
}, | ||
filter : { | ||
terms : { | ||
"name.last" : ["banon", "kimchy"] | ||
} | ||
} | ||
} | ||
} |