From 1bcf0722c4bdc3978cf63faf64df40643d6c0f9f Mon Sep 17 00:00:00 2001 From: javanna Date: Wed, 3 Aug 2016 19:52:42 +0200 Subject: [PATCH] Throw parsing error if span_term query contains multiple fields Span term Query, like many other queries, used to parse even when the query referred to multiple fields and the first one would win. We rather throw an exception now instead. Also modified the parsing code to consume the whole query object. --- .../index/query/SpanTermQueryBuilder.java | 66 +++++++++---------- .../query/SpanTermQueryBuilderTests.java | 22 +++++++ 2 files changed, 52 insertions(+), 36 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/index/query/SpanTermQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/SpanTermQueryBuilder.java index 978ed1be3fe..1a77c73b0cc 100644 --- a/core/src/main/java/org/elasticsearch/index/query/SpanTermQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/SpanTermQueryBuilder.java @@ -94,49 +94,43 @@ public class SpanTermQueryBuilder extends BaseTermQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException, ParsingException { XContentParser parser = parseContext.parser(); - - XContentParser.Token token = parser.currentToken(); - if (token == XContentParser.Token.START_OBJECT) { - token = parser.nextToken(); - } - - assert token == XContentParser.Token.FIELD_NAME; - String fieldName = parser.currentName(); - - + String fieldName = null; Object value = null; float boost = AbstractQueryBuilder.DEFAULT_BOOST; String queryName = null; - token = parser.nextToken(); - if (token == XContentParser.Token.START_OBJECT) { - String currentFieldName = null; - while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { - if (token == XContentParser.Token.FIELD_NAME) { - currentFieldName = parser.currentName(); - } else { - if (parseContext.getParseFieldMatcher().match(currentFieldName, TERM_FIELD)) { - value = parser.objectBytes(); - } else if (parseContext.getParseFieldMatcher().match(currentFieldName, BaseTermQueryBuilder.VALUE_FIELD)) { - value = parser.objectBytes(); - } else if (parseContext.getParseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) { - boost = parser.floatValue(); - } else if (parseContext.getParseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) { - queryName = parser.text(); + String currentFieldName = null; + XContentParser.Token token; + while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { + if (token == XContentParser.Token.FIELD_NAME) { + currentFieldName = parser.currentName(); + } else if (token == XContentParser.Token.START_OBJECT) { + if (fieldName != null) { + throw new ParsingException(parser.getTokenLocation(), "[span_term] query doesn't support multiple fields, found [" + + fieldName + "] and [" + currentFieldName + "]"); + } + fieldName = currentFieldName; + while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { + if (token == XContentParser.Token.FIELD_NAME) { + currentFieldName = parser.currentName(); } else { - throw new ParsingException(parser.getTokenLocation(), - "[span_term] query does not support [" + currentFieldName + "]"); + if (parseContext.getParseFieldMatcher().match(currentFieldName, TERM_FIELD)) { + value = parser.objectBytes(); + } else if (parseContext.getParseFieldMatcher().match(currentFieldName, BaseTermQueryBuilder.VALUE_FIELD)) { + value = parser.objectBytes(); + } else if (parseContext.getParseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) { + boost = parser.floatValue(); + } else if (parseContext.getParseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) { + queryName = parser.text(); + } else { + throw new ParsingException(parser.getTokenLocation(), + "[span_term] query does not support [" + currentFieldName + "]"); + } } } + } else { + fieldName = parser.currentName(); + value = parser.objectBytes(); } - parser.nextToken(); - } else { - value = parser.objectBytes(); - // move to the next token - parser.nextToken(); - } - - if (value == null) { - throw new ParsingException(parser.getTokenLocation(), "No value specified for term query"); } SpanTermQueryBuilder result = new SpanTermQueryBuilder(fieldName, value); diff --git a/core/src/test/java/org/elasticsearch/index/query/SpanTermQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/SpanTermQueryBuilderTests.java index 546c8536ad9..da76dd15371 100644 --- a/core/src/test/java/org/elasticsearch/index/query/SpanTermQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/SpanTermQueryBuilderTests.java @@ -23,6 +23,7 @@ import org.apache.lucene.index.Term; import org.apache.lucene.search.Query; import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.spans.SpanTermQuery; +import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.lucene.BytesRefs; import org.elasticsearch.index.mapper.MappedFieldType; @@ -108,4 +109,25 @@ public class SpanTermQueryBuilderTests extends AbstractTermQueryTestCase