Skip to content

Apache Lucene open-source search software

LUCENE-6939: add exponential reciprocal scoring mode to BlendedInfixSuggester

Michael McCandlessfabc3b903dbc
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 5d2874aa65c..7d06b8a6922 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -117,6 +117,10 @@ New Features
for LONG and INT fields with NUMERIC and SORTED_NUMERIC doc values.
(Alexey Zelin via Mikhail Khludnev)
+* LUCENE-6939: Add exponential reciprocal scoring to
+ BlendedInfixSuggester, to even more strongly favor suggestions that
+ match closer to the beginning (Arcadius Ahouansou via Mike McCandless)
+
API Changes
* LUCENE-6908: GeoUtils static relational methods have been refactored to new
diff --git a/lucene/suggest/src/java/org/apache/lucene/search/suggest/analyzing/BlendedInfixSuggester.java b/lucene/suggest/src/java/org/apache/lucene/search/suggest/analyzing/BlendedInfixSuggester.java
index 1787f12d548..63d5cd003cc 100644
--- a/lucene/suggest/src/java/org/apache/lucene/search/suggest/analyzing/BlendedInfixSuggester.java
+++ b/lucene/suggest/src/java/org/apache/lucene/search/suggest/analyzing/BlendedInfixSuggester.java
@@ -62,6 +62,8 @@ public class BlendedInfixSuggester extends AnalyzingInfixSuggester {
*/
protected static double LINEAR_COEF = 0.10;
+ private Double exponent = 2.0;
+
/**
* Default factor
*/
@@ -88,6 +90,8 @@ public class BlendedInfixSuggester extends AnalyzingInfixSuggester {
POSITION_LINEAR,
/** weight/(1+position) */
POSITION_RECIPROCAL,
+ /** weight/pow(1+position, exponent) */
+ POSITION_EXPONENTIAL_RECIPROCAL
// TODO:
//SCORE
}
@@ -118,13 +122,14 @@ public class BlendedInfixSuggester extends AnalyzingInfixSuggester {
this.blenderType = blenderType;
this.numFactor = numFactor;
}
-
+
/**
* Create a new instance, loading from a previously built
* directory, if it exists.
*
* @param blenderType Type of blending strategy, see BlenderType for more precisions
* @param numFactor Factor to multiply the number of searched elements before ponderate
+ * @param exponent exponent used only when blenderType is BlenderType.POSITION_EXPONENTIAL_RECIPROCAL
* @param commitOnBuild Call commit after the index has finished building. This would persist the
* suggester index to disk and future instances of this suggester can use this pre-built dictionary.
* @param allTermsRequired All terms in the suggest query must be matched.
@@ -132,13 +137,16 @@ public class BlendedInfixSuggester extends AnalyzingInfixSuggester {
* @throws IOException If there are problems opening the underlying Lucene index.
*/
public BlendedInfixSuggester(Directory dir, Analyzer indexAnalyzer, Analyzer queryAnalyzer,
- int minPrefixChars, BlenderType blenderType, int numFactor,
+ int minPrefixChars, BlenderType blenderType, int numFactor, Double exponent,
boolean commitOnBuild, boolean allTermsRequired, boolean highlight) throws IOException {
super(dir, indexAnalyzer, queryAnalyzer, minPrefixChars, commitOnBuild, allTermsRequired, highlight);
this.blenderType = blenderType;
this.numFactor = numFactor;
+ if(exponent != null) {
+ this.exponent = exponent;
+ }
}
-
+
@Override
public List<Lookup.LookupResult> lookup(CharSequence key, Set<BytesRef> contexts, boolean onlyMorePopular, int num) throws IOException {
// Don't * numFactor here since we do it down below, once, in the call chain:
@@ -303,6 +311,10 @@ public class BlendedInfixSuggester extends AnalyzingInfixSuggester {
coefficient = 1. / (position + 1);
break;
+ case POSITION_EXPONENTIAL_RECIPROCAL:
+ coefficient = 1. / Math.pow((position + 1.0), exponent);
+ break;
+
default:
coefficient = 1;
}
diff --git a/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/BlendedInfixSuggesterTest.java b/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/BlendedInfixSuggesterTest.java
index eb2b722ceaf..614a1a2c4fa 100644
--- a/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/BlendedInfixSuggesterTest.java
+++ b/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/BlendedInfixSuggesterTest.java
@@ -112,6 +112,18 @@ public class BlendedInfixSuggesterTest extends LuceneTestCase {
assertEquals(w, getInResults(suggester, "top", pl, 1));
assertEquals((int) (w * 1 / (1 + 2)), getInResults(suggester, "the", pl, 1));
assertEquals((int) (w * 1 / (1 + 3)), getInResults(suggester, "lake", pl, 1));
+ suggester.close();
+
+ // BlenderType.EXPONENTIAL_RECIPROCAL is using 1/(pow(1+p, exponent)) * w where w is weight and p the position of the word
+ suggester = new BlendedInfixSuggester(newFSDirectory(tempDir), a, a,
+ AnalyzingInfixSuggester.DEFAULT_MIN_PREFIX_CHARS,
+ BlendedInfixSuggester.BlenderType.POSITION_EXPONENTIAL_RECIPROCAL, 1, 4.0, false, true, false);
+
+ suggester.build(new InputArrayIterator(keys));
+
+ assertEquals(w, getInResults(suggester, "top", pl, 1));
+ assertEquals((int) (w * 1 / (Math.pow(1 + 2, 4.0))), getInResults(suggester, "the", pl, 1));
+ assertEquals((int) (w * 1 / (Math.pow(1 + 3, 4.0))), getInResults(suggester, "lake", pl, 1));
suggester.close();
}
diff --git a/solr/core/src/java/org/apache/solr/spelling/suggest/fst/BlendedInfixLookupFactory.java b/solr/core/src/java/org/apache/solr/spelling/suggest/fst/BlendedInfixLookupFactory.java
index 1b92d1828c9..a5be664ac1b 100644
--- a/solr/core/src/java/org/apache/solr/spelling/suggest/fst/BlendedInfixLookupFactory.java
+++ b/solr/core/src/java/org/apache/solr/spelling/suggest/fst/BlendedInfixLookupFactory.java
@@ -21,13 +21,14 @@ import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
+import java.util.Locale;
import java.util.Set;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.search.suggest.Lookup;
import org.apache.lucene.search.suggest.analyzing.AnalyzingInfixSuggester;
-import org.apache.lucene.search.suggest.analyzing.BlendedInfixSuggester;
import org.apache.lucene.search.suggest.analyzing.BlendedInfixSuggester.BlenderType;
+import org.apache.lucene.search.suggest.analyzing.BlendedInfixSuggester;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.BytesRef;
import org.apache.solr.common.util.NamedList;
@@ -49,7 +50,9 @@ public class BlendedInfixLookupFactory extends AnalyzingInfixLookupFactory {
* reciprocal: weight/(1+position)
*/
private static final String BLENDER_TYPE = "blenderType";
-
+
+ private static final String EXPONENT = "exponent";
+
/**
* Factor to multiply the number of searched elements
* Default is 10
@@ -107,11 +110,13 @@ public class BlendedInfixLookupFactory extends AnalyzingInfixLookupFactory {
int numFactor = params.get(NUM_FACTOR) != null
? Integer.parseInt(params.get(NUM_FACTOR).toString())
: BlendedInfixSuggester.DEFAULT_NUM_FACTOR;
-
+
+ Double exponent = params.get(EXPONENT) == null ? null : Double.valueOf(params.get(EXPONENT).toString());
+
try {
return new BlendedInfixSuggester(FSDirectory.open(new File(indexPath).toPath()),
indexAnalyzer, queryAnalyzer, minPrefixChars,
- blenderType, numFactor, true,
+ blenderType, numFactor, exponent, true,
allTermsRequired, highlight) {
@Override
public List<LookupResult> lookup(CharSequence key, Set<BytesRef> contexts, int num, boolean allTermsRequired, boolean doHighlight) throws IOException {
@@ -144,10 +149,8 @@ public class BlendedInfixLookupFactory extends AnalyzingInfixLookupFactory {
private BlenderType getBlenderType(Object blenderTypeParam) {
BlenderType blenderType = BlenderType.POSITION_LINEAR;
if (blenderTypeParam != null) {
- String blenderTypeStr = blenderTypeParam.toString();
- if (blenderTypeStr.equalsIgnoreCase("reciprocal")) {
- blenderType = BlenderType.POSITION_RECIPROCAL;
- }
+ String blenderTypeStr = blenderTypeParam.toString().toUpperCase(Locale.ROOT);
+ blenderType = BlenderType.valueOf(blenderTypeStr);
}
return blenderType;
}
diff --git a/solr/core/src/test-files/solr/collection1/conf/solrconfig-phrasesuggest.xml b/solr/core/src/test-files/solr/collection1/conf/solrconfig-phrasesuggest.xml
index aedb254940e..450766628a2 100644
--- a/solr/core/src/test-files/solr/collection1/conf/solrconfig-phrasesuggest.xml
+++ b/solr/core/src/test-files/solr/collection1/conf/solrconfig-phrasesuggest.xml
@@ -166,7 +166,8 @@
<str name="indexPath">blendedInfixDir1</str>
<!-- Suggester properties -->
- <str name="blenderType">linear</str>
+ <!--full enum name of BlenderType-->
+ <str name="blenderType">position_linear</str>
<str name="suggestAnalyzerFieldType">text</str>
</lst>
@@ -179,10 +180,40 @@
<str name="indexPath">blendedInfixDir2</str>
<!-- Suggester properties -->
- <str name="blenderType">reciprocal</str>
+ <!--full enum name of BlenderType-->
+ <str name="blenderType">position_reciprocal</str>
<str name="suggestAnalyzerFieldType">text</str>
</lst>
-
+
+ <lst name="suggester">
+ <!-- exponential with default exponent -->
+ <str name="name">blended_infix_suggest_exponential_reciprocal</str>
+ <str name="lookupImpl">BlendedInfixLookupFactory</str>
+ <str name="dictionaryImpl">FileDictionaryFactory</str>
+ <str name="buildOnCommit">false</str>
+ <str name="sourceLocation">blendedInfixSuggest.txt</str>
+ <str name="indexPath">blended_infix_suggest_exponential_reciprocal</str>
+
+ <!-- Suggester properties -->
+ <!--full enum name of BlenderType-->
+ <str name="blenderType">position_exponential_reciprocal</str>
+ <str name="suggestAnalyzerFieldType">text</str>
+ </lst>
+
+ <lst name="suggester">
+ <str name="name">blended_infix_suggest_exponential_reciprocal_1</str>
+ <str name="lookupImpl">BlendedInfixLookupFactory</str>
+ <str name="dictionaryImpl">FileDictionaryFactory</str>
+ <str name="buildOnCommit">false</str>
+ <str name="sourceLocation">blendedInfixSuggest.txt</str>
+ <str name="indexPath">blended_infix_suggest_exponential_reciprocal_1</str>
+
+ <!-- Suggester properties -->
+ <str name="blenderType">position_exponential_reciprocal</str>
+ <double name="exponent">1.0</double>
+ <str name="suggestAnalyzerFieldType">text</str>
+ </lst>
+
</searchComponent>
<!-- FuzzyLookup suggest component with FileDictionaryFactory -->
diff --git a/solr/core/src/test/org/apache/solr/spelling/suggest/TestBlendedInfixSuggestions.java b/solr/core/src/test/org/apache/solr/spelling/suggest/TestBlendedInfixSuggestions.java
index b39da4d7056..c11d9db1c5c 100644
--- a/solr/core/src/test/org/apache/solr/spelling/suggest/TestBlendedInfixSuggestions.java
+++ b/solr/core/src/test/org/apache/solr/spelling/suggest/TestBlendedInfixSuggestions.java
@@ -58,7 +58,37 @@ public class TestBlendedInfixSuggestions extends SolrTestCaseJ4 {
"//lst[@name='suggest']/lst[@name='blended_infix_suggest_reciprocal']/lst[@name='the']/arr[@name='suggestions']/lst[3]/str[@name='payload'][.='star']"
);
}
-
+
+ public void testExponentialReciprocalBlenderTypeExponent1() { //exponent=1 will give same output as reciprocal
+ assertQ(req("qt", URI, "q", "the", SuggesterParams.SUGGEST_COUNT, "10", SuggesterParams.SUGGEST_DICT, "blended_infix_suggest_exponential_reciprocal_1"),
+ "//lst[@name='suggest']/lst[@name='blended_infix_suggest_exponential_reciprocal_1']/lst[@name='the']/int[@name='numFound'][.='3']",
+ "//lst[@name='suggest']/lst[@name='blended_infix_suggest_exponential_reciprocal_1']/lst[@name='the']/arr[@name='suggestions']/lst[1]/str[@name='term'][.='<b>the</b> returned']",
+ "//lst[@name='suggest']/lst[@name='blended_infix_suggest_exponential_reciprocal_1']/lst[@name='the']/arr[@name='suggestions']/lst[1]/long[@name='weight'][.='10']",
+ "//lst[@name='suggest']/lst[@name='blended_infix_suggest_exponential_reciprocal_1']/lst[@name='the']/arr[@name='suggestions']/lst[1]/str[@name='payload'][.='ret']",
+ "//lst[@name='suggest']/lst[@name='blended_infix_suggest_exponential_reciprocal_1']/lst[@name='the']/arr[@name='suggestions']/lst[2]/str[@name='term'][.='top of <b>the</b> lake']",
+ "//lst[@name='suggest']/lst[@name='blended_infix_suggest_exponential_reciprocal_1']/lst[@name='the']/arr[@name='suggestions']/lst[2]/long[@name='weight'][.='6']",
+ "//lst[@name='suggest']/lst[@name='blended_infix_suggest_exponential_reciprocal_1']/lst[@name='the']/arr[@name='suggestions']/lst[2]/str[@name='payload'][.='lake']",
+ "//lst[@name='suggest']/lst[@name='blended_infix_suggest_exponential_reciprocal_1']/lst[@name='the']/arr[@name='suggestions']/lst[3]/str[@name='term'][.='star wars: episode v - <b>the</b> empire strikes back']",
+ "//lst[@name='suggest']/lst[@name='blended_infix_suggest_exponential_reciprocal_1']/lst[@name='the']/arr[@name='suggestions']/lst[3]/long[@name='weight'][.='2']",
+ "//lst[@name='suggest']/lst[@name='blended_infix_suggest_exponential_reciprocal_1']/lst[@name='the']/arr[@name='suggestions']/lst[3]/str[@name='payload'][.='star']"
+ );
+ }
+
+ public void testExponentialReciprocalBlenderType() { // default is exponent=2.0
+ assertQ(req("qt", URI, "q", "the", SuggesterParams.SUGGEST_COUNT, "10", SuggesterParams.SUGGEST_DICT, "blended_infix_suggest_exponential_reciprocal"),
+ "//lst[@name='suggest']/lst[@name='blended_infix_suggest_exponential_reciprocal']/lst[@name='the']/int[@name='numFound'][.='3']",
+ "//lst[@name='suggest']/lst[@name='blended_infix_suggest_exponential_reciprocal']/lst[@name='the']/arr[@name='suggestions']/lst[1]/str[@name='term'][.='<b>the</b> returned']",
+ "//lst[@name='suggest']/lst[@name='blended_infix_suggest_exponential_reciprocal']/lst[@name='the']/arr[@name='suggestions']/lst[1]/long[@name='weight'][.='10']",
+ "//lst[@name='suggest']/lst[@name='blended_infix_suggest_exponential_reciprocal']/lst[@name='the']/arr[@name='suggestions']/lst[1]/str[@name='payload'][.='ret']",
+ "//lst[@name='suggest']/lst[@name='blended_infix_suggest_exponential_reciprocal']/lst[@name='the']/arr[@name='suggestions']/lst[2]/str[@name='term'][.='top of <b>the</b> lake']",
+ "//lst[@name='suggest']/lst[@name='blended_infix_suggest_exponential_reciprocal']/lst[@name='the']/arr[@name='suggestions']/lst[2]/long[@name='weight'][.='2']",
+ "//lst[@name='suggest']/lst[@name='blended_infix_suggest_exponential_reciprocal']/lst[@name='the']/arr[@name='suggestions']/lst[2]/str[@name='payload'][.='lake']",
+ "//lst[@name='suggest']/lst[@name='blended_infix_suggest_exponential_reciprocal']/lst[@name='the']/arr[@name='suggestions']/lst[3]/str[@name='term'][.='star wars: episode v - <b>the</b> empire strikes back']",
+ "//lst[@name='suggest']/lst[@name='blended_infix_suggest_exponential_reciprocal']/lst[@name='the']/arr[@name='suggestions']/lst[3]/long[@name='weight'][.='0']",
+ "//lst[@name='suggest']/lst[@name='blended_infix_suggest_exponential_reciprocal']/lst[@name='the']/arr[@name='suggestions']/lst[3]/str[@name='payload'][.='star']"
+ );
+ }
+
public void testMultiSuggester() {
assertQ(req("qt", URI, "q", "the", SuggesterParams.SUGGEST_COUNT, "10", SuggesterParams.SUGGEST_DICT, "blended_infix_suggest_linear", SuggesterParams.SUGGEST_DICT, "blended_infix_suggest_reciprocal"),
"//lst[@name='suggest']/lst[@name='blended_infix_suggest_linear']/lst[@name='the']/int[@name='numFound'][.='3']",