Fix Fuzziness#asDistance(String) (#39643)
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
This commit is contained in:
parent
19f6a35358
commit
5cdea6ef17
|
@ -186,7 +186,7 @@ public final class Fuzziness implements ToXContentFragment, Writeable {
|
|||
}
|
||||
|
||||
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;
|
||||
|
|
|
@ -169,4 +169,28 @@ public class FuzzinessTests extends ESTestCase {
|
|||
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"));
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -664,6 +664,32 @@ public class SearchQueryIT extends ESIntegTestCase {
|
|||
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");
|
||||
|
||||
|
|
Loading…
Reference in New Issue