SOLR-6648: Add support for highlight and allTermsRequired configuration in AnalyzingInfix and BlendedInfix Solr suggesters

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1657655 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Tomas Eduardo Fernandez Lobbe 2015-02-05 18:24:01 +00:00
parent 442f111b4d
commit b96f012f2e
5 changed files with 129 additions and 3 deletions

View File

@ -97,6 +97,10 @@ New Features
collection that has a replica on all nodes where there is a replica in the to index
(Jack Lo, Timothy Potter)
* SOLR-6648: Add support in AnalyzingInfixLookupFactory and BlendedInfixLookupFactory
for setting 'highlight' and 'allTermsRequired' in the suggester configuration.
(Boon Low, Varun Thacker via Tomás Fernández Löbbe)
Bug Fixes
----------------------

View File

@ -57,6 +57,15 @@ public class AnalyzingInfixLookupFactory extends LookupFactory {
*/
protected static final String MIN_PREFIX_CHARS = "minPrefixChars";
/**
* Boolean clause matching option for multiple terms
* Default is true - all terms required.
*/
protected static final String ALL_TERMS_REQUIRED = "allTermsRequired";
/** Highlight suggest terms - default is true. */
protected static final String HIGHLIGHT = "highlight";
/**
* Default path where the index for the suggester is stored/loaded from
* */
@ -94,10 +103,19 @@ public class AnalyzingInfixLookupFactory extends LookupFactory {
int minPrefixChars = params.get(MIN_PREFIX_CHARS) != null
? Integer.parseInt(params.get(MIN_PREFIX_CHARS).toString())
: AnalyzingInfixSuggester.DEFAULT_MIN_PREFIX_CHARS;
boolean allTermsRequired = params.get(ALL_TERMS_REQUIRED) != null
? Boolean.getBoolean(params.get(ALL_TERMS_REQUIRED).toString())
: AnalyzingInfixSuggester.DEFAULT_ALL_TERMS_REQUIRED;
boolean highlight = params.get(HIGHLIGHT) != null
? Boolean.getBoolean(params.get(HIGHLIGHT).toString())
: AnalyzingInfixSuggester.DEFAULT_HIGHLIGHT;
try {
return new AnalyzingInfixSuggester(FSDirectory.open(new File(indexPath).toPath()), indexAnalyzer,
queryAnalyzer, minPrefixChars, true) {
queryAnalyzer, minPrefixChars, true,
allTermsRequired, highlight) {
@Override
public List<LookupResult> lookup(CharSequence key, Set<BytesRef> contexts, int num, boolean allTermsRequired, boolean doHighlight) throws IOException {
List<LookupResult> res = super.lookup(key, contexts, num, allTermsRequired, doHighlight);

View File

@ -93,6 +93,14 @@ public class BlendedInfixLookupFactory extends AnalyzingInfixLookupFactory {
int minPrefixChars = params.get(MIN_PREFIX_CHARS) != null
? Integer.parseInt(params.get(MIN_PREFIX_CHARS).toString())
: AnalyzingInfixSuggester.DEFAULT_MIN_PREFIX_CHARS;
boolean allTermsRequired = params.get(ALL_TERMS_REQUIRED) != null
? Boolean.getBoolean(params.get(ALL_TERMS_REQUIRED).toString())
: AnalyzingInfixSuggester.DEFAULT_ALL_TERMS_REQUIRED;
boolean highlight = params.get(HIGHLIGHT) != null
? Boolean.getBoolean(params.get(HIGHLIGHT).toString())
: AnalyzingInfixSuggester.DEFAULT_HIGHLIGHT;
BlenderType blenderType = getBlenderType(params.get(BLENDER_TYPE));
@ -103,7 +111,8 @@ public class BlendedInfixLookupFactory extends AnalyzingInfixLookupFactory {
try {
return new BlendedInfixSuggester(FSDirectory.open(new File(indexPath).toPath()),
indexAnalyzer, queryAnalyzer, minPrefixChars,
blenderType, numFactor, true) {
blenderType, numFactor, true,
allTermsRequired, highlight) {
@Override
public List<LookupResult> lookup(CharSequence key, Set<BytesRef> contexts, int num, boolean allTermsRequired, boolean doHighlight) throws IOException {
List<LookupResult> res = super.lookup(key, contexts, num, allTermsRequired, doHighlight);

View File

@ -82,7 +82,43 @@
<!-- specify a fieldType using keywordtokenizer + lowercase + cleanup -->
<str name="queryAnalyzerFieldType">phrase_suggest</str>
</searchComponent>
<!-- AnalyzingInfixLookup suggest component (SolrSuggester - default) -->
<searchComponent class="solr.SuggestComponent" name="analyzing_infix_suggest">
<!-- Default: highlight - true, allTermsRequired - true -->
<lst name="suggester">
<str name="name">analyzing_infix_suggest_default</str>
<str name="lookupImpl">AnalyzingInfixLookupFactory</str>
<str name="dictionaryImpl">FileDictionaryFactory</str>
<str name="buildOnCommit">false</str>
<str name="indexPath">analyzing_infix_suggest_default</str>
<str name="sourceLocation">analyzingInfixSuggest.txt</str>
<str name="suggestAnalyzerFieldType">text</str>
</lst>
<lst name="suggester">
<str name="name">analyzing_infix_suggest_without_highlight</str>
<str name="lookupImpl">AnalyzingInfixLookupFactory</str>
<str name="dictionaryImpl">FileDictionaryFactory</str>
<str name="buildOnCommit">false</str>
<str name="indexPath">analyzing_infix_suggest_without_highlight</str>
<str name="sourceLocation">analyzingInfixSuggest.txt</str>
<str name="suggestAnalyzerFieldType">text</str>
<bool name="highlight">false</bool>
</lst>
<lst name="suggester">
<str name="name">analyzing_infix_suggest_not_all_terms_required</str>
<str name="lookupImpl">AnalyzingInfixLookupFactory</str>
<str name="dictionaryImpl">FileDictionaryFactory</str>
<str name="buildOnCommit">false</str>
<str name="indexPath">analyzing_infix_suggest_not_all_terms_required</str>
<str name="sourceLocation">analyzingInfixSuggest.txt</str>
<str name="suggestAnalyzerFieldType">text</str>
<bool name="allTermsRequired">false</bool>
</lst>
</searchComponent>
<!-- FuzzyLookup suggest component with HighFrequencyDictionary -->
<searchComponent class="solr.SuggestComponent" name="fuzzy_suggest_analyzing_with_high_freq_dict">
@ -306,6 +342,14 @@
</arr>
</requestHandler>
<requestHandler class="org.apache.solr.handler.component.SearchHandler" name="/analyzing_infix_suggest">
<lst name="defaults">
<str name="suggest">true</str>
</lst>
<arr name="components">
<str>analyzing_infix_suggest</str>
</arr>
</requestHandler>
<requestHandler class="org.apache.solr.handler.component.SearchHandler" name="/blended_infix_suggest">
<lst name="defaults">

View File

@ -23,11 +23,13 @@ import org.junit.BeforeClass;
public class TestAnalyzeInfixSuggestions extends SolrTestCaseJ4 {
static final String URI_DEFAULT = "/infix_suggest_analyzing";
static final String URI_SUGGEST_DEFAULT = "/analyzing_infix_suggest";
@BeforeClass
public static void beforeClass() throws Exception {
initCore("solrconfig-phrasesuggest.xml","schema-phrasesuggest.xml");
assertQ(req("qt", URI_DEFAULT, "q", "", SpellingParams.SPELLCHECK_BUILD, "true"));
assertQ(req("qt", URI_SUGGEST_DEFAULT, "q", "", SuggesterParams.SUGGEST_BUILD_ALL, "true"));
}
public void testSingle() throws Exception {
@ -41,6 +43,17 @@ public class TestAnalyzeInfixSuggestions extends SolrTestCaseJ4 {
"//lst[@name='spellcheck']/lst[@name='suggestions']/lst[@name='high']/int[@name='numFound'][.='1']",
"//lst[@name='spellcheck']/lst[@name='suggestions']/lst[@name='high']/arr[@name='suggestion']/str[1][.='Japanese Autocomplete and Japanese <b>High</b>lighter broken']"
);
/* equivalent SolrSuggester, SuggestComponent tests */
assertQ(req("qt", URI_SUGGEST_DEFAULT, "q", "japan", SuggesterParams.SUGGEST_COUNT, "1", SuggesterParams.SUGGEST_DICT, "analyzing_infix_suggest_default"),
"//lst[@name='suggest']/lst[@name='analyzing_infix_suggest_default']/lst[@name='japan']/int[@name='numFound'][.='1']",
"//lst[@name='suggest']/lst[@name='analyzing_infix_suggest_default']/lst[@name='japan']/arr[@name='suggestions']/lst[1]/str[@name='term'][.='<b>Japan</b>ese Autocomplete and <b>Japan</b>ese Highlighter broken']"
);
assertQ(req("qt", URI_SUGGEST_DEFAULT, "q", "high", SuggesterParams.SUGGEST_COUNT, "1", SuggesterParams.SUGGEST_DICT, "analyzing_infix_suggest_default"),
"//lst[@name='suggest']/lst[@name='analyzing_infix_suggest_default']/lst[@name='high']/int[@name='numFound'][.='1']",
"//lst[@name='suggest']/lst[@name='analyzing_infix_suggest_default']/lst[@name='high']/arr[@name='suggestions']/lst[1]/str[@name='term'][.='Japanese Autocomplete and Japanese <b>High</b>lighter broken']"
);
}
public void testMultiple() throws Exception {
@ -62,6 +75,44 @@ public class TestAnalyzeInfixSuggestions extends SolrTestCaseJ4 {
"//lst[@name='spellcheck']/lst[@name='suggestions']/lst[@name='japan']/arr[@name='suggestion']/str[2][.='Add <b>Japan</b>ese Kanji number normalization to Kuromoji']",
"//lst[@name='spellcheck']/lst[@name='suggestions']/lst[@name='japan']/arr[@name='suggestion']/str[3][.='Add decompose compound <b>Japan</b>ese Katakana token capability to Kuromoji']"
);
/* SolrSuggester, SuggestComponent tests: allTermsRequire (true), highlight (true) */
assertQ(req("qt", URI_SUGGEST_DEFAULT, "q", "japan", SuggesterParams.SUGGEST_COUNT, "2", SuggesterParams.SUGGEST_DICT, "analyzing_infix_suggest_default"),
"//lst[@name='suggest']/lst[@name='analyzing_infix_suggest_default']/lst[@name='japan']/int[@name='numFound'][.='2']",
"//lst[@name='suggest']/lst[@name='analyzing_infix_suggest_default']/lst[@name='japan']/arr[@name='suggestions']/lst[1]/str[@name='term'][.='<b>Japan</b>ese Autocomplete and <b>Japan</b>ese Highlighter broken']",
"//lst[@name='suggest']/lst[@name='analyzing_infix_suggest_default']/lst[@name='japan']/arr[@name='suggestions']/lst[2]/str[@name='term'][.='Add <b>Japan</b>ese Kanji number normalization to Kuromoji']"
);
assertQ(req("qt", URI_SUGGEST_DEFAULT, "q", "japanese ka", SuggesterParams.SUGGEST_COUNT, "2", SuggesterParams.SUGGEST_DICT, "analyzing_infix_suggest_default"),
"//lst[@name='suggest']/lst[@name='analyzing_infix_suggest_default']/lst[@name='japanese ka']/int[@name='numFound'][.='2']",
"//lst[@name='suggest']/lst[@name='analyzing_infix_suggest_default']/lst[@name='japanese ka']/arr[@name='suggestions']/lst[1]/str[@name='term'][.='Add <b>Japanese</b> <b>Ka</b>nji number normalization to Kuromoji']",
"//lst[@name='suggest']/lst[@name='analyzing_infix_suggest_default']/lst[@name='japanese ka']/arr[@name='suggestions']/lst[2]/str[@name='term'][.='Add decompose compound <b>Japanese</b> <b>Ka</b>takana token capability to Kuromoji']"
);
}
public void testWithoutHighlight() throws Exception {
assertQ(req("qt", URI_SUGGEST_DEFAULT, "q", "japan", SuggesterParams.SUGGEST_COUNT, "2", SuggesterParams.SUGGEST_DICT, "analyzing_infix_suggest_without_highlight"),
"//lst[@name='suggest']/lst[@name='analyzing_infix_suggest_without_highlight']/lst[@name='japan']/int[@name='numFound'][.='2']",
"//lst[@name='suggest']/lst[@name='analyzing_infix_suggest_without_highlight']/lst[@name='japan']/arr[@name='suggestions']/lst[1]/str[@name='term'][.='Japanese Autocomplete and Japanese Highlighter broken']",
"//lst[@name='suggest']/lst[@name='analyzing_infix_suggest_without_highlight']/lst[@name='japan']/arr[@name='suggestions']/lst[2]/str[@name='term'][.='Add Japanese Kanji number normalization to Kuromoji']"
);
}
public void testNotAllTermsRequired() throws Exception {
assertQ(req("qt", URI_SUGGEST_DEFAULT, "q", "japanese javanese", SuggesterParams.SUGGEST_COUNT, "5", SuggesterParams.SUGGEST_DICT, "analyzing_infix_suggest_not_all_terms_required"),
"//lst[@name='suggest']/lst[@name='analyzing_infix_suggest_not_all_terms_required']/lst[@name='japanese javanese']/int[@name='numFound'][.='3']",
"//lst[@name='suggest']/lst[@name='analyzing_infix_suggest_not_all_terms_required']/lst[@name='japanese javanese']/arr[@name='suggestions']/lst[1]/str[@name='term'][.='<b>Japanese</b> Autocomplete and <b>Japanese</b> Highlighter broken']",
"//lst[@name='suggest']/lst[@name='analyzing_infix_suggest_not_all_terms_required']/lst[@name='japanese javanese']/arr[@name='suggestions']/lst[2]/str[@name='term'][.='Add <b>Japanese</b> Kanji number normalization to Kuromoji']",
"//lst[@name='suggest']/lst[@name='analyzing_infix_suggest_not_all_terms_required']/lst[@name='japanese javanese']/arr[@name='suggestions']/lst[3]/str[@name='term'][.='Add decompose compound <b>Japanese</b> Katakana token capability to Kuromoji']"
);
assertQ(req("qt", URI_SUGGEST_DEFAULT, "q", "just number", SuggesterParams.SUGGEST_COUNT, "5", SuggesterParams.SUGGEST_DICT, "analyzing_infix_suggest_not_all_terms_required"),
"//lst[@name='suggest']/lst[@name='analyzing_infix_suggest_not_all_terms_required']/lst[@name='just number']/int[@name='numFound'][.='2']",
"//lst[@name='suggest']/lst[@name='analyzing_infix_suggest_not_all_terms_required']/lst[@name='just number']/arr[@name='suggestions']/lst[1]/str[@name='term'][.='Add Japanese Kanji <b>number</b> normalization to Kuromoji']",
"//lst[@name='suggest']/lst[@name='analyzing_infix_suggest_not_all_terms_required']/lst[@name='just number']/arr[@name='suggestions']/lst[2]/str[@name='term'][.='This is <b>just</b> another entry!']"
);
}
}