Package fr.becpg.repo.helper
Class BeCPGQueryHelper
java.lang.Object
fr.becpg.repo.helper.BeCPGQueryHelper
BeCPGQueryHelper class.
- Version:
- $Id: $Id
- Author:
- matthieu
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final recordA suggestion with its relevance rank, computed once before sorting. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final PatternCombining marks left by the Unicode decomposition of an accented letter.private static final PatternA Han ideograph is a word on its own, Chinese and Japanese being written without spaces.French ligatures, which Unicode decomposition does not split.private static final org.apache.commons.logging.LogConstantloggerprivate static final PatternAnything but a letter or a digit separates two words of a search.private static final intRelevance ranks of a suggestion label, best first.private static final intprivate static final intprivate static final intprivate static final PatternSpaces between the words of a separated text.static final StringConstantSUFFIX_ALL="*"private static final StringConstantSUFFIX_DOUBLE_QUOTE="\""private static final StringConstantSUFFIX_SIMPLE_QUOTE="'"private static final StringConstantSUFFIX_SPACE=" "private static final PatternQuotes around a typed query and its closing wildcard, which are search syntax, not part of the searched label. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate 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.private static StringFolds a typed query for a label comparison, without its surrounding quotes nor its closing wildcard.private static StringLower-cases a text and strips its accents and ligatures, for a plain substring comparison.private static booleanhasWordStartingWith(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.static booleanisAllQuery(String query) isAllQuery.static booleanisQueryMatch(String query, String entityName) Tells whether a name matches a free-text query.static StringprepareQuery(String query) prepareQuery.static StringprepareQueryForSorting(String query) prepareQueryForSorting.private static intRanks a label against a folded query.private static booleanremoveWordStartingWith(List<SearchWord> words, SearchWord prefix) Removes the first word started by the given typed word.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.private static booleanstartsDistinctWords(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.private static StringunfoldLigatures(String text) Writes the French ligatures as two letters, which Unicode decomposition leaves untouched.
-
Field Details
-
logger
private static final org.apache.commons.logging.Log loggerConstantlogger -
SUFFIX_ALL
ConstantSUFFIX_ALL="*"- See Also:
-
SUFFIX_SPACE
ConstantSUFFIX_SPACE=" "- See Also:
-
SUFFIX_DOUBLE_QUOTE
ConstantSUFFIX_DOUBLE_QUOTE="\""- See Also:
-
SUFFIX_SIMPLE_QUOTE
ConstantSUFFIX_SIMPLE_QUOTE="'"- See Also:
-
NON_WORD_CHARACTERS
Anything but a letter or a digit separates two words of a search. -
COMBINING_MARKS
Combining marks left by the Unicode decomposition of an accented letter. -
HAN_CHARACTER
A Han ideograph is a word on its own, Chinese and Japanese being written without spaces. -
SPACES
Spaces between the words of a separated text. -
LIGATURES
French ligatures, which Unicode decomposition does not split. -
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_EXACTRelevance 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
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 everythingentityName- the name to test- Returns:
- true when the name matches the query
-
extractSearchWords
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
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 wordsnameWords- the analysed name words- Returns:
- true when every query word found a name word
-
removeWordStartingWith
Removes the first word started by the given typed word.- Parameters:
words- the words to search, modified in placeprefix- the typed word- Returns:
- true when a word was found and removed
-
foldText
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
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
prepareQueryForSorting.
-
prepareQuery
prepareQuery.
-
isAllQuery
isAllQuery.
- Parameters:
query- aStringobject.- Returns:
- a boolean.
-
sortByRelevance
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 useritems- the suggestions, in the order of the search engine; left untouchedlabelOf- reads the label of a suggestion, may return null- Returns:
- the sorted suggestions, or
itemswhen there is nothing to sort
-
foldQuery
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
Ranks a label against a folded query.- Parameters:
foldedQuery- the folded query, not emptylabel- the label, may be null- Returns:
- the relevance rank, lower is better
-
hasWordStartingWith
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 labelfoldedQuery- the folded query- Returns:
- true when a word of the label starts with the query
-