Skip to content

Commit

Permalink
Fix Fuzziness#asDistance(String) (#39643)
Browse files Browse the repository at this point in the history
Currently Fuzziness#asDistance(String) doesn't work for custom AUTO values. If
the fuzziness is AUTO, the method returns the correct edit distance to use,
depending on the input string, but for custom AUTO values it currently always
returns an edit distance of 1. Correcting this and adding unit and integration
tests to catch these cases.

Closes #39614
  • Loading branch information
Christoph Büscher committed Mar 5, 2019
1 parent be10934 commit 49ff384
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public int asDistance() {
}

public int asDistance(String text) {
if (this.equals(AUTO)) { //AUTO
if (this.equals(AUTO) || isAutoWithCustomValues()) { //AUTO
final int len = termLen(text);
if (len < lowDistance) {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,28 @@ private static Fuzziness doSerializeRoundtrip(Fuzziness in) throws IOException {
StreamInput streamInput = output.bytes().streamInput();
return new Fuzziness(streamInput);
}

public void testAsDistanceString() {
Fuzziness fuzziness = Fuzziness.build("0");
assertEquals(0, fuzziness.asDistance(randomAlphaOfLengthBetween(0, 10)));
fuzziness = Fuzziness.build("1");
assertEquals(1, fuzziness.asDistance(randomAlphaOfLengthBetween(0, 10)));
fuzziness = Fuzziness.build("2");
assertEquals(2, fuzziness.asDistance(randomAlphaOfLengthBetween(0, 10)));

fuzziness = Fuzziness.build("AUTO");
assertEquals(0, fuzziness.asDistance(""));
assertEquals(0, fuzziness.asDistance("ab"));
assertEquals(1, fuzziness.asDistance("abc"));
assertEquals(1, fuzziness.asDistance("abcde"));
assertEquals(2, fuzziness.asDistance("abcdef"));

fuzziness = Fuzziness.build("AUTO:5,7");
assertEquals(0, fuzziness.asDistance(""));
assertEquals(0, fuzziness.asDistance("abcd"));
assertEquals(1, fuzziness.asDistance("abcde"));
assertEquals(1, fuzziness.asDistance("abcdef"));
assertEquals(2, fuzziness.asDistance("abcdefg"));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,32 @@ public void testMatchQueryNumeric() throws Exception {
expectThrows(SearchPhaseExecutionException.class, () -> client().prepareSearch().setQuery(matchQuery("double", "2 3 4")).get());
}

public void testMatchQueryFuzzy() throws Exception {
assertAcked(prepareCreate("test").addMapping("_doc", "text", "type=text"));

indexRandom(true, client().prepareIndex("test", "_doc", "1").setSource("text", "Unit"),
client().prepareIndex("test", "_doc", "2").setSource("text", "Unity"));

SearchResponse searchResponse = client().prepareSearch().setQuery(matchQuery("text", "uniy").fuzziness("0")).get();
assertHitCount(searchResponse, 0L);

searchResponse = client().prepareSearch().setQuery(matchQuery("text", "uniy").fuzziness("1")).get();
assertHitCount(searchResponse, 2L);
assertSearchHits(searchResponse, "1", "2");

searchResponse = client().prepareSearch().setQuery(matchQuery("text", "uniy").fuzziness("AUTO")).get();
assertHitCount(searchResponse, 2L);
assertSearchHits(searchResponse, "1", "2");

searchResponse = client().prepareSearch().setQuery(matchQuery("text", "uniy").fuzziness("AUTO:5,7")).get();
assertHitCount(searchResponse, 0L);

searchResponse = client().prepareSearch().setQuery(matchQuery("text", "unify").fuzziness("AUTO:5,7")).get();
assertHitCount(searchResponse, 1L);
assertSearchHits(searchResponse, "2");
}


public void testMultiMatchQuery() throws Exception {
createIndex("test");

Expand Down

0 comments on commit 49ff384

Please sign in to comment.