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.
This commit is contained in:
parent
c3dfe0846c
commit
1bcf0722c4
|
@ -94,22 +94,21 @@ public class SpanTermQueryBuilder extends BaseTermQueryBuilder<SpanTermQueryBuil
|
||||||
|
|
||||||
public static Optional<SpanTermQueryBuilder> fromXContent(QueryParseContext parseContext) throws IOException, ParsingException {
|
public static Optional<SpanTermQueryBuilder> fromXContent(QueryParseContext parseContext) throws IOException, ParsingException {
|
||||||
XContentParser parser = parseContext.parser();
|
XContentParser parser = parseContext.parser();
|
||||||
|
String fieldName = null;
|
||||||
XContentParser.Token token = parser.currentToken();
|
|
||||||
if (token == XContentParser.Token.START_OBJECT) {
|
|
||||||
token = parser.nextToken();
|
|
||||||
}
|
|
||||||
|
|
||||||
assert token == XContentParser.Token.FIELD_NAME;
|
|
||||||
String fieldName = parser.currentName();
|
|
||||||
|
|
||||||
|
|
||||||
Object value = null;
|
Object value = null;
|
||||||
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
|
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
|
||||||
String queryName = null;
|
String queryName = null;
|
||||||
token = parser.nextToken();
|
|
||||||
if (token == XContentParser.Token.START_OBJECT) {
|
|
||||||
String currentFieldName = null;
|
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) {
|
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
||||||
if (token == XContentParser.Token.FIELD_NAME) {
|
if (token == XContentParser.Token.FIELD_NAME) {
|
||||||
currentFieldName = parser.currentName();
|
currentFieldName = parser.currentName();
|
||||||
|
@ -128,15 +127,10 @@ public class SpanTermQueryBuilder extends BaseTermQueryBuilder<SpanTermQueryBuil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
parser.nextToken();
|
|
||||||
} else {
|
} else {
|
||||||
|
fieldName = parser.currentName();
|
||||||
value = parser.objectBytes();
|
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);
|
SpanTermQueryBuilder result = new SpanTermQueryBuilder(fieldName, value);
|
||||||
|
|
|
@ -23,6 +23,7 @@ import org.apache.lucene.index.Term;
|
||||||
import org.apache.lucene.search.Query;
|
import org.apache.lucene.search.Query;
|
||||||
import org.apache.lucene.search.TermQuery;
|
import org.apache.lucene.search.TermQuery;
|
||||||
import org.apache.lucene.search.spans.SpanTermQuery;
|
import org.apache.lucene.search.spans.SpanTermQuery;
|
||||||
|
import org.elasticsearch.common.ParsingException;
|
||||||
import org.elasticsearch.common.lucene.BytesRefs;
|
import org.elasticsearch.common.lucene.BytesRefs;
|
||||||
import org.elasticsearch.index.mapper.MappedFieldType;
|
import org.elasticsearch.index.mapper.MappedFieldType;
|
||||||
|
|
||||||
|
@ -108,4 +109,25 @@ public class SpanTermQueryBuilderTests extends AbstractTermQueryTestCase<SpanTer
|
||||||
assertEquals(json, "kimchy", parsed.value());
|
assertEquals(json, "kimchy", parsed.value());
|
||||||
assertEquals(json, 2.0, parsed.boost(), 0.0001);
|
assertEquals(json, 2.0, parsed.boost(), 0.0001);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testParseFailsWithMultipleFields() throws IOException {
|
||||||
|
String json = "{\n" +
|
||||||
|
" \"span_term\" : {\n" +
|
||||||
|
" \"message1\" : {\n" +
|
||||||
|
" \"term\" : \"this\"\n" +
|
||||||
|
" },\n" +
|
||||||
|
" \"message2\" : {\n" +
|
||||||
|
" \"term\" : \"this\"\n" +
|
||||||
|
" }\n" +
|
||||||
|
" }\n" +
|
||||||
|
"}";
|
||||||
|
|
||||||
|
try {
|
||||||
|
parseQuery(json);
|
||||||
|
fail("parseQuery should have failed");
|
||||||
|
} catch(ParsingException e) {
|
||||||
|
assertEquals("[span_term] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue