Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding query fuzziness validation #5805

Merged
merged 14 commits into from
Mar 8, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Increasing timeout of testQuorumRecovery to 90 seconds from 30 ([#5651](https://github.com/opensearch-project/OpenSearch/pull/5651))
- [Segment Replication] Fix for peer recovery ([#5344](https://github.com/opensearch-project/OpenSearch/pull/5344))
- [Test] Renaming PIT tests to IT to fix intermittent test failures ([#5750](https://github.com/opensearch-project/OpenSearch/pull/5750))
- Fix fuzziness validation ([#5805](https://github.com/opensearch-project/OpenSearch/pull/5805))
sejli marked this conversation as resolved.
Show resolved Hide resolved

### Security

Expand Down
13 changes: 9 additions & 4 deletions server/src/main/java/org/opensearch/common/unit/Fuzziness.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,12 @@ public static Fuzziness parse(XContentParser parser) throws IOException {
return build(fuzziness);
}
} catch (NumberFormatException ex) {
return build(fuzziness);
try {
final float minimumSimilarity = Float.parseFloat(fuzziness);
return build(fuzziness);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid fuzziness value: " + fuzziness);
sejli marked this conversation as resolved.
Show resolved Hide resolved
}
}

default:
Expand Down Expand Up @@ -225,7 +230,7 @@ public float asFloat() {
if (this.equals(AUTO) || isAutoWithCustomValues()) {
return 1f;
}
return Float.parseFloat(fuzziness.toString());
return Float.parseFloat(fuzziness);
}

private int termLen(String text) {
Expand All @@ -234,9 +239,9 @@ private int termLen(String text) {

public String asString() {
if (isAutoWithCustomValues()) {
return fuzziness.toString() + ":" + lowDistance + "," + highDistance;
return fuzziness + ":" + lowDistance + "," + highDistance;
}
return fuzziness.toString();
return fuzziness;
sejli marked this conversation as resolved.
Show resolved Hide resolved
}

private boolean isAutoWithCustomValues() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

package org.opensearch.common.unit;

import org.opensearch.OpenSearchParseException;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.xcontent.XContentBuilder;
Expand All @@ -41,6 +42,7 @@

import static org.opensearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.sameInstance;

Expand Down Expand Up @@ -138,6 +140,26 @@ public void testParseFromXContent() throws IOException {

}

public void testFuzzinessValidationWithStrings() throws IOException {
String[] invalidStrings = new String[] { "+++", "asdfghjkl", "2k23" };
XContentBuilder json = jsonBuilder().startObject().field(Fuzziness.X_FIELD_NAME, randomFrom(invalidStrings)).endObject();
try (XContentParser parser = createParser(json)) {
assertThat(parser.nextToken(), equalTo(XContentParser.Token.START_OBJECT));
assertThat(parser.nextToken(), equalTo(XContentParser.Token.FIELD_NAME));
assertThat(parser.nextToken(), equalTo(XContentParser.Token.VALUE_STRING));
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> Fuzziness.parse(parser));
assertThat(e.getMessage(), containsString("Invalid fuzziness value:"));
}
json = jsonBuilder().startObject().field(Fuzziness.X_FIELD_NAME, "AUTO:").endObject();
try (XContentParser parser = createParser(json)) {
assertThat(parser.nextToken(), equalTo(XContentParser.Token.START_OBJECT));
assertThat(parser.nextToken(), equalTo(XContentParser.Token.FIELD_NAME));
assertThat(parser.nextToken(), equalTo(XContentParser.Token.VALUE_STRING));
OpenSearchParseException e = expectThrows(OpenSearchParseException.class, () -> Fuzziness.parse(parser));
assertThat(e.getMessage(), containsString("failed to find low and high distance values"));
sejli marked this conversation as resolved.
Show resolved Hide resolved
}
}

public void testAuto() {
assertThat(Fuzziness.AUTO.asFloat(), equalTo(1f));
}
Expand Down