Apache Lucene open-source search software
SOLR-4076: fuzzy support for MultiTermAwareComponent in SolrQueryParser
Yonik Seeley102b0b8f61c2
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex c70b648ebd3..cc140442dc3 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -194,6 +194,12 @@ Bug Fixesthat start with /zookeeper, as this can fail and stop the removal offurther nodes. (Mark Miller)+* SOLR-4076: SolrQueryParser should run fuzzy terms through+ MultiTermAwareComponents to ensure that (for example) a fuzzy query of+ foobar~2 is equivalent to FooBar~2 on a field that includes lowercasing.+ (yonik)++Other Changes----------------------diff --git a/solr/core/src/java/org/apache/solr/search/SolrQueryParser.java b/solr/core/src/java/org/apache/solr/search/SolrQueryParser.javaindex eb32a3a840d..d8de2f694ea 100644--- a/solr/core/src/java/org/apache/solr/search/SolrQueryParser.java+++ b/solr/core/src/java/org/apache/solr/search/SolrQueryParser.java@@ -246,4 +246,12 @@ public class SolrQueryParser extends QueryParser {termStr = analyzeIfMultitermTermText(field, termStr, schema.getFieldType(field));return newRegexpQuery(new Term(field, termStr));}++ @Override+ protected Query getFuzzyQuery(String field, String termStr, float minSimilarity) throws ParseException {+ termStr = analyzeIfMultitermTermText(field, termStr, schema.getFieldType(field));+ Term t = new Term(field, termStr);+ return newFuzzyQuery(t, minSimilarity, getFuzzyPrefixLength());+ }+}diff --git a/solr/core/src/test/org/apache/solr/search/TestFoldingMultitermQuery.java b/solr/core/src/test/org/apache/solr/search/TestFoldingMultitermQuery.javaindex ddcb235a76c..db800fc7023 100644--- a/solr/core/src/test/org/apache/solr/search/TestFoldingMultitermQuery.java+++ b/solr/core/src/test/org/apache/solr/search/TestFoldingMultitermQuery.java@@ -185,26 +185,37 @@ public class TestFoldingMultitermQuery extends SolrTestCaseJ4 {assertQ(req("q", "content_lower_token:hȉ*l?*"), "//result[@numFound='1']");}+ @Test+ public void testFuzzy() throws Exception {+ assertQ(req("q", "content:ZiLLx~1"),+ "//result[@numFound='1']");+ assertQ(req("q", "content_straight:ZiLLx~1"), // case preserving field shouldn't match+ "//result[@numFound='0']");+ assertQ(req("q", "content_folding:ZiLLx~1"), // case preserving field shouldn't match+ "//result[@numFound='0']");+ }+@Testpublic void testRegex() throws Exception {assertQ(req("q", "content:/Zill[a-z]/"),- "//result[@numFound='1']");+ "//result[@numFound='1']");assertQ(req("q", "content:/Zill[A-Z]/"), // everything in the regex gets lowercased?- "//result[@numFound='1']");+ "//result[@numFound='1']");assertQ(req("q", "content_keyword:/.*Zill[A-Z]/"),- "//result[@numFound='1']");+ "//result[@numFound='1']");assertQ(req("q", "content_straight:/Zill[a-z]/"), // case preserving field shouldn't match- "//result[@numFound='0']");+ "//result[@numFound='0']");assertQ(req("q", "content_folding:/Zill[a-z]/"), // case preserving field shouldn't match- "//result[@numFound='0']");+ "//result[@numFound='0']");assertQ(req("q", "content_keyword:/Abcdefg1 Finger/"), // test spaces- "//result[@numFound='1']");+ "//result[@numFound='1']");}+@Testpublic void testGeneral() throws Exception {assertQ(req("q", "content_stemming:fings*"), "//result[@numFound='0']"); // should not match (but would if fings* was stemmed to fing*