Class SolrQueryUtil


  • public class SolrQueryUtil
    extends Object
    Utilities for
    • Field Detail

      • QUERY_ESCAPE_CHARACTERS

        protected static final List<String> QUERY_ESCAPE_CHARACTERS
    • Method Detail

      • sanitizeQuery

        public static String sanitizeQuery​(String query)
        Escape special Solr characters in query.
        Parameters:
        query - Original query string
        Returns:
        Query with special characters escaped by backslash
      • buildQuery

        public static String buildQuery​(String field,
                                        Object value)
        Create query for given field to have given value.
        Parameters:
        field - Solr field name with type
        value - Value to query for
        Returns:
        Query for given field to have given value
      • buildQuery

        public static String buildQuery​(String field,
                                        Collection<String> values)
        Create query for given field to have one of the given values. Values are combined with OR operator.
        Parameters:
        field - Solr field name with type
        values - List of values to query for
        Returns:
        Query for field to have one of the given values
      • buildPhraseQueryWithSlop

        public static String buildPhraseQueryWithSlop​(@Nullable
                                                      String field,
                                                      String query,
                                                      String slop)
        Creates a query with phrase slop.

           field:"query"~slop
         
        If a value for field is not passed in, it is ignored, and
         "query"~slop
         
        is returned.
        Parameters:
        field - The field to prepend to this query. Optional
        query - The query to use, optionally wrapped in quotes.
        slop - The phrase slop
        Returns:
        A query string with phrase slop added
      • buildRangeQuery

        public static String buildRangeQuery​(String field,
                                             @Nullable
                                             String min,
                                             @Nullable
                                             String max)
        Create inclusive range query for given field to be within the given range. If blank, the range ends will be replaced by "*".
        Parameters:
        field - Solr field name with type
        min - Lower bound of range (inclusive)
        max - Upper bound of range (inclusive)
        Returns:
        Query for field to be within the given range, inclusive
      • buildRangeQuery

        public static String buildRangeQuery​(String field,
                                             @Nullable
                                             String min,
                                             @Nullable
                                             String max,
                                             boolean inclusive)
        Create query for given field to be within the given range. If blank, the range ends will be replaced by "*".
        Parameters:
        field - Solr field name with type
        min - Lower bound of range (inclusive)
        max - Upper bound of range (inclusive)
        Returns:
        Query for field to be within the given range
      • buildRange

        public static String buildRange​(@Nullable
                                        String min,
                                        @Nullable
                                        String max,
                                        boolean inclusive)
        Create range value for given field to be within the given range. If blank, the range ends will be replaced by "*". Arguments min and max will be automatically sanitized.
        Parameters:
        min - Lower bound of range
        max - Upper bound of range
        inclusive - Whether this is an inclusive query or not.
        Returns:
        Value for field to be within the given range
        See Also:
        buildRange(String, String, boolean, boolean)
      • buildRange

        public static String buildRange​(@Nullable
                                        String min,
                                        @Nullable
                                        String max,
                                        boolean inclusive,
                                        boolean sanitize)
        Create range value for given field to be within the given range. If blank, the range ends will be replaced by "*".
        Parameters:
        min - Lower bound of range
        max - Upper bound of range
        inclusive - Whether this is an inclusive query or not.
        sanitize - Whether the arguments from min and max should be sanitized.
        Returns:
        Value for field to be within the given range
      • buildDateRange

        public static String buildDateRange​(String fieldName,
                                            @Nullable
                                            Instant from,
                                            @Nullable
                                            Instant to,
                                            boolean inclusive)
        Given two instants in time, build a range query between the two instants. The values will be in a format that Solr accepts. Specifically "YYYY-MM-DDThh:mm:ssZ". If
         null
         
        is passed for either of the instants, "*" is substituted, which indicates either the earliest or latest representable point of time. The range returned will be properly sanitized for consumption by Solr. See the Solr Documentation on working with dates for more information.
        Parameters:
        from - The from instant
        to - The to instant
        inclusive - Whether this is an inclusive date range or not.
        Returns:
        A string in the format of fieldName:[dateString TO dateString]
      • instantToDateString

        public static String instantToDateString​(@Nullable
                                                 Instant instant)
        Converts an Instant to a date string usable by Solr. The instant will be truncated to millisecond precision.
        Parameters:
        instant - The instant to convert to a date string consumable by Solr.
        Returns:
        A date string consumable by Solr.
      • buildInterval

        public static String buildInterval​(String min,
                                           String max)
        Create expression for interval for min and max. If blank, the range ends will be replaced by "*". Ranges are inclusive.
        Parameters:
        min - Lower bound of range (inclusive)
        max - Upper bound of range (inclusive)
        Returns:
        Query for field to be within the given range
      • getIntervalMin

        @Nullable
        public static String getIntervalMin​(String interval)
        Retrieve the lower bound value for the given inclusive interval. NULL if there is no bound.
        Parameters:
        interval - The interval to get the min from, format [min,max]
        Returns:
        The lower bound value for the interval
      • getIntervalMax

        @Nullable
        public static String getIntervalMax​(String interval)
        Retrieve the upper bound value for the given inclusive interval. NULL if there is no bound.
        Parameters:
        interval - The interval to get the max from, format [min,max]
        Returns:
        The upper bound value for the interval
      • buildExclusion

        public static String buildExclusion​(String field)
        Create an exclusion local parameter for the given field. Use this in conjunction with buildTag(String) to exclude filters when doing multi-select faceting.
        Parameters:
        field - The field to create an exclusion for
        Returns:
        The exclusion to prepend on the Solr query parameter
        See Also:
        Solr Faceting: Tagging and Excluding Filters
      • buildLocalParameter

        public static String buildLocalParameter​(String parameter,
                                                 String value)
        Create a local parameter to be associated with a Solr query parameter
        Parameters:
        value - The value the local parameter should be given
        parameter - The name of the local parameter to create
        Returns:
        The local parameter to prepend on Solr query parameter
      • buildLocalParameter

        public static String buildLocalParameter​(@Nullable
                                                 String type,
                                                 Map<String,​String> params)
        Create a local parameter to be included in a Solr query
        Parameters:
        type - The type of local parameter
        params - Map of local parameters to include
        Returns:
        The local parameter to prepend on Solr query parameter
      • or

        public static String or​(List<String> values)
        Combine the provided values in an OR statement.
        Parameters:
        values - Values to be OR'd
        Returns:
        OR'd values
      • and

        public static String and​(List<String> values)
        Combine the provided values in an AND statement.
        Parameters:
        values - Values to be AND'd
        Returns:
        AND'd values
      • conjoin

        public static String conjoin​(List<String> values,
                                     String conjunction)
        Combine the provided values with conjunction.
        Parameters:
        values - Values to be conjoined
        conjunction - Conjunction to join values
        Returns:
        Conjoined values
      • inValues

        public static String inValues​(Collection<String> values)
        Combine the provided values to be used in an "in" query.

        For example, "(1 2 3)"

        Parameters:
        values - Values to combine
        Returns:
        Combined values
      • logQuery

        public static void logQuery​(org.apache.solr.client.solrj.SolrQuery solrQuery)
        Logs a Solr query to debug output.
        Parameters:
        solrQuery - The query to log
      • negate

        public static String negate​(String query)
        Build a negated version of the given query.
        Parameters:
        query - The query to negate
        Returns:
        The negated query
      • applyBoost

        public static String applyBoost​(String field,
                                        @Nullable
                                        Double boost)
        Format the provided field and boost as a boosted field for Solr.
        Parameters:
        field - the field to boost
        boost - the boost to apply
        Returns:
        boosted Solr field in the format "field^boost"
      • toJsonString

        public static String toJsonString​(org.apache.solr.client.solrj.response.QueryResponse queryResponse)
        Converts a query response into a JSON responses.

        WARNING: Calls to this method should reserved for debug logging only, and guarded against with log.isDebugEnabled

        Parameters:
        queryResponse - The query response from Solr
        Returns:
        A JSON string representation of the response.
      • buildFullOriginalQueryStr

        @Nullable
        public static String buildFullOriginalQueryStr​(org.apache.solr.client.solrj.response.QueryResponse queryResponse)
        Based on the response, builds the original query String sent to Solr. This allows easy copy/pasting of the full query used for diagnostic purposes.

        INFO: This is not efficient and not intended for production. Calls to this method should reserved for debug logging only, and guarded against with log.isDebugEnabled

        Parameters:
        queryResponse - The QueryResponse
        Returns:
        The full query used, or null if unable to build