From a323091dac0a892f33fb589055d1c39e68650d26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20H=C3=B8ydahl?= Date: Wed, 12 Nov 2014 08:25:28 +0000 Subject: [PATCH] SOLR-6085: Suggester crashes when prefixToken is longer than surface form git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1638711 13f79535-47bb-0310-9956-ffa450edef68 --- .../analyzing/AnalyzingInfixSuggester.java | 8 +++--- .../AnalyzingInfixSuggesterTest.java | 26 +++++++++++++++++++ solr/CHANGES.txt | 2 ++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/lucene/suggest/src/java/org/apache/lucene/search/suggest/analyzing/AnalyzingInfixSuggester.java b/lucene/suggest/src/java/org/apache/lucene/search/suggest/analyzing/AnalyzingInfixSuggester.java index e0684a208c2..de59d7dcea7 100644 --- a/lucene/suggest/src/java/org/apache/lucene/search/suggest/analyzing/AnalyzingInfixSuggester.java +++ b/lucene/suggest/src/java/org/apache/lucene/search/suggest/analyzing/AnalyzingInfixSuggester.java @@ -695,12 +695,14 @@ public class AnalyzingInfixSuggester extends Lookup implements Closeable { protected void addPrefixMatch(StringBuilder sb, String surface, String analyzed, String prefixToken) { // TODO: apps can try to invert their analysis logic // here, e.g. downcase the two before checking prefix: + if (prefixToken.length() >= surface.length()) { + addWholeMatch(sb, surface, analyzed); + return; + } sb.append(""); sb.append(surface.substring(0, prefixToken.length())); sb.append(""); - if (prefixToken.length() < surface.length()) { - sb.append(surface.substring(prefixToken.length())); - } + sb.append(surface.substring(prefixToken.length())); } @Override diff --git a/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/AnalyzingInfixSuggesterTest.java b/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/AnalyzingInfixSuggesterTest.java index 951f20159e5..0d75e290920 100644 --- a/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/AnalyzingInfixSuggesterTest.java +++ b/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/AnalyzingInfixSuggesterTest.java @@ -41,9 +41,11 @@ import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.suggest.Input; import org.apache.lucene.search.suggest.InputArrayIterator; import org.apache.lucene.search.suggest.Lookup.LookupResult; +import org.apache.lucene.store.Directory; import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.TestUtil; +import org.junit.Test; public class AnalyzingInfixSuggesterTest extends LuceneTestCase { @@ -1076,4 +1078,28 @@ public class AnalyzingInfixSuggesterTest extends LuceneTestCase { suggester.close(); } } + + @Test + public void testAddPrefixMatch() throws IOException { + Analyzer a = new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false); + Directory dir = newDirectory(); + AnalyzingInfixSuggester suggester = new AnalyzingInfixSuggester(dir, a); + + assertEquals("Solr", pfmToString(suggester, "Solr", "Sol")); + assertEquals("Solr", pfmToString(suggester, "Solr", "Solr")); + + // Test SOLR-6085 - the analyzed tokens match due to ss->ß normalization + assertEquals("daß", pfmToString(suggester, "daß", "dass")); + + dir.close(); + suggester.close(); + } + + private String pfmToString(AnalyzingInfixSuggester suggester, String surface, String prefix) throws IOException { + StringBuilder sb = new StringBuilder(); + suggester.addPrefixMatch(sb, surface, "", prefix); + return sb.toString(); + } + + } diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index 86a20f2d57e..55fda2089e8 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -400,6 +400,8 @@ Bug Fixes * SOLR-6704: TrieDateField type drops schema properties in branch 4.10 (Tomás Fernández Löbbe) +* SOLR-6085: Suggester crashes when prefixToken is longer than surface form (janhoy) + ================== 4.10.2 ================== Bug Fixes