Skip to content

Apache Lucene open-source search software

SOLR-7304: SpellingQueryConverter to ignore "TO" as a possible range query operator

James Dyer933a753aca04
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index f86501599dc..6a0fb87a37d 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -207,8 +207,10 @@ Bug Fixes
* SOLR-8366: ConcurrentUpdateSolrClient attempts to use response's content type as charset encoding
for parsing exception. (shalin)
-* SOLR-6271: fix ConjunctionSolrSpellChecker to not compare StringDistance by instance.
+* SOLR-6271: Fix ConjunctionSolrSpellChecker to not compare StringDistance by instance.
(Igor Kostromin via James Dyer)
+
+* SOLR-7304: Fix Spellcheck Collate to not invalidate range queries. (James Dyer)
Other Changes
----------------------
diff --git a/solr/core/src/java/org/apache/solr/spelling/SpellingQueryConverter.java b/solr/core/src/java/org/apache/solr/spelling/SpellingQueryConverter.java
index 4b8a3942e3b..5159b01edc4 100644
--- a/solr/core/src/java/org/apache/solr/spelling/SpellingQueryConverter.java
+++ b/solr/core/src/java/org/apache/solr/spelling/SpellingQueryConverter.java
@@ -104,6 +104,8 @@ public class SpellingQueryConverter extends QueryConverter {
if (original == null) { // this can happen with q.alt = and no query
return Collections.emptyList();
}
+ boolean mightContainRangeQuery = (original.indexOf('[') != -1 || original.indexOf('{') != -1)
+ && (original.indexOf(']') != -1 || original.indexOf('}') != -1);
Collection<Token> result = new ArrayList<>();
Matcher matcher = QUERY_REGEX.matcher(original);
String nextWord = null;
@@ -123,7 +125,10 @@ public class SpellingQueryConverter extends QueryConverter {
if(matcher.find()) {
nextWord = matcher.group(0);
nextStartIndex = matcher.start();
- }
+ }
+ if(mightContainRangeQuery && "TO".equals(word)) {
+ continue;
+ }
if("AND".equals(word) || "OR".equals(word) || "NOT".equals(word)) {
lastBooleanOp = word;
continue;
diff --git a/solr/core/src/test/org/apache/solr/spelling/SpellCheckCollatorTest.java b/solr/core/src/test/org/apache/solr/spelling/SpellCheckCollatorTest.java
index 4df5b827f5d..d1a4c4442d8 100644
--- a/solr/core/src/test/org/apache/solr/spelling/SpellCheckCollatorTest.java
+++ b/solr/core/src/test/org/apache/solr/spelling/SpellCheckCollatorTest.java
@@ -48,7 +48,7 @@ public class SpellCheckCollatorTest extends SolrTestCaseJ4 {
public static void beforeClass() throws Exception {
initCore("solrconfig-spellcheckcomponent.xml", "schema.xml");
assertU(adoc("id", "0",
- "lowerfilt", "faith hope and love",
+ "lowerfilt", "faith hope and love to",
"teststop", "metanoia"));
assertU(adoc("id", "1",
"lowerfilt", "faith hope and loaves",
@@ -94,6 +94,38 @@ public class SpellCheckCollatorTest extends SolrTestCaseJ4 {
"teststop", "metanoia"));
assertU(commit());
}
+
+ @Test
+ public void testCollationWithRangeQuery() throws Exception
+ {
+ SolrCore core = h.getCore();
+ SearchComponent speller = core.getSearchComponent("spellcheck");
+ assertTrue("speller is null and it shouldn't be", speller != null);
+
+ ModifiableSolrParams params = new ModifiableSolrParams();
+ params.add(SpellCheckComponent.COMPONENT_NAME, "true");
+ params.add(SpellingParams.SPELLCHECK_BUILD, "true");
+ params.add(SpellingParams.SPELLCHECK_COUNT, "10");
+ params.add(SpellingParams.SPELLCHECK_COLLATE, "true");
+ params.add(SpellingParams.SPELLCHECK_ALTERNATIVE_TERM_COUNT, "10");
+ params.add(CommonParams.Q, "id:[1 TO 10] AND lowerfilt:lovw");
+ {
+ SolrRequestHandler handler = core.getRequestHandler("spellCheckCompRH");
+ SolrQueryResponse rsp = new SolrQueryResponse();
+ rsp.add("responseHeader", new SimpleOrderedMap());
+ SolrQueryRequest req = new LocalSolrQueryRequest(core, params);
+ handler.handleRequest(req, rsp);
+ req.close();
+ NamedList values = rsp.getValues();
+ NamedList spellCheck = (NamedList) values.get("spellcheck");
+ NamedList collationHolder = (NamedList) spellCheck.get("collations");
+ List<String> collations = collationHolder.getAll("collation");
+ assertTrue(collations.size()==1);
+ String collation = collations.iterator().next();
+ System.out.println(collation);
+ assertTrue("Incorrect collation: " + collation,"id:[1 TO 10] AND lowerfilt:love".equals(collation));
+ }
+ }
@Test
public void testCollationWithHypens() throws Exception