Rewrite match and match_phrase queries to term queries on keyword fields (#82612)

Term queries can in certain circumstances (eg when run against constant keyword
fields) rewrite themselves to match_no_docs queries, which is very useful for filtering
out shards from searches and field_caps requests. But match and match_phrase
queries can reduce down to simple term queries when there is no fuzziness defined
on them, and when they are run using a keyword analyzer.

This commit makes simple match and match_phrase rewrite themselves to term
queries when run against keyword fields.

Fixes #82515
This commit is contained in:
Alan Woodward 2022-01-17 17:02:07 +00:00 committed by GitHub
parent f287852424
commit 2d77ef57cf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 177 additions and 14 deletions

View file

@ -63,26 +63,26 @@ public class QueryBuilderBWCIT extends AbstractFullClusterRestartTestCase {
static {
addCandidate("""
"match": { "keyword_field": "value"}
""", new MatchQueryBuilder("keyword_field", "value"));
"match": { "text_field": "value"}
""", new MatchQueryBuilder("text_field", "value"));
addCandidate("""
"match": { "keyword_field": {"query": "value", "operator": "and"} }
""", new MatchQueryBuilder("keyword_field", "value").operator(Operator.AND));
"match": { "text_field": {"query": "value", "operator": "and"} }
""", new MatchQueryBuilder("text_field", "value").operator(Operator.AND));
addCandidate("""
"match": { "keyword_field": {"query": "value", "analyzer": "english"} }
""", new MatchQueryBuilder("keyword_field", "value").analyzer("english"));
"match": { "text_field": {"query": "value", "analyzer": "english"} }
""", new MatchQueryBuilder("text_field", "value").analyzer("english"));
addCandidate("""
"match": { "keyword_field": {"query": "value", "minimum_should_match": 3} }
""", new MatchQueryBuilder("keyword_field", "value").minimumShouldMatch("3"));
"match": { "text_field": {"query": "value", "minimum_should_match": 3} }
""", new MatchQueryBuilder("text_field", "value").minimumShouldMatch("3"));
addCandidate("""
"match": { "keyword_field": {"query": "value", "fuzziness": "auto"} }
""", new MatchQueryBuilder("keyword_field", "value").fuzziness(Fuzziness.AUTO));
"match": { "text_field": {"query": "value", "fuzziness": "auto"} }
""", new MatchQueryBuilder("text_field", "value").fuzziness(Fuzziness.AUTO));
addCandidate("""
"match_phrase": { "keyword_field": "value"}
""", new MatchPhraseQueryBuilder("keyword_field", "value"));
"match_phrase": { "text_field": "value"}
""", new MatchPhraseQueryBuilder("text_field", "value"));
addCandidate("""
"match_phrase": { "keyword_field": {"query": "value", "slop": 3}}
""", new MatchPhraseQueryBuilder("keyword_field", "value").slop(3));
"match_phrase": { "text_field": {"query": "value", "slop": 3}}
""", new MatchPhraseQueryBuilder("text_field", "value").slop(3));
addCandidate("""
"range": { "long_field": {"gte": 1, "lte": 9}}
""", new RangeQueryBuilder("long_field").from(1).to(9));
@ -179,6 +179,11 @@ public class QueryBuilderBWCIT extends AbstractFullClusterRestartTestCase {
mappingsAndSettings.field("type", "keyword");
mappingsAndSettings.endObject();
}
{
mappingsAndSettings.startObject("text_field");
mappingsAndSettings.field("type", "text");
mappingsAndSettings.endObject();
}
{
mappingsAndSettings.startObject("long_field");
mappingsAndSettings.field("type", "long");