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:
Closeable
,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.- WARNING: This API is experimental and might change in incompatible ways in the next release.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
BlendedInfixSuggester.BlenderType
The different types of blender.-
Nested classes/interfaces inherited from class org.apache.lucene.search.suggest.Lookup
Lookup.LookupPriorityQueue, Lookup.LookupResult
-
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_NUM_FACTOR
Default factorprotected static double
LINEAR_COEF
Coefficient used for linear blending-
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, 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, 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 Instance Methods Concrete Methods Modifier and Type Method Description protected double
calculateCoefficient(int position)
Calculate the weight coefficient based on the position of the first matching word.protected List<Lookup.LookupResult>
createResults(IndexSearcher searcher, TopFieldDocs hits, int num, CharSequence key, boolean doHighlight, Set<String> matchedTokens, 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.List<Lookup.LookupResult>
lookup(CharSequence key, 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
).List<Lookup.LookupResult>
lookup(CharSequence key, Set<BytesRef> contexts, boolean onlyMorePopular, int num)
Look up a key and return possible completion for this key.List<Lookup.LookupResult>
lookup(CharSequence key, Set<BytesRef> contexts, int num, boolean allTermsRequired, boolean doHighlight)
Lookup, with context but without booleans.List<Lookup.LookupResult>
lookup(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
-
-
-
-
Constructor Detail
-
BlendedInfixSuggester
public BlendedInfixSuggester(Directory dir, Analyzer analyzer) throws IOException
Create a new instance, loading from a previously built directory, if it exists.- Throws:
IOException
-
BlendedInfixSuggester
public BlendedInfixSuggester(Directory dir, Analyzer indexAnalyzer, Analyzer queryAnalyzer, int minPrefixChars, BlendedInfixSuggester.BlenderType blenderType, int numFactor, boolean commitOnBuild) throws 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:
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, Double exponent, boolean commitOnBuild, boolean allTermsRequired, boolean highlight) throws 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:
IOException
- If there are problems opening the underlying Lucene index.
-
-
Method Detail
-
lookup
public List<Lookup.LookupResult> lookup(CharSequence key, Set<BytesRef> contexts, boolean onlyMorePopular, int num) throws 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:
IOException
-
lookup
public List<Lookup.LookupResult> lookup(CharSequence key, Set<BytesRef> contexts, int num, boolean allTermsRequired, boolean doHighlight) throws 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:
IOException
-
lookup
public List<Lookup.LookupResult> lookup(CharSequence key, Map<BytesRef,BooleanClause.Occur> contextInfo, int num, boolean allTermsRequired, boolean doHighlight) throws 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:
IOException
-
lookup
public List<Lookup.LookupResult> lookup(CharSequence key, BooleanQuery contextQuery, int num, boolean allTermsRequired, boolean doHighlight) throws 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:
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 List<Lookup.LookupResult> createResults(IndexSearcher searcher, TopFieldDocs hits, int num, CharSequence key, boolean doHighlight, Set<String> matchedTokens, String prefixToken) throws 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:
IOException
- If there are problems reading fields 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
-
-