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 3, 2022
1 parent 0210b76 commit 7145fce
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 22 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 @@ -177,7 +177,9 @@ 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);
if (fuzziness != null) {
this.fuzziness = Fuzziness.build(fuzziness);
}
return this;
}

Expand Down Expand Up @@ -354,9 +356,7 @@ public static MatchBoolPrefixQueryBuilder fromXContent(XContentParser parser) th
queryBuilder.minimumShouldMatch(minimumShouldMatch);
queryBuilder.boost(boost);
queryBuilder.queryName(queryName);
if (fuzziness != null) {
queryBuilder.fuzziness(fuzziness);
}
queryBuilder.fuzziness(fuzziness);
queryBuilder.prefixLength(prefixLength);
queryBuilder.maxExpansions(maxExpansion);
queryBuilder.fuzzyTranspositions(fuzzyTranspositions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ 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);
if (fuzziness != null) {
this.fuzziness = Fuzziness.build(fuzziness);
}
return this;
}

Expand Down Expand Up @@ -565,9 +567,7 @@ public static MatchQueryBuilder fromXContent(XContentParser parser) throws IOExc
matchQuery.operator(operator);
matchQuery.analyzer(analyzer);
matchQuery.minimumShouldMatch(minimumShouldMatch);
if (fuzziness != null) {
matchQuery.fuzziness(fuzziness);
}
matchQuery.fuzziness(fuzziness);
matchQuery.fuzzyRewrite(fuzzyRewrite);
matchQuery.prefixLength(prefixLength);
matchQuery.fuzzyTranspositions(fuzzyTranspositions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ 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;

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
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ 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;
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;
public static final boolean DEFAULT_FUZZY_TRANSPOSITIONS = FuzzyQuery.defaultTranspositions;
Expand Down Expand Up @@ -136,7 +135,7 @@ public class QueryStringQueryBuilder extends AbstractQueryBuilder<QueryStringQue

private boolean enablePositionIncrements = DEFAULT_ENABLE_POSITION_INCREMENTS;

private Fuzziness fuzziness = DEFAULT_FUZZINESS;
private Fuzziness fuzziness = null;

private int fuzzyPrefixLength = DEFAULT_FUZZY_PREFIX_LENGTH;

Expand Down Expand Up @@ -195,7 +194,7 @@ public QueryStringQueryBuilder(StreamInput in) throws IOException {
allowLeadingWildcard = in.readOptionalBoolean();
analyzeWildcard = in.readOptionalBoolean();
enablePositionIncrements = in.readBoolean();
fuzziness = new Fuzziness(in);
fuzziness = in.readOptionalWriteable(Fuzziness::new);
fuzzyPrefixLength = in.readVInt();
fuzzyMaxExpansions = in.readVInt();
fuzzyRewrite = in.readOptionalString();
Expand Down Expand Up @@ -229,7 +228,7 @@ protected void doWriteTo(StreamOutput out) throws IOException {
out.writeOptionalBoolean(this.allowLeadingWildcard);
out.writeOptionalBoolean(this.analyzeWildcard);
out.writeBoolean(this.enablePositionIncrements);
this.fuzziness.writeTo(out);
out.writeOptionalWriteable(fuzziness);
out.writeVInt(this.fuzzyPrefixLength);
out.writeVInt(this.fuzzyMaxExpansions);
out.writeOptionalString(this.fuzzyRewrite);
Expand Down Expand Up @@ -415,8 +414,10 @@ 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) {
if (fuzziness != null) {
this.fuzziness = Fuzziness.build(fuzziness);
}
return this;
}

Expand Down Expand Up @@ -614,7 +615,9 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
builder.field(ALLOW_LEADING_WILDCARD_FIELD.getPreferredName(), this.allowLeadingWildcard);
}
builder.field(ENABLE_POSITION_INCREMENTS_FIELD.getPreferredName(), this.enablePositionIncrements);
this.fuzziness.toXContent(builder, params);
if (fuzziness != null) {
this.fuzziness.toXContent(builder, params);
}
builder.field(FUZZY_PREFIX_LENGTH_FIELD.getPreferredName(), this.fuzzyPrefixLength);
builder.field(FUZZY_MAX_EXPANSIONS_FIELD.getPreferredName(), this.fuzzyMaxExpansions);
if (this.fuzzyRewrite != null) {
Expand Down Expand Up @@ -670,7 +673,7 @@ public static QueryStringQueryBuilder fromXContent(XContentParser parser) throws
Boolean lenient = null;
Operator defaultOperator = QueryStringQueryBuilder.DEFAULT_OPERATOR;
String timeZone = null;
Fuzziness fuzziness = QueryStringQueryBuilder.DEFAULT_FUZZINESS;
Fuzziness fuzziness = null;
String fuzzyRewrite = null;
String rewrite = null;
Map<String, Float> fieldsAndWeights = null;
Expand Down Expand Up @@ -936,7 +939,9 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
);
queryParser.setAnalyzeWildcard(analyzeWildcard == null ? context.queryStringAnalyzeWildcard() : analyzeWildcard);
queryParser.setEnablePositionIncrements(enablePositionIncrements);
queryParser.setFuzziness(fuzziness);
if (fuzziness != null) {
queryParser.setFuzziness(fuzziness);
}
queryParser.setFuzzyPrefixLength(fuzzyPrefixLength);
queryParser.setFuzzyMaxExpansions(fuzzyMaxExpansions);
queryParser.setFuzzyRewriteMethod(QueryParsers.parseRewriteMethod(this.fuzzyRewrite, LoggingDeprecationHandler.INSTANCE));
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);
assertNull(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);
assertNull(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);
assertNull(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 All @@ -828,7 +828,7 @@ public void testToQueryFuzzyQueryAutoFuziness() throws Exception {
String queryString = new String(bytes);
for (int j = 0; j < 2; j++) {
Query query = queryStringQuery(queryString + (j == 0 ? "~" : "~auto")).defaultField(TEXT_FIELD_NAME)
.fuzziness(Fuzziness.AUTO)
// .fuzziness(Fuzziness.AUTO)
.toQuery(createShardContext());
assertThat(query, instanceOf(FuzzyQuery.class));
FuzzyQuery fuzzyQuery = (FuzzyQuery) query;
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);
assertNull(queryStringQueryBuilder.fuzziness());
}

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

0 comments on commit 7145fce

Please sign in to comment.