Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw parsing exception if terms filter or query has more than one field #5137

Merged
merged 1 commit into from Oct 12, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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