Package ubic.gemma.core.search.lucene
Class LuceneQueryUtils
- java.lang.Object
-
- ubic.gemma.core.search.lucene.LuceneQueryUtils
-
public class LuceneQueryUtils extends Object
Utilities for parsing search queries using Lucene.- Author:
- poirigui
-
-
Constructor Summary
Constructors Constructor Description LuceneQueryUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
escape(String query)
Escape all reserved Lucene characters in the given query.static Set<String>
extractTerms(SearchSettings settings)
Extract terms, regardless of their logical organization.static Set<Set<String>>
extractTermsDnf(SearchSettings settings)
static Set<Set<String>>
extractTermsDnf(SearchSettings settings, boolean allowWildcards)
Extract a DNF (Disjunctive Normal Form) from the terms of a query.static boolean
isWildcard(SearchSettings settings)
Check if the query is a wildcard query.static org.apache.lucene.search.Query
parseSafely(String query, org.apache.lucene.queryParser.QueryParser queryParser, Consumer<? super org.apache.lucene.queryParser.ParseException> report)
Safely parse the given search query into a Lucene query, falling back on a query with special characters escaped if necessary.static org.apache.lucene.search.Query
parseSafely(SearchSettings settings, org.apache.lucene.queryParser.QueryParser queryParser)
Safely parse the given search settings into a Lucene query, falling back on a query with special characters escaped if necessary.static String
prepareDatabaseQuery(String query, boolean allowWildcards)
static String
prepareDatabaseQuery(SearchSettings settings)
Escape the query for a database match.static String
prepareDatabaseQuery(SearchSettings settings, boolean allowWildcards)
Obtain a query suitable for a database match.static URI
prepareTermUriQuery(String s)
static URI
prepareTermUriQuery(SearchSettings settings)
static String
quote(String query)
Quote the given Lucene query to be used for an exact match.
-
-
-
Method Detail
-
parseSafely
public static org.apache.lucene.search.Query parseSafely(SearchSettings settings, org.apache.lucene.queryParser.QueryParser queryParser) throws SearchException
Safely parse the given search settings into a Lucene query, falling back on a query with special characters escaped if necessary.- Throws:
SearchException
-
parseSafely
public static org.apache.lucene.search.Query parseSafely(String query, org.apache.lucene.queryParser.QueryParser queryParser, @Nullable Consumer<? super org.apache.lucene.queryParser.ParseException> report) throws SearchException
Safely parse the given search query into a Lucene query, falling back on a query with special characters escaped if necessary.- Parameters:
report
- a consumer for potentialParseException
when attempting to parse the query, ignored if null- Throws:
SearchException
-
escape
public static String escape(String query)
Escape all reserved Lucene characters in the given query.
-
extractTerms
public static Set<String> extractTerms(SearchSettings settings) throws SearchException
Extract terms, regardless of their logical organization.Prohibited terms are excluded.
- Throws:
SearchException
-
extractTermsDnf
public static Set<Set<String>> extractTermsDnf(SearchSettings settings) throws SearchException
- Throws:
SearchException
-
extractTermsDnf
public static Set<Set<String>> extractTermsDnf(SearchSettings settings, boolean allowWildcards) throws SearchException
Extract a DNF (Disjunctive Normal Form) from the terms of a query.Clauses can be nested (i.e.
a OR (d OR (c AND (d AND e))
) as long asOR
andAND
are not interleaved.Prohibited clauses are ignored unless they break the DNF structure, in which case this will return an empty set.
- Parameters:
allowWildcards
- allowPrefixQuery
andWildcardQuery
clauses- Throws:
SearchException
-
prepareDatabaseQuery
@Nullable public static String prepareDatabaseQuery(SearchSettings settings) throws SearchException
Escape the query for a database match.- Throws:
SearchException
- See Also:
prepareDatabaseQuery(SearchSettings, boolean)
-
prepareDatabaseQuery
@Nullable public static String prepareDatabaseQuery(SearchSettings settings, boolean allowWildcards) throws SearchException
Obtain a query suitable for a database match.This method will return the first global term in the query that is not prohibited. If
allowWildcards
is set to true, prefix and wildcard terms will be considered as well.The resulting string is free from character that would usually be used for a free-text match unless
allowWildcards
is set to true.- Parameters:
allowWildcards
- if true, wildcards are supported (i.e. '*' and '?') and translated to their corresponding LIKE SQL syntax (i.e. '%' and '_'), all other special characters are escaped.- Returns:
- the first suitable term in the query, or null if none of them are applicable for a database query
- Throws:
SearchException
-
prepareDatabaseQuery
@Nullable public static String prepareDatabaseQuery(String query, boolean allowWildcards) throws SearchException
- Throws:
SearchException
-
prepareTermUriQuery
@Nullable public static URI prepareTermUriQuery(String s) throws SearchException
- Throws:
SearchException
-
prepareTermUriQuery
@Nullable public static URI prepareTermUriQuery(SearchSettings settings) throws SearchException
- Throws:
SearchException
-
isWildcard
public static boolean isWildcard(SearchSettings settings)
Check if the query is a wildcard query.
-
-