Class 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.
    • 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
    • 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 precisions
        numFactor - Factor to multiply the number of searched elements before ponderate
        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.
        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 precisions
        numFactor - Factor to multiply the number of searched elements before ponderate
        exponent - exponent used only when blenderType is BlenderType.POSITION_EXPONENTIAL_RECIPROCAL
        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.
        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 class AnalyzingInfixSuggester
        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 match
        onlyMorePopular - return only more popular results
        num - 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 class AnalyzingInfixSuggester
        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 class AnalyzingInfixSuggester
        Throws:
        java.io.IOException
      • 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 (the prefixToken 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 class AnalyzingInfixSuggester
        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 in
        result - the result we try to add
        num - 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 document
        matchedTokens - tokens found in the query
        prefixToken - 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