Fix quoted _exists_ query (#33019)

This change in the `query_string` query fixes the detection of the special
`_exists_` field when it is used with a quoted term.

Closes #28922
This commit is contained in:
Jim Ferenczi 2018-08-21 22:15:09 +02:00 committed by GitHub
parent 15727ae8ed
commit 767c69593c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 4 deletions

View File

@ -280,14 +280,14 @@ public class QueryStringQueryParser extends XQueryParser {
@Override @Override
public Query getFieldQuery(String field, String queryText, boolean quoted) throws ParseException { public Query getFieldQuery(String field, String queryText, boolean quoted) throws ParseException {
if (quoted) {
return getFieldQuery(field, queryText, getPhraseSlop());
}
if (field != null && EXISTS_FIELD.equals(field)) { if (field != null && EXISTS_FIELD.equals(field)) {
return existsQuery(queryText); return existsQuery(queryText);
} }
if (quoted) {
return getFieldQuery(field, queryText, getPhraseSlop());
}
// Detects additional operators '<', '<=', '>', '>=' to handle range query with one side unbounded. // Detects additional operators '<', '<=', '>', '>=' to handle range query with one side unbounded.
// It is required to use a prefix field operator to enable the detection since they are not treated // It is required to use a prefix field operator to enable the detection since they are not treated
// as logical operator by the query parser (e.g. age:>=10). // as logical operator by the query parser (e.g. age:>=10).
@ -333,6 +333,10 @@ public class QueryStringQueryParser extends XQueryParser {
@Override @Override
protected Query getFieldQuery(String field, String queryText, int slop) throws ParseException { protected Query getFieldQuery(String field, String queryText, int slop) throws ParseException {
if (field != null && EXISTS_FIELD.equals(field)) {
return existsQuery(queryText);
}
Map<String, Float> fields = extractMultiFields(field, true); Map<String, Float> fields = extractMultiFields(field, true);
if (fields.isEmpty()) { if (fields.isEmpty()) {
return newUnmappedFieldQuery(field); return newUnmappedFieldQuery(field);

View File

@ -998,6 +998,18 @@ public class QueryStringQueryBuilderTests extends AbstractQueryTestCase<QueryStr
} else { } else {
assertThat(query, equalTo(new ConstantScoreQuery(new TermQuery(new Term("_field_names", STRING_FIELD_NAME))))); assertThat(query, equalTo(new ConstantScoreQuery(new TermQuery(new Term("_field_names", STRING_FIELD_NAME)))));
} }
for (boolean quoted : new boolean[] {true, false}) {
String value = (quoted ? "\"" : "") + STRING_FIELD_NAME + (quoted ? "\"" : "");
queryBuilder = new QueryStringQueryBuilder("_exists_:" + value);
query = queryBuilder.toQuery(context);
if (context.getIndexSettings().getIndexVersionCreated().onOrAfter(Version.V_6_1_0)
&& (context.fieldMapper(STRING_FIELD_NAME).omitNorms() == false)) {
assertThat(query, equalTo(new ConstantScoreQuery(new NormsFieldExistsQuery(STRING_FIELD_NAME))));
} else {
assertThat(query, equalTo(new ConstantScoreQuery(new TermQuery(new Term("_field_names", STRING_FIELD_NAME)))));
}
}
QueryShardContext contextNoType = createShardContextWithNoType(); QueryShardContext contextNoType = createShardContextWithNoType();
query = queryBuilder.toQuery(contextNoType); query = queryBuilder.toQuery(contextNoType);
assertThat(query, equalTo(new MatchNoDocsQuery())); assertThat(query, equalTo(new MatchNoDocsQuery()));