Skip to content

Commit 767c695

Browse files
authored
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
1 parent 15727ae commit 767c695

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

server/src/main/java/org/elasticsearch/index/search/QueryStringQueryParser.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,14 +280,14 @@ protected Query newMatchAllDocsQuery() {
280280

281281
@Override
282282
public Query getFieldQuery(String field, String queryText, boolean quoted) throws ParseException {
283-
if (quoted) {
284-
return getFieldQuery(field, queryText, getPhraseSlop());
285-
}
286-
287283
if (field != null && EXISTS_FIELD.equals(field)) {
288284
return existsQuery(queryText);
289285
}
290286

287+
if (quoted) {
288+
return getFieldQuery(field, queryText, getPhraseSlop());
289+
}
290+
291291
// Detects additional operators '<', '<=', '>', '>=' to handle range query with one side unbounded.
292292
// It is required to use a prefix field operator to enable the detection since they are not treated
293293
// as logical operator by the query parser (e.g. age:>=10).
@@ -333,6 +333,10 @@ public Query getFieldQuery(String field, String queryText, boolean quoted) throw
333333

334334
@Override
335335
protected Query getFieldQuery(String field, String queryText, int slop) throws ParseException {
336+
if (field != null && EXISTS_FIELD.equals(field)) {
337+
return existsQuery(queryText);
338+
}
339+
336340
Map<String, Float> fields = extractMultiFields(field, true);
337341
if (fields.isEmpty()) {
338342
return newUnmappedFieldQuery(field);

server/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,18 @@ public void testExistsFieldQuery() throws Exception {
998998
} else {
999999
assertThat(query, equalTo(new ConstantScoreQuery(new TermQuery(new Term("_field_names", STRING_FIELD_NAME)))));
10001000
}
1001+
1002+
for (boolean quoted : new boolean[] {true, false}) {
1003+
String value = (quoted ? "\"" : "") + STRING_FIELD_NAME + (quoted ? "\"" : "");
1004+
queryBuilder = new QueryStringQueryBuilder("_exists_:" + value);
1005+
query = queryBuilder.toQuery(context);
1006+
if (context.getIndexSettings().getIndexVersionCreated().onOrAfter(Version.V_6_1_0)
1007+
&& (context.fieldMapper(STRING_FIELD_NAME).omitNorms() == false)) {
1008+
assertThat(query, equalTo(new ConstantScoreQuery(new NormsFieldExistsQuery(STRING_FIELD_NAME))));
1009+
} else {
1010+
assertThat(query, equalTo(new ConstantScoreQuery(new TermQuery(new Term("_field_names", STRING_FIELD_NAME)))));
1011+
}
1012+
}
10011013
QueryShardContext contextNoType = createShardContextWithNoType();
10021014
query = queryBuilder.toQuery(contextNoType);
10031015
assertThat(query, equalTo(new MatchNoDocsQuery()));

0 commit comments

Comments
 (0)