From 484e3ee857a7e0821aef13ff08d80794211e2daa Mon Sep 17 00:00:00 2001 From: abhidemon Date: Tue, 28 Mar 2017 20:42:17 +0530 Subject: [PATCH] feat(WordBreakSpellChecker): Override spellcheck's SuggestMode by WordBreakSolrSpellChecker Now `suggestMode` can be specified for WordBreakSolrSpellChecker, if this value is not null, it will override the `suggestMode` of SpellCheckComponent. Fixes Performance and Relevancy --- .../spelling/WordBreakSolrSpellChecker.java | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) 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 a5d692b6fe24..598a0021391e 100644 --- a/solr/core/src/java/org/apache/solr/spelling/WordBreakSolrSpellChecker.java +++ b/solr/core/src/java/org/apache/solr/spelling/WordBreakSolrSpellChecker.java @@ -22,12 +22,16 @@ import java.util.Iterator; import java.util.List; import java.util.Locale; +import java.util.Arrays; + + import java.util.regex.Pattern; import org.apache.lucene.analysis.Token; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; import org.apache.lucene.search.spell.CombineSuggestion; +import org.apache.lucene.search.spell.SuggestMode; import org.apache.lucene.search.spell.SuggestWord; import org.apache.lucene.search.spell.WordBreakSpellChecker; import org.apache.lucene.search.spell.WordBreakSpellChecker.BreakSuggestionSortMethod; @@ -90,7 +94,10 @@ public class WordBreakSolrSpellChecker extends SolrSpellChecker { * See {@link WordBreakSpellChecker#setMinSuggestionFrequency} */ public static final String PARAM_MIN_SUGGESTION_FREQUENCY = "minSuggestionFreq"; - + + public static final String PARAM_SUGGEST_MODE = "suggestMode"; + + /** *

* Specify a value on the "breakSugestionTieBreaker" parameter. @@ -114,6 +121,7 @@ public enum BreakSuggestionTieBreaker { private WordBreakSpellChecker wbsp = null; private boolean combineWords = false; private boolean breakWords = false; + private SuggestMode suggestMode = null; private BreakSuggestionSortMethod sortMethod = BreakSuggestionSortMethod.NUM_CHANGES_THEN_MAX_FREQUENCY; private static final Pattern spacePattern = Pattern.compile("\\s+"); @@ -123,6 +131,16 @@ public String init(@SuppressWarnings("unchecked") NamedList config, String name = super.init(config, core); combineWords = boolParam(config, PARAM_COMBINE_WORDS); breakWords = boolParam(config, PARAM_BREAK_WORDS); + if (config.get(PARAM_SUGGEST_MODE)!=null){ + try{ + suggestMode = SuggestMode.valueOf(strParam(config,PARAM_SUGGEST_MODE)); + } catch (IllegalArgumentException iae){ + String errMsg = "Error initialising " + this.getClass().getName() + + ". Invalid `suggestMode` : '" + strParam(config, PARAM_SUGGEST_MODE) + + "' . Permitted Values: " + Arrays.asList(SuggestMode.values()); + throw new IllegalArgumentException(errMsg); + } + } wbsp = new WordBreakSpellChecker(); String bstb = strParam(config, PARAM_BREAK_SUGGESTION_TIE_BREAKER); if (bstb != null) { @@ -195,7 +213,9 @@ public SpellingResult getSuggestions(SpellingOptions options) throws IOException { IndexReader ir = options.reader; int numSuggestions = options.count; - + if ( suggestMode == null ){ + suggestMode = options.suggestMode; + } StringBuilder sb = new StringBuilder(); Token[] tokenArr = options.tokens.toArray(new Token[options.tokens.size()]); List tokenArrWithSeparators = new ArrayList<>(options.tokens.size() + 2); @@ -229,7 +249,7 @@ public SpellingResult getSuggestions(SpellingOptions options) tokenArrWithSeparators.add(tokenArr[i]); if (breakWords) { SuggestWord[][] breakSuggestions = wbsp.suggestWordBreaks(thisTerm, - numSuggestions, ir, options.suggestMode, sortMethod); + numSuggestions, ir, suggestMode, sortMethod); if(breakSuggestions.length==0) { noBreakSuggestionList.add(new ResultEntry(tokenArr[i], null, 0)); } @@ -258,7 +278,7 @@ public SpellingResult getSuggestions(SpellingOptions options) List combineSuggestionList = Collections.emptyList(); CombineSuggestion[] combineSuggestions = wbsp.suggestWordCombinations( - termArr.toArray(new Term[termArr.size()]), numSuggestions, ir, options.suggestMode); + termArr.toArray(new Term[termArr.size()]), numSuggestions, ir, suggestMode); if (combineWords) { combineSuggestionList = new ArrayList<>( combineSuggestions.length);