Class Util

java.lang.Object
de.cxp.ocs.smartsuggest.util.Util

public class Util extends Object
  • Field Details

  • Constructor Details

    • Util

      public Util()
  • Method Details

    • commonChars

      public static double commonChars(@NonNull @NonNull Locale locale, @NonNull @NonNull String input, @NonNull @NonNull String target)
      Returns the common chars of an input string compared to an target string. The logic works directional, so it's important to always compare to the same target and change the input
      Parameters:
      locale - the locale used to any string normalizations
      input - the input string to compare against the target
      target - the target string
      Returns:
      a value between 1 and 0, where 1 means all chars are common and 0 means no chars are common.
    • getCommonPrefixComparator

      public static Comparator<Suggestion> getCommonPrefixComparator(Locale locale, String term)

      Returns the Comparator that orders suggestion according to their common prefix to the given 'term'. Queries with a longer common prefix are preferred.

      Parameters:
      locale - the locale of the client. Used to load the proper stopwords
      term - the term for which to get suggestions
      Returns:
      the Comparator
    • getCommonPrefixLength

      public static int getCommonPrefixLength(String a, String b)
    • getCommonCharsComparator

      public static Comparator<Suggestion> getCommonCharsComparator(Locale locale, String term)

      Returns the Comparator that orders suggestion with according to their common chars to the given 'term'. Queries with more common chars are preferred.

      Parameters:
      locale - the locale of the client. Used to load the proper stopwords
      term - the term for which to get suggestions
      Returns:
      the Comparator
    • getDescendingWeightComparator

      public static Comparator<Suggestion> getDescendingWeightComparator()
      Returns the Comparator used to sort suggestions by their weight, where queries of the group "sharpened matches" are preferred over all others (=max weight).
      Returns:
      the Comparator for sharpened suggestions.
    • getTopKFuzzySuggestionCollector

      public static Collector<org.apache.lucene.search.suggest.Lookup.LookupResult,?,List<org.apache.lucene.search.suggest.Lookup.LookupResult>> getTopKFuzzySuggestionCollector(int topK, Locale locale, String inputTerm)
      Collector to get the first N suggestions by this order:
      • First prefer the ones with more common prefix with the input term
      • Then prefer the ones with more common chars with the input term
      • Finally prefer the ones with higher weight
      Parameters:
      topK - amount of suggestions to collect
      locale - locale
      inputTerm - term of the user
      Returns:
      a suggestion collector
    • getFuzzySuggestionsComparator

      public static Comparator<Suggestion> getFuzzySuggestionsComparator(Locale locale, String inputTerm)
      Retrieve comparator to optimally order fuzzy suggestions:
      • First prefer the ones with more common prefix with the input term
      • Then prefer the ones with more common chars with the input term
      • Then prefer the ones with higher weight
      • Finally do natural sorting to get consistent ordering
      Parameters:
      locale -
      inputTerm -
      Returns:
    • getDefaultComparator

      public static Comparator<Suggestion> getDefaultComparator(Locale locale, String inputTerm)
      Retrieve comparator to order suggestions within a group:
      • First prefer the ones with higher weight
      • Then prefer the ones with more common chars with the input term
      • Finally do natural sorting to get consistent ordering
      Parameters:
      locale -
      inputTerm -
      Returns: