diff --git a/lucene/suggest/src/java/org/apache/lucene/search/spell/LuceneLevenshteinDistance.java b/lucene/suggest/src/java/org/apache/lucene/search/spell/LuceneLevenshteinDistance.java index 3ba0bd332be..21ca535c1e5 100644 --- a/lucene/suggest/src/java/org/apache/lucene/search/spell/LuceneLevenshteinDistance.java +++ b/lucene/suggest/src/java/org/apache/lucene/search/spell/LuceneLevenshteinDistance.java @@ -114,4 +114,12 @@ public final class LuceneLevenshteinDistance implements StringDistance { } return ref; } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (null == obj) return false; + return (getClass() == obj.getClass()); + } + } diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index 999d2f65707..399a2a7d44e 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -154,6 +154,8 @@ Bug Fixes (Rohit, Varun Thacker) * SOLR-12150: Fix a test bug in CdcrBidirectionalTest.testBiDir (Steve Rowe, Amrit Sarkar via Varun Thacker) + +* SOLR-10513: ConjunctionSolrSpellChecker did not work with LuceneLevenshteinDistance (Amrit Sarkar via James Dyer) Optimizations ---------------------- diff --git a/solr/core/src/java/org/apache/solr/spelling/ConjunctionSolrSpellChecker.java b/solr/core/src/java/org/apache/solr/spelling/ConjunctionSolrSpellChecker.java index 2daab28d405..f92919d4453 100644 --- a/solr/core/src/java/org/apache/solr/spelling/ConjunctionSolrSpellChecker.java +++ b/solr/core/src/java/org/apache/solr/spelling/ConjunctionSolrSpellChecker.java @@ -81,9 +81,8 @@ public class ConjunctionSolrSpellChecker extends SolrSpellChecker { checkers.add(checker); } - @SuppressWarnings("unchecked") @Override - public String init(NamedList config, SolrCore core) { + public String init(@SuppressWarnings("rawtypes") NamedList config, SolrCore core) { for (int i = 0; i < checkers.size(); i++) { SolrSpellChecker c = checkers.get(i); String dn = c.init(config, core); diff --git a/solr/core/src/test/org/apache/solr/spelling/ConjunctionSolrSpellCheckerTest.java b/solr/core/src/test/org/apache/solr/spelling/ConjunctionSolrSpellCheckerTest.java index 0df837fd49a..8ec933b94d6 100644 --- a/solr/core/src/test/org/apache/solr/spelling/ConjunctionSolrSpellCheckerTest.java +++ b/solr/core/src/test/org/apache/solr/spelling/ConjunctionSolrSpellCheckerTest.java @@ -18,27 +18,50 @@ package org.apache.solr.spelling; import java.io.IOException; +import org.apache.lucene.search.spell.JaroWinklerDistance; import org.apache.lucene.search.spell.LevenshteinDistance; +import org.apache.lucene.search.spell.LuceneLevenshteinDistance; import org.apache.lucene.search.spell.NGramDistance; import org.apache.lucene.search.spell.StringDistance; import org.apache.lucene.util.LuceneTestCase; import org.apache.solr.core.SolrCore; import org.apache.solr.search.SolrIndexSearcher; +import org.junit.Assert; import org.junit.Test; public class ConjunctionSolrSpellCheckerTest extends LuceneTestCase { + public static final Class[] AVAILABLE_DISTANCES = {LevenshteinDistance.class, LuceneLevenshteinDistance.class, + JaroWinklerDistance.class, NGramDistance.class}; + @Test public void test() throws Exception { ConjunctionSolrSpellChecker cssc = new ConjunctionSolrSpellChecker(); - MockSolrSpellChecker levenstein1 = new MockSolrSpellChecker(new LevenshteinDistance()); - MockSolrSpellChecker levenstein2 = new MockSolrSpellChecker(new LevenshteinDistance()); - MockSolrSpellChecker ngram = new MockSolrSpellChecker(new NGramDistance()); + @SuppressWarnings("unchecked") + Class sameDistance = (Class) AVAILABLE_DISTANCES[random().nextInt(AVAILABLE_DISTANCES.length)]; - cssc.addChecker(levenstein1); - cssc.addChecker(levenstein2); + StringDistance sameDistance1 = sameDistance.newInstance(); + StringDistance sameDistance2 = sameDistance.newInstance(); + + //NGramDistance defaults to 2, so we'll try 3 or 4 to ensure we have one that is not-equal. + StringDistance differentDistance = new NGramDistance(3); + if(sameDistance1.equals(differentDistance)) { + differentDistance = new NGramDistance(4); + if(sameDistance1.equals(differentDistance)) { + fail("Cannot set up test. 2 NGramDistances with different gram sizes should not be equal."); + } + } + Assert.assertEquals("The distance " + sameDistance + " does not properly implement equals.", sameDistance1, sameDistance2); + + + MockSolrSpellChecker checker1 = new MockSolrSpellChecker(sameDistance1); + MockSolrSpellChecker checker2 = new MockSolrSpellChecker(sameDistance2); + MockSolrSpellChecker checker3 = new MockSolrSpellChecker(differentDistance); + + cssc.addChecker(checker1); + cssc.addChecker(checker2); try { - cssc.addChecker(ngram); + cssc.addChecker(checker3); fail("ConjunctionSolrSpellChecker should have thrown an exception about non-identical StringDistances."); } catch (IllegalArgumentException iae) { // correct behavior