From dac0b565891634291ee346f786beb4ad441aa563 Mon Sep 17 00:00:00 2001 From: James Dyer Date: Tue, 1 Dec 2015 18:29:23 +0000 Subject: [PATCH] SOLR-8175: fix AIOOBE w/WordBreakSolrSpellChecker git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1717492 13f79535-47bb-0310-9956-ffa450edef68 --- solr/CHANGES.txt | 6 ++++++ .../solr/spelling/WordBreakSolrSpellChecker.java | 10 ++++++---- .../solr/spelling/WordBreakSolrSpellCheckerTest.java | 11 +++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index 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.java index 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 tokenArrWithSeparators = new ArrayList<>(options.tokens.size() + 2); List termArr = new ArrayList<>(options.tokens.size() + 2); - List breakSuggestionList = new ArrayList<>(); List 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.java index 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 searcher = core.getSearcher(); QueryConverter qc = new SpellingQueryConverter(); qc.setAnalyzer(new MockAnalyzer(random())); + + { + //Prior to SOLR-8175, the required term would cause an AIOOBE. + Collection 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 tokens = qc.convert("paintable pine apple good ness"); SpellingOptions spellOpts = new SpellingOptions(tokens, searcher.get().getIndexReader(), 10); SpellingResult result = checker.getSuggestions(spellOpts);