Merge pull request #12756 from keety/terms_query_disable_coord

Add support for `disable_coord` param to `terms` query
This commit is contained in:
Adrien Grand 2015-08-11 16:13:58 +02:00
commit 99f4f09b84
3 changed files with 40 additions and 1 deletions

View File

@ -84,6 +84,8 @@ public class TermsQueryParser implements QueryParser {
String lookupRouting = null;
String minShouldMatch = null;
boolean disableCoord = false;
XContentParser.Token token;
List<Object> terms = Lists.newArrayList();
String fieldName = null;
@ -147,6 +149,8 @@ public class TermsQueryParser implements QueryParser {
minShouldMatch = parser.textOrNull();
} else if ("boost".equals(currentFieldName)) {
boost = parser.floatValue();
} else if (("disable_coord").equals(currentFieldName) || ("disableCoord").equals(currentFieldName)) {
disableCoord = parser.booleanValue();
} else if ("_name".equals(currentFieldName)) {
queryName = parser.text();
} else {
@ -191,7 +195,7 @@ public class TermsQueryParser implements QueryParser {
query = new TermsQuery(fieldName, filterValues);
}
} else {
BooleanQuery bq = new BooleanQuery();
BooleanQuery bq = new BooleanQuery(disableCoord);
for (Object term : terms) {
if (fieldType != null) {
bq.add(fieldType.termQuery(term, parseContext), Occur.SHOULD);

View File

@ -1074,6 +1074,33 @@ public class SimpleIndexQueryParserTests extends ESSingleNodeTestCase {
assertThat(((TermQuery) clauses[1].getQuery()).getTerm(), equalTo(new Term("name.first", "test")));
assertThat(clauses[1].getOccur(), equalTo(BooleanClause.Occur.SHOULD));
assertFalse("terms query disable_coord disabled by default", booleanQuery.isCoordDisabled());
}
@Test
public void testTermsQueryOptions() throws IOException {
IndexQueryParserService queryParser = queryParser();
String query = copyToStringFromClasspath("/org/elasticsearch/index/query/terms-query-options.json");
Query parsedQuery = queryParser.parse(query).query();
assertThat(parsedQuery, instanceOf(BooleanQuery.class));
BooleanQuery booleanQuery = (BooleanQuery) parsedQuery;
BooleanClause[] clauses = booleanQuery.getClauses();
assertThat(clauses.length, equalTo(3));
assertThat(((TermQuery) clauses[0].getQuery()).getTerm(), equalTo(new Term("name.first", "shay")));
assertThat(clauses[0].getOccur(), equalTo(BooleanClause.Occur.SHOULD));
assertThat(((TermQuery) clauses[1].getQuery()).getTerm(), equalTo(new Term("name.first", "test")));
assertThat(clauses[1].getOccur(), equalTo(BooleanClause.Occur.SHOULD));
assertThat(((TermQuery) clauses[2].getQuery()).getTerm(), equalTo(new Term("name.first", "elasticsearch")));
assertThat(clauses[2].getOccur(), equalTo(BooleanClause.Occur.SHOULD));
assertTrue("terms query disable_coord option mismatch", booleanQuery.isCoordDisabled());
assertThat(booleanQuery.getBoost(), equalTo(2.0f));
assertThat(booleanQuery.getMinimumNumberShouldMatch(), equalTo(2));
}
@Test

View File

@ -0,0 +1,8 @@
{
"terms":{
"name.first":["shay", "test", "elasticsearch"],
"disable_coord":true,
"boost":2.0,
"min_should_match":2
}
}