Apache Solr open-source search software
SOLR-8175: fix AIOOBE w/WordBreakSolrSpellChecker
James Dyerdac0b5658916
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 328c30f8e0b..4d4d131c3d8 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -188,6 +188,12 @@ New Features* SOLR-7928: Improve CheckIndex to work against HdfsDirectory(Mike Drob, Gregory Chanan)+Bug Fixes+----------------------++* SOLR-8175: Word Break Spellchecker would throw AIOOBE with certain queries containing+ "should" clauses. (Ryan Josal via James Dyer)+Other Changes----------------------diff --git a/solr/core/src/java/org/apache/solr/spelling/WordBreakSolrSpellChecker.java b/solr/core/src/java/org/apache/solr/spelling/WordBreakSolrSpellChecker.javaindex 6183296db16..8ec17b57499 100644--- a/solr/core/src/java/org/apache/solr/spelling/WordBreakSolrSpellChecker.java+++ b/solr/core/src/java/org/apache/solr/spelling/WordBreakSolrSpellChecker.java@@ -199,8 +199,8 @@ public class WordBreakSolrSpellChecker extends SolrSpellChecker {StringBuilder sb = new StringBuilder();Token[] tokenArr = options.tokens.toArray(new Token[options.tokens.size()]);+ List<Token> tokenArrWithSeparators = new ArrayList<>(options.tokens.size() + 2);List<Term> termArr = new ArrayList<>(options.tokens.size() + 2);-List<ResultEntry> breakSuggestionList = new ArrayList<>();List<ResultEntry> noBreakSuggestionList = new ArrayList<>();boolean lastOneProhibited = false;@@ -219,6 +219,7 @@ public class WordBreakSolrSpellChecker extends SolrSpellChecker {if (i > 0&& (prohibited != lastOneProhibited || required != lastOneRequired || lastOneprocedesNewBooleanOp)) {termArr.add(WordBreakSpellChecker.SEPARATOR_TERM);+ tokenArrWithSeparators.add(null);}lastOneProhibited = prohibited;lastOneRequired = required;@@ -226,6 +227,7 @@ public class WordBreakSolrSpellChecker extends SolrSpellChecker {Term thisTerm = new Term(field, tokenArr[i].toString());termArr.add(thisTerm);+ tokenArrWithSeparators.add(tokenArr[i]);if (breakWords) {SuggestWord[][] breakSuggestions = wbsp.suggestWordBreaks(thisTerm,numSuggestions, ir, options.suggestMode, sortMethod);@@ -269,10 +271,10 @@ public class WordBreakSolrSpellChecker extends SolrSpellChecker {if (i > firstTermIndex) {sb.append(" ");}- sb.append(tokenArr[i].toString());+ sb.append(tokenArrWithSeparators.get(i).toString());}- Token token = new Token(sb.toString(), tokenArr[firstTermIndex]- .startOffset(), tokenArr[lastTermIndex].endOffset());+ Token token = new Token(sb.toString(), tokenArrWithSeparators.get(firstTermIndex)+ .startOffset(), tokenArrWithSeparators.get(lastTermIndex).endOffset());combineSuggestionList.add(new ResultEntry(token, cs.suggestion.string,cs.suggestion.freq));}diff --git a/solr/core/src/test/org/apache/solr/spelling/WordBreakSolrSpellCheckerTest.java b/solr/core/src/test/org/apache/solr/spelling/WordBreakSolrSpellCheckerTest.javaindex 0d16b8120b5..eeb3185649f 100644--- a/solr/core/src/test/org/apache/solr/spelling/WordBreakSolrSpellCheckerTest.java+++ b/solr/core/src/test/org/apache/solr/spelling/WordBreakSolrSpellCheckerTest.java@@ -70,6 +70,17 @@ public class WordBreakSolrSpellCheckerTest extends SolrTestCaseJ4 {RefCounted<SolrIndexSearcher> searcher = core.getSearcher();QueryConverter qc = new SpellingQueryConverter();qc.setAnalyzer(new MockAnalyzer(random()));++ {+ //Prior to SOLR-8175, the required term would cause an AIOOBE.+ Collection<Token> tokens = qc.convert("+pine apple good ness");+ SpellingOptions spellOpts = new SpellingOptions(tokens, searcher.get().getIndexReader(), 10);+ SpellingResult result = checker.getSuggestions(spellOpts);+ searcher.decref();+ assertTrue(result != null && result.getSuggestions() != null);+ assertTrue(result.getSuggestions().size()==5);+ }+Collection<Token> tokens = qc.convert("paintable pine apple good ness");SpellingOptions spellOpts = new SpellingOptions(tokens, searcher.get().getIndexReader(), 10);SpellingResult result = checker.getSuggestions(spellOpts);