diff --git a/CHANGES.txt b/CHANGES.txt index 16cccfbc3eb..19ab4878229 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -528,6 +528,8 @@ Bug Fixes 47. SOLR-669: snappuler fix for FreeBSD/Darwin (Richard "Trey" Hyde via Otis Gospodnetic) +48. SOLR-606: Fixed spell check collation offset issue. (Stefan Oestreicher , Geoffrey Young, gsingers) + Other Changes 1. SOLR-135: Moved common classes to org.apache.solr.common and altered the build scripts to make two jars: apache-solr-1.3.jar and diff --git a/src/java/org/apache/solr/handler/component/SpellCheckComponent.java b/src/java/org/apache/solr/handler/component/SpellCheckComponent.java index 23f4ce3f7fd..c977554172a 100644 --- a/src/java/org/apache/solr/handler/component/SpellCheckComponent.java +++ b/src/java/org/apache/solr/handler/component/SpellCheckComponent.java @@ -190,7 +190,7 @@ public class SpellCheckComponent extends SearchComponent implements SolrCoreAwar boolean isCorrectlySpelled = true; Map best = null; if (collate == true){ - best = new HashMap(suggestions.size()); + best = new LinkedHashMap(suggestions.size()); } for (Map.Entry> entry : suggestions.entrySet()) { Token inputToken = entry.getKey(); @@ -225,10 +225,13 @@ public class SpellCheckComponent extends SearchComponent implements SolrCoreAwar } if (collate == true){ StringBuilder collation = new StringBuilder(origQuery); + int offset = 0; for (Iterator> bestIter = best.entrySet().iterator(); bestIter.hasNext();) { Map.Entry entry = bestIter.next(); Token tok = entry.getKey(); - collation.replace(tok.startOffset(), tok.endOffset(), entry.getValue()); + collation.replace(tok.startOffset() + offset, + tok.endOffset() + offset, entry.getValue()); + offset += entry.getValue().length() - (tok.endOffset() - tok.startOffset()); } String collVal = collation.toString(); if (collVal.equals(origQuery) == false) { diff --git a/src/test/org/apache/solr/handler/component/SpellCheckComponentTest.java b/src/test/org/apache/solr/handler/component/SpellCheckComponentTest.java index 0b11a4e3e19..64d1bce1cf8 100644 --- a/src/test/org/apache/solr/handler/component/SpellCheckComponentTest.java +++ b/src/test/org/apache/solr/handler/component/SpellCheckComponentTest.java @@ -199,6 +199,7 @@ public class SpellCheckComponentTest extends AbstractSolrTestCase { SolrRequestHandler handler = core.getRequestHandler("spellCheckCompRH"); SolrQueryResponse rsp = new SolrQueryResponse(); + rsp.add("responseHeader", new SimpleOrderedMap()); handler.handleRequest(new LocalSolrQueryRequest(core, params), rsp); NamedList values = rsp.getValues(); NamedList spellCheck = (NamedList) values.get("spellcheck"); @@ -212,6 +213,7 @@ public class SpellCheckComponentTest extends AbstractSolrTestCase { params.add(CommonParams.Q, "documemt lowerfilt:broen^4"); handler = core.getRequestHandler("spellCheckCompRH"); rsp = new SolrQueryResponse(); + rsp.add("responseHeader", new SimpleOrderedMap()); handler.handleRequest(new LocalSolrQueryRequest(core, params), rsp); values = rsp.getValues(); spellCheck = (NamedList) values.get("spellcheck"); @@ -222,6 +224,23 @@ public class SpellCheckComponentTest extends AbstractSolrTestCase { assertTrue("collation is null and it shouldn't be", collation != null); assertTrue(collation + " is not equal to " + "document lowerfilt:brown^4", collation.equals("document lowerfilt:brown^4") == true); + params.remove(CommonParams.Q); + params.add(CommonParams.Q, "documemtsss broens"); + handler = core.getRequestHandler("spellCheckCompRH"); + rsp = new SolrQueryResponse(); + rsp.add("responseHeader", new SimpleOrderedMap()); + handler.handleRequest(new LocalSolrQueryRequest(core, params), rsp); + values = rsp.getValues(); + spellCheck = (NamedList) values.get("spellcheck"); + assertTrue("spellCheck is null and it shouldn't be", spellCheck != null); + suggestions = (NamedList) spellCheck.get("suggestions"); + assertTrue("suggestions is null and it shouldn't be", suggestions != null); + collation = (String) suggestions.get("collation"); + assertTrue("collation is null and it shouldn't be", collation != null); + System.out.println("Collation: " + collation); + assertTrue(collation + " is not equal to " + "document brown", collation.equals("document brown") == true); + + } public void testCorrectSpelling() throws Exception {