Class QueryUtils


  • public class QueryUtils
    extends Object
    • Constructor Summary

      Constructors 
      Constructor Description
      QueryUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static org.apache.lucene.search.BooleanQuery build​(org.apache.lucene.search.BooleanQuery.Builder builder, QParser parser)  
      static org.apache.lucene.search.Query combineQueryAndFilter​(org.apache.lucene.search.Query scoreQuery, org.apache.lucene.search.Query filterQuery)
      Combines a scoring query with a non-scoring (filter) query.
      static void ensurePrefixQueryObeysMinimumPrefixLength​(QParser parser, org.apache.lucene.search.Query query, String prefix)
      Validates that a provided prefix query obeys any limits (if configured) on the minimum allowable prefix size
      static org.apache.lucene.search.Query fixNegativeQuery​(org.apache.lucene.search.Query q)
      Fixes a negative query by adding a MatchAllDocs query clause.
      static org.apache.lucene.search.Query getAbs​(org.apache.lucene.search.Query q)
      Returns the original query if it was already a positive query, otherwise return the negative of the query (i.e., a positive query).
      static Set<org.apache.lucene.search.Query> getTaggedQueries​(SolrQueryRequest req, Collection<String> desiredTags)
      Returns a Set containing all of the Queries in the designated SolrQueryRequest possessing a tag in the provided list of desired tags.
      static boolean isConstantScoreQuery​(org.apache.lucene.search.Query q)
      Recursively unwraps the specified query to determine whether it is capable of producing a score that varies across different documents.
      static boolean isNegative​(org.apache.lucene.search.Query q)
      return true if this query has no positive components
      static org.apache.lucene.search.Query makeQueryable​(org.apache.lucene.search.Query q)
      Makes negative queries suitable for querying by lucene.
      static List<org.apache.lucene.search.Query> parseFilterQueries​(SolrQueryRequest req)
      Parse the filter queries in Solr request
    • Field Detail

      • NO_PREFIX_QUERY_LENGTH_LIMIT

        public static final int NO_PREFIX_QUERY_LENGTH_LIMIT
        See Also:
        Constant Field Values
    • Constructor Detail

      • QueryUtils

        public QueryUtils()
    • Method Detail

      • isNegative

        public static boolean isNegative​(org.apache.lucene.search.Query q)
        return true if this query has no positive components
      • isConstantScoreQuery

        public static boolean isConstantScoreQuery​(org.apache.lucene.search.Query q)
        Recursively unwraps the specified query to determine whether it is capable of producing a score that varies across different documents. Returns true if this query is not capable of producing a varying score (i.e., it is a constant score query).
      • ensurePrefixQueryObeysMinimumPrefixLength

        public static void ensurePrefixQueryObeysMinimumPrefixLength​(QParser parser,
                                                                     org.apache.lucene.search.Query query,
                                                                     String prefix)
        Validates that a provided prefix query obeys any limits (if configured) on the minimum allowable prefix size

        The limit is retrieved from the provided QParser (see QParser.getPrefixQueryMinPrefixLength() for the default implementation).

        Parameters:
        parser - the QParser used to parse the query being validated. No limit will be enforced if 'null'
        query - the query to validate. Limits will only be enforced if this is a PrefixQuery
        prefix - a String term included in the provided query. Its size is compared against the configured limit
      • getAbs

        public static org.apache.lucene.search.Query getAbs​(org.apache.lucene.search.Query q)
        Returns the original query if it was already a positive query, otherwise return the negative of the query (i.e., a positive query).

        Example: both id:10 and id:-10 will return id:10

        The caller can tell the sign of the original by a reference comparison between the original and returned query.

        Parameters:
        q - Query to create the absolute version of
        Returns:
        Absolute version of the Query
      • makeQueryable

        public static org.apache.lucene.search.Query makeQueryable​(org.apache.lucene.search.Query q)
        Makes negative queries suitable for querying by lucene.
      • fixNegativeQuery

        public static org.apache.lucene.search.Query fixNegativeQuery​(org.apache.lucene.search.Query q)
        Fixes a negative query by adding a MatchAllDocs query clause. The query passed in *must* be a negative query.
      • build

        public static org.apache.lucene.search.BooleanQuery build​(org.apache.lucene.search.BooleanQuery.Builder builder,
                                                                  QParser parser)
        WARNING: This API is experimental and might change in incompatible ways in the next release.
        throw exception if max boolean clauses are exceeded
      • combineQueryAndFilter

        public static org.apache.lucene.search.Query combineQueryAndFilter​(org.apache.lucene.search.Query scoreQuery,
                                                                           org.apache.lucene.search.Query filterQuery)
        Combines a scoring query with a non-scoring (filter) query. If both parameters are null then return a MatchAllDocsQuery. If only scoreQuery is present then return it. If only filterQuery is present then return it wrapped with constant scoring. If neither are null then we combine with a BooleanQuery.
      • parseFilterQueries

        public static List<org.apache.lucene.search.Query> parseFilterQueries​(SolrQueryRequest req)
                                                                       throws SyntaxError
        Parse the filter queries in Solr request
        Parameters:
        req - Solr request
        Returns:
        and array of Query. If the request does not contain filter queries, returns an empty list.
        Throws:
        SyntaxError - if an error occurs during parsing
      • getTaggedQueries

        public static Set<org.apache.lucene.search.Query> getTaggedQueries​(SolrQueryRequest req,
                                                                           Collection<String> desiredTags)
        Returns a Set containing all of the Queries in the designated SolrQueryRequest possessing a tag in the provided list of desired tags. The Set uses reference equality so, for example, it will have 2 elements if the caller requests the tag "t1" for a request where "fq={!tag=t1}a:b&fq={!tag=t1}a:b". The Set will be empty (not null) if there are no matches.

        This method assumes that the provided SolrQueryRequest's context has been populated with a "tags" entry, which should be a Map from a tag name to a Collection of QParsers. In general, the "tags" entry will not be present until the QParsers have been instantiated, for example via QueryComponent.prepare()

        Parameters:
        req - Solr request
        desiredTags - the tags to look for
        Returns:
        Set of Queries in the given SolrQueryRequest possessing any of the desiredTags