From 9a385de07721743c764089f811a82f15c80bc221 Mon Sep 17 00:00:00 2001 From: keety Date: Sun, 9 Aug 2015 18:47:25 -0400 Subject: [PATCH] fix to support disable_coord param in terms query --- .../index/query/TermsQueryParser.java | 6 ++++- .../query/SimpleIndexQueryParserTests.java | 27 +++++++++++++++++++ .../index/query/terms-query-options.json | 8 ++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 core/src/test/java/org/elasticsearch/index/query/terms-query-options.json diff --git a/core/src/main/java/org/elasticsearch/index/query/TermsQueryParser.java b/core/src/main/java/org/elasticsearch/index/query/TermsQueryParser.java index 2930b94e0cb..09f578d4bfa 100644 --- a/core/src/main/java/org/elasticsearch/index/query/TermsQueryParser.java +++ b/core/src/main/java/org/elasticsearch/index/query/TermsQueryParser.java @@ -84,6 +84,8 @@ public class TermsQueryParser implements QueryParser { String lookupRouting = null; String minShouldMatch = null; + boolean disableCoord = false; + XContentParser.Token token; List 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); diff --git a/core/src/test/java/org/elasticsearch/index/query/SimpleIndexQueryParserTests.java b/core/src/test/java/org/elasticsearch/index/query/SimpleIndexQueryParserTests.java index 5381c92a3f9..22d751b8f1e 100644 --- a/core/src/test/java/org/elasticsearch/index/query/SimpleIndexQueryParserTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/SimpleIndexQueryParserTests.java @@ -1073,6 +1073,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 diff --git a/core/src/test/java/org/elasticsearch/index/query/terms-query-options.json b/core/src/test/java/org/elasticsearch/index/query/terms-query-options.json new file mode 100644 index 00000000000..48263a5bc6e --- /dev/null +++ b/core/src/test/java/org/elasticsearch/index/query/terms-query-options.json @@ -0,0 +1,8 @@ +{ + "terms":{ + "name.first":["shay", "test", "elasticsearch"], + "disable_coord":true, + "boost":2.0, + "min_should_match":2 + } +} \ No newline at end of file