Class SolrQueryUtil
java.lang.Object
com.broadleafcommerce.search.provider.solr.util.SolrQueryUtil
Utilities for
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic String
Combine the provided values in an AND statement.static String
applyBoost
(String field, Double boost) Format the provided field and boost as a boosted field for Solr.static String
buildDateRange
(String fieldName, Instant from, Instant to, boolean inclusive) Given two instants in time, build a range query between the two instants.static String
buildExclusion
(String field) Create an exclusion local parameter for the given field.static String
buildFullOriginalQueryStr
(org.apache.solr.client.solrj.response.QueryResponse queryResponse) Based on the response, builds the original query String sent to Solr.static String
buildInterval
(String min, String max) Create expression for interval for min and max.static String
buildLocalParameter
(String parameter, String value) Create a local parameter to be associated with a Solr query parameterstatic String
buildLocalParameter
(String type, Map<String, String> params) Create a local parameter to be included in a Solr querystatic String
buildPhraseQueryWithSlop
(String field, String query, String slop) Creates a query with phrase slop.static String
buildQuery
(String field, Object value) Create query for given field to have given value.static String
buildQuery
(String field, Collection<String> values) Create query for given field to have one of the given values.static String
buildRange
(String min, String max, boolean inclusive) Create range value for given field to be within the given range.static String
buildRange
(String min, String max, boolean inclusive, boolean sanitize) Create range value for given field to be within the given range.static String
buildRangeQuery
(String field, String min, String max) Create inclusive range query for given field to be within the given range.static String
buildRangeQuery
(String field, String min, String max, boolean inclusive) Create query for given field to be within the given range.static String
Create a tag local parameter for the given field.static String
Combine the provided values with conjunction.static String
getIntervalMax
(String interval) Retrieve the upper bound value for the given inclusive interval.static String
getIntervalMin
(String interval) Retrieve the lower bound value for the given inclusive interval.static String
instantToDateString
(Instant instant) Converts an Instant to a date string usable by Solr.static String
inValues
(Collection<String> values) Combine the provided values to be used in an "in" query.static void
logQuery
(org.apache.solr.client.solrj.SolrQuery solrQuery) Logs a Solr query to debug output.static String
Build a negated version of the given query.static String
Combine the provided values in an OR statement.static String
sanitizeQuery
(String query) Escape special Solr characters in query.static String
toJsonString
(org.apache.solr.client.solrj.response.QueryResponse queryResponse) Converts a query response into a JSON responses.
-
Field Details
-
QUERY_ESCAPE_CHARACTERS
-
-
Method Details
-
sanitizeQuery
Escape special Solr characters in query.- Parameters:
query
- Original query string- Returns:
- Query with special characters escaped by backslash
-
buildQuery
Create query for given field to have given value.- Parameters:
field
- Solr field name with typevalue
- Value to query for- Returns:
- Query for given field to have given value
-
buildQuery
Create query for given field to have one of the given values. Values are combined with OR operator.- Parameters:
field
- Solr field name with typevalues
- List of values to query for- Returns:
- Query for field to have one of the given values
-
buildPhraseQueryWithSlop
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. Optionalquery
- The query to use, optionally wrapped in quotes.slop
- The phrase slop- Returns:
- A query string with phrase slop added
-
buildRangeQuery
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 typemin
- 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 typemin
- Lower bound of range (inclusive)max
- Upper bound of range (inclusive)- Returns:
- Query for field to be within the given range
-
buildRange
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 rangemax
- Upper bound of rangeinclusive
- Whether this is an inclusive query or not.- Returns:
- Value for field to be within the given range
- See Also:
-
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 rangemax
- Upper bound of rangeinclusive
- 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". Ifnull
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 instantto
- The to instantinclusive
- Whether this is an inclusive date range or not.- Returns:
- A string in the format of fieldName:[dateString TO dateString]
-
instantToDateString
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
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
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
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
-
buildTag
Create a tag local parameter for the given field. Use this in conjunction withbuildExclusion(String)
to exclude filters when doing multi-select faceting.- Parameters:
field
- The field to create a tag for- Returns:
- The tag to prepend on the Solr query parameter
- See Also:
-
buildExclusion
Create an exclusion local parameter for the given field. Use this in conjunction withbuildTag(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:
-
buildLocalParameter
Create a local parameter to be associated with a Solr query parameter- Parameters:
value
- The value the local parameter should be givenparameter
- The name of the local parameter to create- Returns:
- The local parameter to prepend on Solr query parameter
-
buildLocalParameter
Create a local parameter to be included in a Solr query- Parameters:
type
- The type of local parameterparams
- Map of local parameters to include- Returns:
- The local parameter to prepend on Solr query parameter
-
or
Combine the provided values in an OR statement.- Parameters:
values
- Values to be OR'd- Returns:
- OR'd values
-
and
Combine the provided values in an AND statement.- Parameters:
values
- Values to be AND'd- Returns:
- AND'd values
-
conjoin
Combine the provided values with conjunction.- Parameters:
values
- Values to be conjoinedconjunction
- Conjunction to join values- Returns:
- Conjoined values
-
inValues
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
Build a negated version of the given query.- Parameters:
query
- The query to negate- Returns:
- The negated query
-
applyBoost
Format the provided field and boost as a boosted field for Solr.- Parameters:
field
- the field to boostboost
- 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
-