Class LuceneQueryUtils


  • public class LuceneQueryUtils
    extends Object
    Utilities for parsing search queries using Lucene.
    Author:
    poirigui
    • Constructor Detail

      • LuceneQueryUtils

        public LuceneQueryUtils()
    • 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)
                                                          throws SearchException
        Safely parse the given search query into a Lucene query, falling back on a query with special characters escaped if necessary.
        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 as OR and AND are not interleaved.

        Prohibited clauses are ignored unless they break the DNF structure, in which case this will return an empty set.

        Parameters:
        allowWildcards - allow PrefixQuery and WildcardQuery clauses
        Throws:
        SearchException
      • 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
      • isWildcard

        public static boolean isWildcard​(SearchSettings settings)
        Check if the query is a wildcard query.
      • quote

        public static String quote​(String query)
        Quote the given Lucene query to be used for an exact match.