Skip to content

Commit

Permalink
Fix quoted _exists_ query (elastic#33019)
Browse files Browse the repository at this point in the history
This change in the `query_string` query fixes the detection of the special
`_exists_` field when it is used with a quoted term.

Closes elastic#28922
  • Loading branch information
jimczi authored Aug 21, 2018
1 parent 15727ae commit 767c695
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,14 @@ protected Query newMatchAllDocsQuery() {

@Override
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)) {
return existsQuery(queryText);
}

if (quoted) {
return getFieldQuery(field, queryText, getPhraseSlop());
}

// 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
// as logical operator by the query parser (e.g. age:>=10).
Expand Down Expand Up @@ -333,6 +333,10 @@ public Query getFieldQuery(String field, String queryText, boolean quoted) throw

@Override
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);
if (fields.isEmpty()) {
return newUnmappedFieldQuery(field);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,18 @@ public void testExistsFieldQuery() throws Exception {
} else {
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();
query = queryBuilder.toQuery(contextNoType);
assertThat(query, equalTo(new MatchNoDocsQuery()));
Expand Down

0 comments on commit 767c695

Please sign in to comment.