Skip to content

Commit

Permalink
Support terms filter, closes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
kimchy committed Feb 9, 2010
1 parent 78c2205 commit bd2b0a6
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ public static TermJsonFilterBuilder termFilter(String name, double value) {
return new TermJsonFilterBuilder(name, value);
}

public static TermsJsonFilterBuilder termsFilter(String name, String... values) {
return new TermsJsonFilterBuilder(name, values);
}

public static TermsJsonFilterBuilder termsFilter(String name, int... values) {
return new TermsJsonFilterBuilder(name, values);
}

public static TermsJsonFilterBuilder termsFilter(String name, long... values) {
return new TermsJsonFilterBuilder(name, values);
}

public static TermsJsonFilterBuilder termsFilter(String name, float... values) {
return new TermsJsonFilterBuilder(name, values);
}

public static TermsJsonFilterBuilder termsFilter(String name, double... values) {
return new TermsJsonFilterBuilder(name, values);
}

public static PrefixJsonFilterBuilder prefixFilter(String name, String value) {
return new PrefixJsonFilterBuilder(name, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public JsonQueryParserRegistry(Index index,
Map<String, JsonFilterParser> filterParsersMap = newHashMap();
// add defaults
add(filterParsersMap, new TermJsonFilterParser(index, indexSettings));
add(filterParsersMap, new TermsJsonFilterParser(index, indexSettings));
add(filterParsersMap, new RangeJsonFilterParser(index, indexSettings));
add(filterParsersMap, new PrefixJsonFilterParser(index, indexSettings));
add(filterParsersMap, new QueryJsonFilterParser(index, indexSettings));
Expand Down
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();
}
}
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Set;

import static org.elasticsearch.index.query.json.JsonFilterBuilders.*;
import static org.elasticsearch.index.query.json.JsonQueryBuilders.*;
Expand Down Expand Up @@ -376,6 +377,36 @@ public class SimpleJsonIndexQueryParserTests {
assertThat(((TermFilter) filteredQuery.getFilter()).getTerm(), equalTo(new Term("name.last", "banon")));
}

@Test public void testTermsFilterQueryBuilder() throws Exception {
IndexQueryParser queryParser = newQueryParser();
Query parsedQuery = queryParser.parse(filteredQuery(termQuery("name.first", "shay"), termsFilter("name.last", "banon", "kimchy")).build());
assertThat(parsedQuery, instanceOf(FilteredQuery.class));
FilteredQuery filteredQuery = (FilteredQuery) parsedQuery;
assertThat(filteredQuery.getFilter(), instanceOf(TermsFilter.class));
TermsFilter termsFilter = (TermsFilter) filteredQuery.getFilter();
Field field = TermsFilter.class.getDeclaredField("terms");
field.setAccessible(true);
Set<Term> terms = (Set<Term>) field.get(termsFilter);
assertThat(terms.size(), equalTo(2));
assertThat(terms.iterator().next().text(), equalTo("banon"));
}


@Test public void testTermsFilterQuery() throws Exception {
IndexQueryParser queryParser = newQueryParser();
String query = copyToStringFromClasspath("/org/elasticsearch/index/query/json/terms-filter.json");
Query parsedQuery = queryParser.parse(query);
assertThat(parsedQuery, instanceOf(FilteredQuery.class));
FilteredQuery filteredQuery = (FilteredQuery) parsedQuery;
assertThat(filteredQuery.getFilter(), instanceOf(TermsFilter.class));
TermsFilter termsFilter = (TermsFilter) filteredQuery.getFilter();
Field field = TermsFilter.class.getDeclaredField("terms");
field.setAccessible(true);
Set<Term> terms = (Set<Term>) field.get(termsFilter);
assertThat(terms.size(), equalTo(2));
assertThat(terms.iterator().next().text(), equalTo("banon"));
}

@Test public void testConstantScoreQueryBuilder() throws IOException {
IndexQueryParser queryParser = newQueryParser();
Query parsedQuery = queryParser.parse(constantScoreQuery(termFilter("name.last", "banon")));
Expand Down
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"]
}
}
}
}

0 comments on commit bd2b0a6

Please sign in to comment.