Fixed quote_field_suffix in query_string (#29332)

This change fixes the handling of the `quote_field_suffix` option on `query_string`
 query. The expansion was not applied to default fields query.

Closes #29324
This commit is contained in:
Jim Ferenczi 2018-04-04 17:29:09 +02:00 committed by GitHub
parent 08abbdf129
commit 644e5ea97a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 0 deletions

View File

@ -66,6 +66,7 @@ import static org.elasticsearch.common.lucene.search.Queries.fixNegativeQueryIfN
import static org.elasticsearch.common.lucene.search.Queries.newLenientFieldQuery;
import static org.elasticsearch.common.lucene.search.Queries.newUnmappedFieldQuery;
import static org.elasticsearch.index.search.QueryParserHelper.resolveMappingField;
import static org.elasticsearch.index.search.QueryParserHelper.resolveMappingFields;
/**
* A {@link XQueryParser} that uses the {@link MapperService} in order to build smarter
@ -264,6 +265,8 @@ public class QueryStringQueryParser extends XQueryParser {
// Filters unsupported fields if a pattern is requested
// Filters metadata fields if all fields are requested
return resolveMappingField(context, field, 1.0f, !allFields, !multiFields, quoted ? quoteFieldSuffix : null);
} else if (quoted && quoteFieldSuffix != null) {
return resolveMappingFields(context, fieldsAndWeights, quoteFieldSuffix);
} else {
return fieldsAndWeights;
}

View File

@ -1040,6 +1040,37 @@ public class QueryStringQueryBuilderTests extends AbstractQueryTestCase<QueryStr
assertEquals(expectedQuery, query);
}
public void testQuoteFieldSuffix() throws IOException {
assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
QueryShardContext context = createShardContext();
assertEquals(new TermQuery(new Term(STRING_FIELD_NAME, "bar")),
new QueryStringQueryBuilder("bar")
.quoteFieldSuffix("_2")
.field(STRING_FIELD_NAME)
.doToQuery(context)
);
assertEquals(new TermQuery(new Term(STRING_FIELD_NAME_2, "bar")),
new QueryStringQueryBuilder("\"bar\"")
.quoteFieldSuffix("_2")
.field(STRING_FIELD_NAME)
.doToQuery(context)
);
// Now check what happens if the quote field does not exist
assertEquals(new TermQuery(new Term(STRING_FIELD_NAME, "bar")),
new QueryStringQueryBuilder("bar")
.quoteFieldSuffix(".quote")
.field(STRING_FIELD_NAME)
.doToQuery(context)
);
assertEquals(new TermQuery(new Term(STRING_FIELD_NAME, "bar")),
new QueryStringQueryBuilder("\"bar\"")
.quoteFieldSuffix(".quote")
.field(STRING_FIELD_NAME)
.doToQuery(context)
);
}
public void testToFuzzyQuery() throws Exception {
assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);