Class BeCPGQueryHelper

java.lang.Object
fr.becpg.repo.helper.BeCPGQueryHelper

public class BeCPGQueryHelper extends Object

BeCPGQueryHelper class.

Version:
$Id: $Id
Author:
matthieu
  • Field Details

    • logger

      private static final org.apache.commons.logging.Log logger
      Constant logger
    • SUFFIX_ALL

      public static final String SUFFIX_ALL
      Constant SUFFIX_ALL="*"
      See Also:
    • SUFFIX_SPACE

      private static final String SUFFIX_SPACE
      Constant SUFFIX_SPACE=" "
      See Also:
    • SUFFIX_DOUBLE_QUOTE

      private static final String SUFFIX_DOUBLE_QUOTE
      Constant SUFFIX_DOUBLE_QUOTE="\""
      See Also:
    • SUFFIX_SIMPLE_QUOTE

      private static final String SUFFIX_SIMPLE_QUOTE
      Constant SUFFIX_SIMPLE_QUOTE="'"
      See Also:
    • NON_WORD_CHARACTERS

      private static final Pattern NON_WORD_CHARACTERS
      Anything but a letter or a digit separates two words of a search.
    • COMBINING_MARKS

      private static final Pattern COMBINING_MARKS
      Combining marks left by the Unicode decomposition of an accented letter.
    • HAN_CHARACTER

      private static final Pattern HAN_CHARACTER
      A Han ideograph is a word on its own, Chinese and Japanese being written without spaces.
    • SPACES

      private static final Pattern SPACES
      Spaces between the words of a separated text.
    • LIGATURES

      private static final Map<String,String> LIGATURES
      French ligatures, which Unicode decomposition does not split.
    • SURROUNDING_QUERY_SYNTAX

      private static final Pattern SURROUNDING_QUERY_SYNTAX
      Quotes around a typed query and its closing wildcard, which are search syntax, not part of the searched label.
    • RANK_EXACT

      private static final int RANK_EXACT
      Relevance ranks of a suggestion label, best first.
      See Also:
    • RANK_PREFIX

      private static final int RANK_PREFIX
      See Also:
    • RANK_WORD_PREFIX

      private static final int RANK_WORD_PREFIX
      See Also:
    • RANK_OTHER

      private static final int RANK_OTHER
      See Also:
  • Constructor Details

    • BeCPGQueryHelper

      private BeCPGQueryHelper()

      Constructor for BeCPGQueryHelper.

  • Method Details

    • isQueryMatch

      public static boolean isQueryMatch(String query, String entityName)
      Tells whether a name matches a free-text query. Every word of the query must start a distinct word of the name, whatever the order, the case, the accents or the ligatures. Punctuation separates words, and no word is an operator: "AND", "OR" or "NOT" are searched as words. A query made of punctuation only is searched as a plain substring.
      Parameters:
      query - the text typed by the user; blank or "*" matches everything
      entityName - the name to test
      Returns:
      true when the name matches the query
    • extractSearchWords

      private static List<SearchWord> extractSearchWords(String text)
      Splits a text into the words the search compares: lower-cased, without accents nor ligatures, and cut on any character that is neither a letter nor a digit. No word is dropped, so that every typed word counts.
      Parameters:
      text - the text to split
      Returns:
      the words, possibly empty
    • startsDistinctWords

      private static boolean startsDistinctWords(List<SearchWord> queryWords, List<SearchWord> nameWords)
      Checks that each query word starts its own name word, so that a word typed twice must be found twice. Longer query words pick their name word first, to keep a short prefix from taking the only word a longer one fits.
      Parameters:
      queryWords - the analysed query words
      nameWords - the analysed name words
      Returns:
      true when every query word found a name word
    • removeWordStartingWith

      private static boolean removeWordStartingWith(List<SearchWord> words, SearchWord prefix)
      Removes the first word started by the given typed word.
      Parameters:
      words - the words to search, modified in place
      prefix - the typed word
      Returns:
      true when a word was found and removed
    • foldText

      private static String foldText(String text)
      Lower-cases a text and strips its accents and ligatures, for a plain substring comparison.
      Parameters:
      text - the text to fold
      Returns:
      the folded text
    • unfoldLigatures

      private static String unfoldLigatures(String text)
      Writes the French ligatures as two letters, which Unicode decomposition leaves untouched.
      Parameters:
      text - the text to unfold
      Returns:
      the text with œ, Œ, æ and Æ spelled out
    • prepareQueryForSorting

      public static String prepareQueryForSorting(String query)

      prepareQueryForSorting.

      Parameters:
      query - a String object
      Returns:
      a String object
    • prepareQuery

      public static String prepareQuery(String query)

      prepareQuery.

      Parameters:
      query - a String object.
      Returns:
      a String object.
    • isAllQuery

      public static boolean isAllQuery(String query)

      isAllQuery.

      Parameters:
      query - a String object.
      Returns:
      a boolean.
    • sortByRelevance

      public static <T> List<T> sortByRelevance(String query, List<T> items, Function<T,String> labelOf)
      Sorts suggestions by relevance to the typed query: the label equal to the query first, then the labels starting with it, then the labels having a word starting with it, then the others. Case, accents and ligatures are ignored. The sort is stable, so suggestions of the same rank keep the order of the search engine.
      Type Parameters:
      T - the suggestion type
      Parameters:
      query - the text typed by the user
      items - the suggestions, in the order of the search engine; left untouched
      labelOf - reads the label of a suggestion, may return null
      Returns:
      the sorted suggestions, or items when there is nothing to sort
    • foldQuery

      private static String foldQuery(String query)
      Folds a typed query for a label comparison, without its surrounding quotes nor its closing wildcard.
      Parameters:
      query - the typed query, may be null
      Returns:
      the folded query, empty when nothing is left to compare
    • rankLabel

      private static int rankLabel(String foldedQuery, String label)
      Ranks a label against a folded query.
      Parameters:
      foldedQuery - the folded query, not empty
      label - the label, may be null
      Returns:
      the relevance rank, lower is better
    • hasWordStartingWith

      private static boolean hasWordStartingWith(String foldedLabel, String foldedQuery)
      Tells whether the query starts a word of the label other than the first one, a word starting after any character that is neither a letter nor a digit.
      Parameters:
      foldedLabel - the folded label
      foldedQuery - the folded query
      Returns:
      true when a word of the label starts with the query