Skip to content

Commit

Permalink
Throw parsing exception if terms filter or query has more than one field
Browse files Browse the repository at this point in the history
Closes #5014
  • Loading branch information
imotov committed Oct 12, 2014
1 parent 08ba5ba commit 28a7d63
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
Expand Up @@ -101,6 +101,9 @@ public Filter parse(QueryParseContext parseContext) throws IOException, QueryPar
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_ARRAY) {
if (fieldName != null) {
throw new QueryParsingException(parseContext.index(), "[terms] filter does not support multiple fields");
}
fieldName = currentFieldName;

while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
Expand Down
Expand Up @@ -75,6 +75,9 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_ARRAY) {
if (fieldName != null) {
throw new QueryParsingException(parseContext.index(), "[terms] query does not support multiple fields");
}
fieldName = currentFieldName;
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
Object value = parser.objectBytes();
Expand Down
Expand Up @@ -52,6 +52,7 @@
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.cache.filter.support.CacheKeyFilter;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.core.NumberFieldMapper;
Expand Down Expand Up @@ -1110,6 +1111,38 @@ public void testTermsQuery() throws IOException {
assertThat(clauses[1].getOccur(), equalTo(BooleanClause.Occur.SHOULD));
}

@Test
public void testTermsQueryWithMultipleFields() throws IOException {
IndexQueryParserService queryParser = queryParser();
String query = XContentFactory.jsonBuilder().startObject()
.startObject("terms").array("foo", 123).array("bar", 456).endObject()
.endObject().string();
try {
queryParser.parse(query).query();
fail();
} catch (QueryParsingException ex) {
assertThat(ex.getMessage(), equalTo("[test] [terms] query does not support multiple fields"));
}
}

@Test
public void testTermsFilterWithMultipleFields() throws IOException {
IndexQueryParserService queryParser = queryParser();
String query = XContentFactory.jsonBuilder().startObject()
.startObject("filtered")
.startObject("query").startObject("match_all").endObject().endObject()
.startObject("filter").startObject("terms").array("foo", 123).array("bar", 456).endObject().endObject()
.endObject().string();
try {
queryParser.parse(query).query();
fail();
} catch (QueryParsingException ex) {
assertThat(ex.getMessage(), equalTo("[test] [terms] filter does not support multiple fields"));
}
}



@Test
public void testInQuery() throws IOException {
IndexQueryParserService queryParser = queryParser();
Expand Down

0 comments on commit 28a7d63

Please sign in to comment.