Skip to content

Commit

Permalink
Refactor fuzziness interface on query builders
Browse files Browse the repository at this point in the history
Signed-off-by: noCharger <[email protected]>
  • Loading branch information
noCharger committed Dec 2, 2022
1 parent 0210b76 commit 19c050f
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Fix 'org.apache.hc.core5.http.ParseException: Invalid protocol version' under JDK 16+ ([#4827](https://github.com/opensearch-project/OpenSearch/pull/4827))
- Fixed compression support for h2c protocol ([#4944](https://github.com/opensearch-project/OpenSearch/pull/4944))
- Reject bulk requests with invalid actions ([#5299](https://github.com/opensearch-project/OpenSearch/issues/5299))
- Refactor fuzziness interface on query builders ([#5433](https://github.com/opensearch-project/OpenSearch/pull/5433))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,9 @@ public Object value() {
return maybeConvertToString(this.value);
}

public FuzzyQueryBuilder fuzziness(Fuzziness fuzziness) {
this.fuzziness = (fuzziness == null) ? DEFAULT_FUZZINESS : fuzziness;
/** Sets the fuzziness used when evaluated to a fuzzy query type. Defaults to "AUTO". */
public FuzzyQueryBuilder fuzziness(Object fuzziness) {
this.fuzziness = (fuzziness == null) ? DEFAULT_FUZZINESS : Fuzziness.build(fuzziness);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public class MatchBoolPrefixQueryBuilder extends AbstractQueryBuilder<MatchBoolP

public static final String NAME = "match_bool_prefix";

/** Default maximum edit distance. Defaults to AUTO. */
public static final Fuzziness DEFAULT_FUZZINESS = Fuzziness.AUTO;

private static final Operator DEFAULT_OPERATOR = Operator.OR;

private final String fieldName;
Expand Down Expand Up @@ -177,7 +180,7 @@ public String minimumShouldMatch() {

/** Sets the fuzziness used when evaluated to a fuzzy query type. Defaults to "AUTO". */
public MatchBoolPrefixQueryBuilder fuzziness(Object fuzziness) {
this.fuzziness = Fuzziness.build(fuzziness);
this.fuzziness = (fuzziness == null) ? DEFAULT_FUZZINESS : Fuzziness.build(fuzziness);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ public class MatchQueryBuilder extends AbstractQueryBuilder<MatchQueryBuilder> {
public static final ParseField QUERY_FIELD = new ParseField("query");
public static final ParseField GENERATE_SYNONYMS_PHRASE_QUERY = new ParseField("auto_generate_synonyms_phrase_query");

/** Default maximum edit distance. Defaults to AUTO. */
public static final Fuzziness DEFAULT_FUZZINESS = Fuzziness.AUTO;

/** The name for the match query */
public static final String NAME = "match";

Expand Down Expand Up @@ -210,7 +213,7 @@ public String analyzer() {

/** Sets the fuzziness used when evaluated to a fuzzy query type. Defaults to "AUTO". */
public MatchQueryBuilder fuzziness(Object fuzziness) {
this.fuzziness = Fuzziness.build(fuzziness);
this.fuzziness = (fuzziness == null) ? DEFAULT_FUZZINESS : Fuzziness.build(fuzziness);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public class MultiMatchQueryBuilder extends AbstractQueryBuilder<MultiMatchQuery

public static final MultiMatchQueryBuilder.Type DEFAULT_TYPE = MultiMatchQueryBuilder.Type.BEST_FIELDS;
public static final Operator DEFAULT_OPERATOR = Operator.OR;

/** Default maximum edit distance. Defaults to AUTO. */
public static final Fuzziness DEFAULT_FUZZINESS = Fuzziness.AUTO;

public static final int DEFAULT_PHRASE_SLOP = MatchQuery.DEFAULT_PHRASE_SLOP;
public static final int DEFAULT_PREFIX_LENGTH = FuzzyQuery.defaultPrefixLength;
public static final int DEFAULT_MAX_EXPANSIONS = FuzzyQuery.defaultMaxExpansions;
Expand Down Expand Up @@ -401,9 +405,7 @@ public int slop() {
* Sets the fuzziness used when evaluated to a fuzzy query type. Defaults to "AUTO".
*/
public MultiMatchQueryBuilder fuzziness(Object fuzziness) {
if (fuzziness != null) {
this.fuzziness = Fuzziness.build(fuzziness);
}
this.fuzziness = (fuzziness == null) ? DEFAULT_FUZZINESS : Fuzziness.build(fuzziness);
return this;
}

Expand Down Expand Up @@ -753,7 +755,6 @@ public static MultiMatchQueryBuilder fromXContent(XContentParser parser) throws
.type(type)
.analyzer(analyzer)
.cutoffFrequency(cutoffFrequency)
.fuzziness(fuzziness)
.fuzzyRewrite(fuzzyRewrite)
.maxExpansions(maxExpansions)
.minimumShouldMatch(minimumShouldMatch)
Expand All @@ -766,6 +767,9 @@ public static MultiMatchQueryBuilder fromXContent(XContentParser parser) throws
.boost(boost)
.queryName(queryName)
.fuzzyTranspositions(fuzzyTranspositions);
if (fuzziness != null) {
builder.fuzziness(fuzziness);
}
if (lenient != null) {
builder.lenient(lenient);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public class QueryStringQueryBuilder extends AbstractQueryBuilder<QueryStringQue
public static final int DEFAULT_FUZZY_PREFIX_LENGTH = FuzzyQuery.defaultPrefixLength;
public static final int DEFAULT_FUZZY_MAX_EXPANSIONS = FuzzyQuery.defaultMaxExpansions;
public static final int DEFAULT_PHRASE_SLOP = 0;

/** Default maximum edit distance. Defaults to AUTO. */
public static final Fuzziness DEFAULT_FUZZINESS = Fuzziness.AUTO;
public static final Operator DEFAULT_OPERATOR = Operator.OR;
public static final MultiMatchQueryBuilder.Type DEFAULT_TYPE = MultiMatchQueryBuilder.Type.BEST_FIELDS;
Expand Down Expand Up @@ -415,8 +417,8 @@ public boolean enablePositionIncrements() {
/**
* Set the edit distance for fuzzy queries. Default is "AUTO".
*/
public QueryStringQueryBuilder fuzziness(Fuzziness fuzziness) {
this.fuzziness = fuzziness == null ? DEFAULT_FUZZINESS : fuzziness;
public QueryStringQueryBuilder fuzziness(Object fuzziness) {
this.fuzziness = (fuzziness == null) ? DEFAULT_FUZZINESS : Fuzziness.build(fuzziness);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ public void testUnsupportedFuzzinessForStringType() throws IOException {
assertThat(e.getMessage(), containsString("For input string"));
}

public void testDefaultFuzziness() {
FuzzyQueryBuilder fuzzyQueryBuilder = new FuzzyQueryBuilder(TEXT_FIELD_NAME, "text").fuzziness(null);
assertEquals(FuzzyQueryBuilder.DEFAULT_FUZZINESS, fuzzyQueryBuilder.fuzziness());
}

public void testToQueryWithStringField() throws IOException {
String query = "{\n"
+ " \"fuzzy\":{\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ public void testIllegalValues() {
}
}

public void testDefaultFuzziness() {
MatchBoolPrefixQueryBuilder matchBoolPrefixQueryBuilder = new MatchBoolPrefixQueryBuilder(TEXT_FIELD_NAME, "text").fuzziness(null);
assertEquals(MatchBoolPrefixQueryBuilder.DEFAULT_FUZZINESS, matchBoolPrefixQueryBuilder.fuzziness());
}

public void testFromSimpleJson() throws IOException {
final String simple = "{" + "\"match_bool_prefix\": {" + "\"fieldName\": \"fieldValue\"" + "}" + "}";
final String expected = "{"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,11 @@ public void testFuzzinessOnNonStringField() throws Exception {
query.toQuery(context); // no exception
}

public void testDefaultFuzziness() {
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("text", TEXT_FIELD_NAME).fuzziness(null);
assertEquals(MatchQueryBuilder.DEFAULT_FUZZINESS, matchQueryBuilder.fuzziness());
}

public void testExactOnUnsupportedField() throws Exception {
MatchQueryBuilder query = new MatchQueryBuilder(GEO_POINT_FIELD_NAME, "2,3");
QueryShardContext context = createShardContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,11 @@ public void testFuzzinessNotAllowedTypes() throws IOException {
}
}

public void testDefaultFuzziness() {
MultiMatchQueryBuilder multiMatchQueryBuilder = new MultiMatchQueryBuilder("text", TEXT_FIELD_NAME).fuzziness(null);
assertEquals(MultiMatchQueryBuilder.DEFAULT_FUZZINESS, multiMatchQueryBuilder.fuzziness());
}

public void testQueryParameterArrayException() {
String json = "{\n"
+ " \"multi_match\" : {\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ public void testToQueryWildcardWithIndexedPrefixes() throws Exception {
assertThat(query, equalTo(expectedQuery));
}

public void testToQueryWilcardQueryWithSynonyms() throws Exception {
public void testToQueryWildcardQueryWithSynonyms() throws Exception {
for (Operator op : Operator.values()) {
BooleanClause.Occur defaultOp = op.toBooleanClauseOccur();
QueryStringQueryParser queryParser = new QueryStringQueryParser(createShardContext(), TEXT_FIELD_NAME);
Expand Down Expand Up @@ -803,7 +803,7 @@ public void testToQueryRegExpQueryMaxDeterminizedStatesParsing() throws Exceptio
assertThat(e.getMessage(), containsString("would require more than 10 effort"));
}

public void testToQueryFuzzyQueryAutoFuziness() throws Exception {
public void testToQueryFuzzyQueryAutoFuzziness() throws Exception {
for (int i = 0; i < 3; i++) {
final int len;
final int expectedEdits;
Expand Down Expand Up @@ -868,6 +868,11 @@ public void testFuzzyNumeric() throws Exception {
query.toQuery(context); // no exception
}

public void testDefaultFuzziness() {
QueryStringQueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder(TEXT_FIELD_NAME).fuzziness(null);
assertEquals(QueryStringQueryBuilder.DEFAULT_FUZZINESS, queryStringQueryBuilder.fuzziness());
}

public void testPrefixNumeric() throws Exception {
QueryStringQueryBuilder query = queryStringQuery("12*").defaultField(INT_FIELD_NAME);
QueryShardContext context = createShardContext();
Expand Down

0 comments on commit 19c050f

Please sign in to comment.