Skip to content

Commit

Permalink
Adding query fuzziness validation (opensearch-project#5805)
Browse files Browse the repository at this point in the history
* Adding fuzziness validation

Signed-off-by: Sean Li <[email protected]>

* Updating imports

Signed-off-by: Sean Li <[email protected]>

* Adding fuzziness validation

Signed-off-by: Sean Li <[email protected]>

* Updating imports

Signed-off-by: Sean Li <[email protected]>

* removing comments

Signed-off-by: Sean Li <[email protected]>

* running spotlessApply

Signed-off-by: Sean Li <[email protected]>

* updating CHANGELOG.md

Signed-off-by: Sean Li <[email protected]>

* resolving reviews

Signed-off-by: Sean Li <[email protected]>

* revising comment

Signed-off-by: Sean Li <[email protected]>

* updating CHANGELOG.md

Signed-off-by: Sean Li <[email protected]>

* moving fix to 2.x in CHANGELOG.md

Signed-off-by: Sean Li <[email protected]>

---------

Signed-off-by: Sean Li <[email protected]>
  • Loading branch information
sejli authored Mar 8, 2023
1 parent f79b4dc commit add1871
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Fixed
- Added equals/hashcode for named DocValueFormat.DateTime inner class ([#6357](https://github.com/opensearch-project/OpenSearch/pull/6357))
- Fixed bug for searchable snapshot to take 'base_path' of blob into account ([#6558](https://github.com/opensearch-project/OpenSearch/pull/6558))
- Fix fuzziness validation ([#5805](https://github.com/opensearch-project/OpenSearch/pull/5805))

### Security

Expand Down
14 changes: 10 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,13 @@ public static Fuzziness parse(XContentParser parser) throws IOException {
return build(fuzziness);
}
} catch (NumberFormatException ex) {
return build(fuzziness);
// Validate if the fuzziness value is formatted correctly as a numeric value.
try {
final float minimumSimilarity = Float.parseFloat(fuzziness);
return build(fuzziness);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid fuzziness value: " + fuzziness);
}
}

default:
Expand Down Expand Up @@ -225,7 +231,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 +240,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;
}

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.core.xcontent.XContentBuilder;
Expand Down Expand Up @@ -138,6 +139,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));
assertTrue(e.getMessage().startsWith("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));
assertTrue(e.getMessage().startsWith("failed to find low and high distance values"));
}
}

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

0 comments on commit add1871

Please sign in to comment.