Class BlendedInfixSuggester
- java.lang.Object
-
- org.apache.lucene.search.suggest.Lookup
-
- org.apache.lucene.search.suggest.analyzing.AnalyzingInfixSuggester
-
- org.apache.lucene.search.suggest.analyzing.BlendedInfixSuggester
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,Accountable
public class BlendedInfixSuggester extends AnalyzingInfixSuggester
Extension of the AnalyzingInfixSuggester which transforms the weight after search to take into account the position of the searched term into the indexed text. Please note that it increases the number of elements searched and applies the ponderation after. It might be costly for long suggestions.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
BlendedInfixSuggester.BlenderType
The different types of blender.private static class
BlendedInfixSuggester.LookUpComparator
-
Nested classes/interfaces inherited from class org.apache.lucene.search.suggest.Lookup
Lookup.LookupPriorityQueue, Lookup.LookupResult
-
-
Field Summary
Fields Modifier and Type Field Description private BlendedInfixSuggester.BlenderType
blenderType
Type of blender used by the suggesterstatic int
DEFAULT_NUM_FACTOR
Default factorprivate java.lang.Double
exponent
protected static double
LINEAR_COEF
Coefficient used for linear blendingprivate static java.util.Comparator<Lookup.LookupResult>
LOOKUP_COMP
private int
numFactor
Factor to multiply the number of searched elements-
Fields inherited from class org.apache.lucene.search.suggest.analyzing.AnalyzingInfixSuggester
CONTEXTS_FIELD_NAME, DEFAULT_ALL_TERMS_REQUIRED, DEFAULT_CLOSE_INDEXWRITER_ON_BUILD, DEFAULT_HIGHLIGHT, DEFAULT_MIN_PREFIX_CHARS, EXACT_TEXT_FIELD_NAME, indexAnalyzer, minPrefixChars, queryAnalyzer, searcherMgr, searcherMgrLock, TEXT_FIELD_NAME, TEXTGRAMS_FIELD_NAME, writer, writerLock
-
Fields inherited from class org.apache.lucene.search.suggest.Lookup
CHARSEQUENCE_COMPARATOR
-
Fields inherited from interface org.apache.lucene.util.Accountable
NULL_ACCOUNTABLE
-
-
Constructor Summary
Constructors Constructor Description BlendedInfixSuggester(Directory dir, Analyzer analyzer)
Create a new instance, loading from a previously built directory, if it exists.BlendedInfixSuggester(Directory dir, Analyzer indexAnalyzer, Analyzer queryAnalyzer, int minPrefixChars, BlendedInfixSuggester.BlenderType blenderType, int numFactor, boolean commitOnBuild)
Create a new instance, loading from a previously built directory, if it exists.BlendedInfixSuggester(Directory dir, Analyzer indexAnalyzer, Analyzer queryAnalyzer, int minPrefixChars, BlendedInfixSuggester.BlenderType blenderType, int numFactor, java.lang.Double exponent, boolean commitOnBuild, boolean allTermsRequired, boolean highlight)
Create a new instance, loading from a previously built directory, if it exists.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static void
boundedTreeAdd(java.util.TreeSet<Lookup.LookupResult> results, Lookup.LookupResult result, int num)
Add an element to the tree respecting a size limitprotected double
calculateCoefficient(int position)
Calculate the weight coefficient based on the position of the first matching word.private double
createCoefficient(IndexSearcher searcher, int doc, java.util.Set<java.lang.String> matchedTokens, java.lang.String prefixToken)
Create the coefficient to transform the weight.protected java.util.List<Lookup.LookupResult>
createResults(IndexSearcher searcher, TopFieldDocs hits, int num, java.lang.CharSequence key, boolean doHighlight, java.util.Set<java.lang.String> matchedTokens, java.lang.String prefixToken)
Create the results based on the search hits.protected FieldType
getTextFieldType()
Subclass can override this method to change the field type of the text field e.g.java.util.List<Lookup.LookupResult>
lookup(java.lang.CharSequence key, java.util.Map<BytesRef,BooleanClause.Occur> contextInfo, int num, boolean allTermsRequired, boolean doHighlight)
Retrieve suggestions, specifying whether all terms must match (allTermsRequired
) and whether the hits should be highlighted (doHighlight
).java.util.List<Lookup.LookupResult>
lookup(java.lang.CharSequence key, java.util.Set<BytesRef> contexts, boolean onlyMorePopular, int num)
Look up a key and return possible completion for this key.java.util.List<Lookup.LookupResult>
lookup(java.lang.CharSequence key, java.util.Set<BytesRef> contexts, int num, boolean allTermsRequired, boolean doHighlight)
Lookup, with context but without booleans.java.util.List<Lookup.LookupResult>
lookup(java.lang.CharSequence key, BooleanQuery contextQuery, int num, boolean allTermsRequired, boolean doHighlight)
This is an advanced method providing the capability to send down to the suggester any arbitrary lucene query to be used to filter the result of the suggester-
Methods inherited from class org.apache.lucene.search.suggest.analyzing.AnalyzingInfixSuggester
add, addContextToQuery, addNonMatch, addPrefixMatch, addWholeMatch, build, close, commit, finishQuery, getCount, getDirectory, getIndexWriterConfig, getLastTokenQuery, highlight, load, lookup, ramBytesUsed, refresh, store, update
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.lucene.util.Accountable
getChildResources
-
-
-
-
Field Detail
-
LINEAR_COEF
protected static double LINEAR_COEF
Coefficient used for linear blending
-
exponent
private java.lang.Double exponent
-
DEFAULT_NUM_FACTOR
public static int DEFAULT_NUM_FACTOR
Default factor
-
numFactor
private final int numFactor
Factor to multiply the number of searched elements
-
blenderType
private final BlendedInfixSuggester.BlenderType blenderType
Type of blender used by the suggester
-
LOOKUP_COMP
private static java.util.Comparator<Lookup.LookupResult> LOOKUP_COMP
-
-
Constructor Detail
-
BlendedInfixSuggester
public BlendedInfixSuggester(Directory dir, Analyzer analyzer) throws java.io.IOException
Create a new instance, loading from a previously built directory, if it exists.- Throws:
java.io.IOException
-
BlendedInfixSuggester
public BlendedInfixSuggester(Directory dir, Analyzer indexAnalyzer, Analyzer queryAnalyzer, int minPrefixChars, BlendedInfixSuggester.BlenderType blenderType, int numFactor, boolean commitOnBuild) throws java.io.IOException
Create a new instance, loading from a previously built directory, if it exists.- Parameters:
blenderType
- Type of blending strategy, see BlenderType for more precisionsnumFactor
- Factor to multiply the number of searched elements before ponderatecommitOnBuild
- 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.- Throws:
java.io.IOException
- If there are problems opening the underlying Lucene index.
-
BlendedInfixSuggester
public BlendedInfixSuggester(Directory dir, Analyzer indexAnalyzer, Analyzer queryAnalyzer, int minPrefixChars, BlendedInfixSuggester.BlenderType blenderType, int numFactor, java.lang.Double exponent, boolean commitOnBuild, boolean allTermsRequired, boolean highlight) throws java.io.IOException
Create a new instance, loading from a previously built directory, if it exists.- Parameters:
blenderType
- Type of blending strategy, see BlenderType for more precisionsnumFactor
- Factor to multiply the number of searched elements before ponderateexponent
- exponent used only when blenderType is BlenderType.POSITION_EXPONENTIAL_RECIPROCALcommitOnBuild
- 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.allTermsRequired
- All terms in the suggest query must be matched.highlight
- Highlight suggest query in suggestions.- Throws:
java.io.IOException
- If there are problems opening the underlying Lucene index.
-
-
Method Detail
-
lookup
public java.util.List<Lookup.LookupResult> lookup(java.lang.CharSequence key, java.util.Set<BytesRef> contexts, boolean onlyMorePopular, int num) throws java.io.IOException
Description copied from class:Lookup
Look up a key and return possible completion for this key.- Overrides:
lookup
in classAnalyzingInfixSuggester
- Parameters:
key
- lookup key. Depending on the implementation this may be a prefix, misspelling, or even infix.contexts
- contexts to filter the lookup by, or null if all contexts are allowed; if the suggestion contains any of the contexts, it's a matchonlyMorePopular
- return only more popular resultsnum
- maximum number of results to return- Returns:
- a list of possible completions, with their relative weight (e.g. popularity)
- Throws:
java.io.IOException
-
lookup
public java.util.List<Lookup.LookupResult> lookup(java.lang.CharSequence key, java.util.Set<BytesRef> contexts, int num, boolean allTermsRequired, boolean doHighlight) throws java.io.IOException
Description copied from class:AnalyzingInfixSuggester
Lookup, with context but without booleans. Context booleans default to SHOULD, so each suggestion must have at least one of the contexts.- Overrides:
lookup
in classAnalyzingInfixSuggester
- Throws:
java.io.IOException
-
lookup
public java.util.List<Lookup.LookupResult> lookup(java.lang.CharSequence key, java.util.Map<BytesRef,BooleanClause.Occur> contextInfo, int num, boolean allTermsRequired, boolean doHighlight) throws java.io.IOException
Description copied from class:AnalyzingInfixSuggester
Retrieve suggestions, specifying whether all terms must match (allTermsRequired
) and whether the hits should be highlighted (doHighlight
).- Overrides:
lookup
in classAnalyzingInfixSuggester
- Throws:
java.io.IOException
-
lookup
public java.util.List<Lookup.LookupResult> lookup(java.lang.CharSequence key, BooleanQuery contextQuery, int num, boolean allTermsRequired, boolean doHighlight) throws java.io.IOException
Description copied from class:AnalyzingInfixSuggester
This is an advanced method providing the capability to send down to the suggester any arbitrary lucene query to be used to filter the result of the suggester- Overrides:
lookup
in classAnalyzingInfixSuggester
- Parameters:
key
- the keyword being looked forcontextQuery
- an arbitrary Lucene query to be used to filter the result of the suggester.AnalyzingInfixSuggester.addContextToQuery(org.apache.lucene.search.BooleanQuery.Builder, org.apache.lucene.util.BytesRef, org.apache.lucene.search.BooleanClause.Occur)
could be used to build this contextQuery.num
- number of items to returnallTermsRequired
- all searched terms must match or notdoHighlight
- if true, the matching term will be highlighted in the search result- Returns:
- the result of the suggester
- Throws:
java.io.IOException
- f the is IO exception while reading data from the index
-
getTextFieldType
protected FieldType getTextFieldType()
Description copied from class:AnalyzingInfixSuggester
Subclass can override this method to change the field type of the text field e.g. to change the index options- Overrides:
getTextFieldType
in classAnalyzingInfixSuggester
-
createResults
protected java.util.List<Lookup.LookupResult> createResults(IndexSearcher searcher, TopFieldDocs hits, int num, java.lang.CharSequence key, boolean doHighlight, java.util.Set<java.lang.String> matchedTokens, java.lang.String prefixToken) throws java.io.IOException
Description copied from class:AnalyzingInfixSuggester
Create the results based on the search hits. Can be overridden by subclass to add particular behavior (e.g. weight transformation). Note that there is no prefix token (theprefixToken
argument will be null) whenever the final token in the incoming request was in fact finished (had trailing characters, such as white-space).- Overrides:
createResults
in classAnalyzingInfixSuggester
- Throws:
java.io.IOException
- If there are problems reading fields from the underlying Lucene index.
-
boundedTreeAdd
private static void boundedTreeAdd(java.util.TreeSet<Lookup.LookupResult> results, Lookup.LookupResult result, int num)
Add an element to the tree respecting a size limit- Parameters:
results
- the tree to add inresult
- the result we try to addnum
- size limit
-
createCoefficient
private double createCoefficient(IndexSearcher searcher, int doc, java.util.Set<java.lang.String> matchedTokens, java.lang.String prefixToken) throws java.io.IOException
Create the coefficient to transform the weight.- Parameters:
doc
- id of the documentmatchedTokens
- tokens found in the queryprefixToken
- unfinished token in the query- Returns:
- the coefficient
- Throws:
java.io.IOException
- If there are problems reading term vectors from the underlying Lucene index.
-
calculateCoefficient
protected double calculateCoefficient(int position)
Calculate the weight coefficient based on the position of the first matching word. Subclass should override it to adapt it to particular needs- Parameters:
position
- of the first matching word in text- Returns:
- the coefficient
-
-