Skip to content

Commit c122fc6

Browse files
authored
SQL: add "fuzziness" option to QUERY and MATCH function predicates (#40529)
* Remove unused "locale" and "lowercase_expanded_terms" options from QUERY.
1 parent e307061 commit c122fc6

File tree

5 files changed

+86
-28
lines changed

5 files changed

+86
-28
lines changed

docs/reference/sql/functions/search.asciidoc

+5-5
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ case of an `or` operator or all of the low frequency terms in the case of an `an
6161
<<query-dsl-match-query-cutoff>> page.
6262

6363
NOTE: The allowed optional parameters for a single-field `MATCH()` variant (for the `match` {es} query) are: `analyzer`, `auto_generate_synonyms_phrase_query`,
64-
`cutoff_frequency`, `lenient`, `fuzzy_transpositions`, `fuzzy_rewrite`, `minimum_should_match`, `operator`,
64+
`cutoff_frequency`, `lenient`, `fuzziness`, `fuzzy_transpositions`, `fuzzy_rewrite`, `minimum_should_match`, `operator`,
6565
`max_expansions`, `prefix_length`.
6666

6767
NOTE: The allowed optional parameters for a multi-field `MATCH()` variant (for the `multi_match` {es} query) are: `analyzer`, `auto_generate_synonyms_phrase_query`,
68-
`cutoff_frequency`, `lenient`, `fuzzy_transpositions`, `fuzzy_rewrite`, `minimum_should_match`, `operator`,
68+
`cutoff_frequency`, `lenient`, `fuzziness`, `fuzzy_transpositions`, `fuzzy_rewrite`, `minimum_should_match`, `operator`,
6969
`max_expansions`, `prefix_length`, `slop`, `tie_breaker`, `type`.
7070

7171

@@ -115,9 +115,9 @@ include-tagged::{sql-specs}/docs/docs.csv-spec[optionalParameterQuery]
115115

116116
NOTE: The allowed optional parameters for `QUERY()` are: `allow_leading_wildcard`, `analyze_wildcard`, `analyzer`,
117117
`auto_generate_synonyms_phrase_query`, `default_field`, `default_operator`, `enable_position_increments`,
118-
`escape`, `fuzzy_max_expansions`, `fuzzy_prefix_length`, `fuzzy_rewrite`, `fuzzy_transpositions`, `lenient`,
119-
`locale`, `lowercase_expanded_terms`, `max_determinized_states`, `minimum_should_match`, `phrase_slop`, `rewrite`,
120-
`quote_analyzer`, `quote_field_suffix`, `tie_breaker`, `time_zone`, `type`.
118+
`escape`, `fuzziness`, `fuzzy_max_expansions`, `fuzzy_prefix_length`, `fuzzy_rewrite`, `fuzzy_transpositions`,
119+
`lenient`, `max_determinized_states`, `minimum_should_match`, `phrase_slop`, `rewrite`, `quote_analyzer`,
120+
`quote_field_suffix`, `tie_breaker`, `time_zone`, `type`.
121121

122122

123123
[[sql-functions-search-score]]

x-pack/plugin/sql/qa/src/main/resources/fulltext.csv-spec

+54
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,60 @@ SELECT emp_no, first_name, gender, last_name FROM test_emp WHERE QUERY('Man*', '
3030
10096 |Jayson |M |Mandell
3131
;
3232

33+
matchWithFuzziness
34+
SELECT first_name, SCORE() FROM test_emp WHERE MATCH(first_name, 'geo', 'fuzziness=6');
35+
36+
first_name:s | SCORE():f
37+
----------------+---------------
38+
Gino |1.3684646
39+
Gao |2.7369292
40+
;
41+
42+
matchWithFuzzinessAuto
43+
SELECT first_name, SCORE() FROM test_emp WHERE MATCH(first_name, 'geo', 'fuzziness=AUTO:1,7;fuzzy_rewrite=scoring_boolean');
44+
45+
first_name:s | SCORE():f
46+
----------------+---------------
47+
Gao |2.7369292
48+
;
49+
50+
multiMatchWithFuzzinessAuto
51+
SELECT first_name, last_name, SCORE() FROM test_emp WHERE MATCH('first_name^3,last_name^5', 'geo hir', 'fuzziness=AUTO:1,5;operator=or') ORDER BY first_name;
52+
53+
first_name:s | last_name:s | SCORE():f
54+
----------------+-----------------+---------------
55+
Gao |Dolinsky |8.210788
56+
Shir |McClurg |8.210788
57+
;
58+
59+
multiMatchWithFuzziness
60+
SELECT first_name, last_name, SCORE() FROM test_emp WHERE MATCH('first_name^3,last_name^5', 'geo hir', 'fuzziness=5;operator=or') ORDER BY first_name;
61+
62+
first_name:s | last_name:s | SCORE():f
63+
----------------+-----------------+---------------
64+
Gao |Dolinsky |8.210788
65+
Gino |Leonhardt |4.105394
66+
Shir |McClurg |8.210788
67+
Uri |Lenart |4.105394
68+
;
69+
70+
queryWithFuzziness
71+
SELECT first_name, SCORE() FROM test_emp WHERE QUERY('geo~', 'fuzziness=5;default_field=first_name');
72+
73+
first_name:s | SCORE():f
74+
----------------+---------------
75+
Gino |1.3684646
76+
Gao |2.7369292
77+
;
78+
79+
queryWithFuzzinessAuto
80+
SELECT first_name, SCORE() FROM test_emp WHERE QUERY('geo~', 'fuzziness=AUTO:1,5;default_field=first_name');
81+
82+
first_name:s | SCORE():f
83+
----------------+---------------
84+
Gao |2.7369292
85+
;
86+
3387
matchQuery
3488
SELECT emp_no, first_name, gender, last_name FROM test_emp WHERE MATCH(first_name, 'Erez');
3589

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/query/MatchQuery.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package org.elasticsearch.xpack.sql.querydsl.query;
77

88
import org.elasticsearch.common.Booleans;
9+
import org.elasticsearch.common.unit.Fuzziness;
910
import org.elasticsearch.index.query.MatchQueryBuilder;
1011
import org.elasticsearch.index.query.Operator;
1112
import org.elasticsearch.index.query.QueryBuilder;
@@ -28,16 +29,17 @@ public class MatchQuery extends LeafQuery {
2829
// TODO: it'd be great if these could be constants instead of Strings, needs a core change to make the fields public first
2930
// TODO: add zero terms query support, I'm not sure the best way to parse it yet...
3031
// appliers.put("zero_terms_query", (qb, s) -> qb.zeroTermsQuery(s));
32+
appliers.put("analyzer", (qb, s) -> qb.analyzer(s));
33+
appliers.put("auto_generate_synonyms_phrase_query", (qb, s) -> qb.autoGenerateSynonymsPhraseQuery(Booleans.parseBoolean(s)));
3134
appliers.put("cutoff_frequency", (qb, s) -> qb.cutoffFrequency(Float.valueOf(s)));
32-
appliers.put("lenient", (qb, s) -> qb.lenient(Booleans.parseBoolean(s)));
35+
appliers.put("fuzziness", (qb, s) -> qb.fuzziness(Fuzziness.build(s)));
3336
appliers.put("fuzzy_transpositions", (qb, s) -> qb.fuzzyTranspositions(Booleans.parseBoolean(s)));
3437
appliers.put("fuzzy_rewrite", (qb, s) -> qb.fuzzyRewrite(s));
38+
appliers.put("lenient", (qb, s) -> qb.lenient(Booleans.parseBoolean(s)));
39+
appliers.put("max_expansions", (qb, s) -> qb.maxExpansions(Integer.valueOf(s)));
3540
appliers.put("minimum_should_match", (qb, s) -> qb.minimumShouldMatch(s));
3641
appliers.put("operator", (qb, s) -> qb.operator(Operator.fromString(s)));
37-
appliers.put("max_expansions", (qb, s) -> qb.maxExpansions(Integer.valueOf(s)));
3842
appliers.put("prefix_length", (qb, s) -> qb.prefixLength(Integer.valueOf(s)));
39-
appliers.put("analyzer", (qb, s) -> qb.analyzer(s));
40-
appliers.put("auto_generate_synonyms_phrase_query", (qb, s) -> qb.autoGenerateSynonymsPhraseQuery(Booleans.parseBoolean(s)));
4143
BUILDER_APPLIERS = Collections.unmodifiableMap(appliers);
4244
}
4345

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/query/MultiMatchQuery.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package org.elasticsearch.xpack.sql.querydsl.query;
77

88
import org.elasticsearch.common.Booleans;
9+
import org.elasticsearch.common.unit.Fuzziness;
910
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
1011
import org.elasticsearch.index.query.Operator;
1112
import org.elasticsearch.index.query.QueryBuilder;
@@ -29,18 +30,19 @@ public class MultiMatchQuery extends LeafQuery {
2930
appliers.put("slop", (qb, s) -> qb.slop(Integer.valueOf(s)));
3031
// TODO: add zero terms query support, I'm not sure the best way to parse it yet...
3132
// appliers.put("zero_terms_query", (qb, s) -> qb.zeroTermsQuery(s));
32-
appliers.put("lenient", (qb, s) -> qb.lenient(Booleans.parseBoolean(s)));
33+
appliers.put("analyzer", (qb, s) -> qb.analyzer(s));
34+
appliers.put("auto_generate_synonyms_phrase_query", (qb, s) -> qb.autoGenerateSynonymsPhraseQuery(Booleans.parseBoolean(s)));
3335
appliers.put("cutoff_frequency", (qb, s) -> qb.cutoffFrequency(Float.valueOf(s)));
34-
appliers.put("tie_breaker", (qb, s) -> qb.tieBreaker(Float.valueOf(s)));
36+
appliers.put("fuzziness", (qb, s) -> qb.fuzziness(Fuzziness.build(s)));
3537
appliers.put("fuzzy_rewrite", (qb, s) -> qb.fuzzyRewrite(s));
38+
appliers.put("fuzzy_transpositions", (qb, s) -> qb.fuzzyTranspositions(Booleans.parseBoolean(s)));
39+
appliers.put("lenient", (qb, s) -> qb.lenient(Booleans.parseBoolean(s)));
40+
appliers.put("max_expansions", (qb, s) -> qb.maxExpansions(Integer.valueOf(s)));
3641
appliers.put("minimum_should_match", (qb, s) -> qb.minimumShouldMatch(s));
3742
appliers.put("operator", (qb, s) -> qb.operator(Operator.fromString(s)));
38-
appliers.put("max_expansions", (qb, s) -> qb.maxExpansions(Integer.valueOf(s)));
3943
appliers.put("prefix_length", (qb, s) -> qb.prefixLength(Integer.valueOf(s)));
40-
appliers.put("analyzer", (qb, s) -> qb.analyzer(s));
44+
appliers.put("tie_breaker", (qb, s) -> qb.tieBreaker(Float.valueOf(s)));
4145
appliers.put("type", (qb, s) -> qb.type(s));
42-
appliers.put("auto_generate_synonyms_phrase_query", (qb, s) -> qb.autoGenerateSynonymsPhraseQuery(Booleans.parseBoolean(s)));
43-
appliers.put("fuzzy_transpositions", (qb, s) -> qb.fuzzyTranspositions(Booleans.parseBoolean(s)));
4446
BUILDER_APPLIERS = Collections.unmodifiableMap(appliers);
4547
}
4648

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/query/QueryStringQuery.java

+13-13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package org.elasticsearch.xpack.sql.querydsl.query;
77

88
import org.elasticsearch.common.Booleans;
9+
import org.elasticsearch.common.unit.Fuzziness;
910
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
1011
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
1112
import org.elasticsearch.index.query.Operator;
@@ -28,30 +29,29 @@ public class QueryStringQuery extends LeafQuery {
2829
static {
2930
HashMap<String, BiConsumer<QueryStringQueryBuilder, String>> appliers = new HashMap<>(28);
3031
// TODO: it'd be great if these could be constants instead of Strings, needs a core change to make the fields public first
32+
appliers.put("allow_leading_wildcard", (qb, s) -> qb.allowLeadingWildcard(Booleans.parseBoolean(s)));
33+
appliers.put("analyze_wildcard", (qb, s) -> qb.analyzeWildcard(Booleans.parseBoolean(s)));
34+
appliers.put("analyzer", (qb, s) -> qb.analyzer(s));
35+
appliers.put("auto_generate_synonyms_phrase_query", (qb, s) -> qb.autoGenerateSynonymsPhraseQuery(Booleans.parseBoolean(s)));
3136
appliers.put("default_field", (qb, s) -> qb.defaultField(s));
3237
appliers.put("default_operator", (qb, s) -> qb.defaultOperator(Operator.fromString(s)));
33-
appliers.put("analyzer", (qb, s) -> qb.analyzer(s));
34-
appliers.put("quote_analyzer", (qb, s) -> qb.quoteAnalyzer(s));
35-
appliers.put("allow_leading_wildcard", (qb, s) -> qb.allowLeadingWildcard(Booleans.parseBoolean(s)));
36-
appliers.put("max_determinized_states", (qb, s) -> qb.maxDeterminizedStates(Integer.valueOf(s)));
37-
appliers.put("lowercase_expanded_terms", (qb, s) -> {});
3838
appliers.put("enable_position_increments", (qb, s) -> qb.enablePositionIncrements(Booleans.parseBoolean(s)));
3939
appliers.put("escape", (qb, s) -> qb.escape(Booleans.parseBoolean(s)));
40-
appliers.put("fuzzy_prefix_length", (qb, s) -> qb.fuzzyPrefixLength(Integer.valueOf(s)));
40+
appliers.put("fuzziness", (qb, s) -> qb.fuzziness(Fuzziness.build(s)));
4141
appliers.put("fuzzy_max_expansions", (qb, s) -> qb.fuzzyMaxExpansions(Integer.valueOf(s)));
42+
appliers.put("fuzzy_prefix_length", (qb, s) -> qb.fuzzyPrefixLength(Integer.valueOf(s)));
4243
appliers.put("fuzzy_rewrite", (qb, s) -> qb.fuzzyRewrite(s));
44+
appliers.put("fuzzy_transpositions", (qb, s) -> qb.fuzzyTranspositions(Booleans.parseBoolean(s)));
45+
appliers.put("lenient", (qb, s) -> qb.lenient(Booleans.parseBoolean(s)));
46+
appliers.put("max_determinized_states", (qb, s) -> qb.maxDeterminizedStates(Integer.valueOf(s)));
47+
appliers.put("minimum_should_match", (qb, s) -> qb.minimumShouldMatch(s));
4348
appliers.put("phrase_slop", (qb, s) -> qb.phraseSlop(Integer.valueOf(s)));
44-
appliers.put("tie_breaker", (qb, s) -> qb.tieBreaker(Float.valueOf(s)));
45-
appliers.put("analyze_wildcard", (qb, s) -> qb.analyzeWildcard(Booleans.parseBoolean(s)));
4649
appliers.put("rewrite", (qb, s) -> qb.rewrite(s));
47-
appliers.put("minimum_should_match", (qb, s) -> qb.minimumShouldMatch(s));
50+
appliers.put("quote_analyzer", (qb, s) -> qb.quoteAnalyzer(s));
4851
appliers.put("quote_field_suffix", (qb, s) -> qb.quoteFieldSuffix(s));
49-
appliers.put("lenient", (qb, s) -> qb.lenient(Booleans.parseBoolean(s)));
50-
appliers.put("locale", (qb, s) -> {});
52+
appliers.put("tie_breaker", (qb, s) -> qb.tieBreaker(Float.valueOf(s)));
5153
appliers.put("time_zone", (qb, s) -> qb.timeZone(s));
5254
appliers.put("type", (qb, s) -> qb.type(MultiMatchQueryBuilder.Type.parse(s, LoggingDeprecationHandler.INSTANCE)));
53-
appliers.put("auto_generate_synonyms_phrase_query", (qb, s) -> qb.autoGenerateSynonymsPhraseQuery(Booleans.parseBoolean(s)));
54-
appliers.put("fuzzy_transpositions", (qb, s) -> qb.fuzzyTranspositions(Booleans.parseBoolean(s)));
5555
BUILDER_APPLIERS = Collections.unmodifiableMap(appliers);
5656
}
5757

0 commit comments

Comments
 (0)