Class SolrPluginUtils
- java.lang.Object
- 
- org.apache.solr.util.SolrPluginUtils
 
- 
 public class SolrPluginUtils extends Object Utilities that may be of use to RequestHandlers.
- 
- 
Nested Class SummaryNested Classes Modifier and Type Class Description static classSolrPluginUtils.DisjunctionMaxQueryParserA subclass of SolrQueryParser that supports aliasing fields for constructing DisjunctionMaxQueries.
 - 
Field SummaryFields Modifier and Type Field Description protected static StringUNKNOWN_VALUE
 - 
Constructor SummaryConstructors Constructor Description SolrPluginUtils()
 - 
Method SummaryAll Methods Static Methods Concrete Methods Modifier and Type Method Description static voidcopyNamedListIntoArrayByDocPosInResponse(org.apache.solr.common.util.NamedList<Object> namedList, Map<Object,ShardDoc> resultIds, Map.Entry<String,Object>[] destArr)Copies the givennamedListassumed to have doc uniqueKey keyed data intodestArrat the position of the document in the response.static DocListdoSimpleQuery(String sreq, SolrQueryRequest req, int start, int limit)Executes a basic querystatic org.apache.solr.common.util.NamedList<Object>doStandardDebug(SolrQueryRequest req, String userQuery, org.apache.lucene.search.Query query, DocList results, boolean dbgQuery, boolean dbgResults)Returns a NamedList containing many "standard" pieces of debugging information.static voiddoStandardQueryDebug(SolrQueryRequest req, String userQuery, org.apache.lucene.search.Query query, boolean dbgQuery, org.apache.solr.common.util.NamedList<Object> dbg)static voiddoStandardResultsDebug(SolrQueryRequest req, org.apache.lucene.search.Query query, DocList results, boolean dbgResults, org.apache.solr.common.util.NamedList<Object> dbg)static org.apache.solr.common.util.NamedList<org.apache.solr.common.util.NamedList<Object>>explanationsToNamedLists(org.apache.solr.common.util.NamedList<org.apache.lucene.search.Explanation> explanations)static org.apache.solr.common.util.NamedList<Object>explanationToNamedList(org.apache.lucene.search.Explanation e)static voidflattenBooleanQuery(org.apache.lucene.search.BooleanQuery.Builder to, org.apache.lucene.search.BooleanQuery from)Recursively walks the "from" query pulling out sub-queries and adding them to the "to" query.static voidforEachRequestPurpose(Integer reqPurpose, Consumer<String> consumer)Given the integer purpose of a request generates a readable value corresponding the request purposes (there can be more than one on a single request).static Set<String>getDebugInterests(String[] params, ResponseBuilder rb)static org.apache.solr.common.util.NamedList<org.apache.lucene.search.Explanation>getExplanations(org.apache.lucene.search.Query query, DocList docs, SolrIndexSearcher searcher, IndexSchema schema)Generates an NamedList of Explanations for each item in a list of docs.static StringgetRequestPurpose(Integer reqPurpose)Given the integer purpose of a request generates a readable value corresponding the request purposes (there can be more than one on a single request).static org.apache.lucene.search.SortgetSort(SolrQueryRequest req)Determines the correct Sort based on the request parameter "sort"static voidinvokeSetters(Object bean, Iterable<? extends Map.Entry<String,?>> initArgs)static voidinvokeSetters(Object bean, Iterable<? extends Map.Entry<String,?>> initArgs, boolean lenient)static intnumDocs(SolrIndexSearcher s, org.apache.lucene.search.Query q, org.apache.lucene.search.Query f)SolrIndexSearch.numDocs(Query,Query) freaks out if the filtering query is null, so we use this workarround.static voidoptimizePreFetchDocs(ResponseBuilder rb, DocList docs, org.apache.lucene.search.Query query, SolrQueryRequest req, SolrQueryResponse res)Pre-fetch documents into the index searcher's document cache.static Map<String,Float>parseFieldBoosts(String in)Given a string containing fieldNames and boost info, converts it to a Map from field name to boost info.static Map<String,Float>parseFieldBoosts(String[] fieldLists)LikeparseFieldBoosts(String), but parses all the strings in the provided array (which may be null).static List<FieldParams>parseFieldBoostsAndSlop(String[] fieldLists, int wordGrams, int defaultSlop)/** LikeparseFieldBoosts(java.lang.String), but allows for an optional slop value prefixed by "~".static List<org.apache.lucene.search.Query>parseQueryStrings(SolrQueryRequest req, String[] queries)Turns an array of query strings into a List of Query objects.static CharSequencepartialEscape(CharSequence s)Escapes all special characters except '"', '-', and '+'static <T> org.apache.solr.common.util.NamedList<T>removeNulls(Map.Entry<String,T>[] entries, org.apache.solr.common.util.NamedList<T> dest)Adds todestall the not-null elements ofentriesthat have non-null namesstatic voidsetDefaults(SolrQueryRequest req, org.apache.solr.common.params.SolrParams defaults, org.apache.solr.common.params.SolrParams appends, org.apache.solr.common.params.SolrParams invariants)Set default-ish params on a SolrQueryRequest.static voidsetDefaults(SolrRequestHandler handler, SolrQueryRequest req, org.apache.solr.common.params.SolrParams defaults, org.apache.solr.common.params.SolrParams appends, org.apache.solr.common.params.SolrParams invariants)static voidsetMinShouldMatch(org.apache.lucene.search.BooleanQuery.Builder q, String spec)static voidsetMinShouldMatch(org.apache.lucene.search.BooleanQuery.Builder q, String spec, boolean mmAutoRelax)Checks the number of optional clauses in the query, and compares it with the specification string to determine the proper value to use.static org.apache.lucene.search.BooleanQuerysetMinShouldMatch(org.apache.lucene.search.BooleanQuery q, String spec)static org.apache.lucene.search.BooleanQuerysetMinShouldMatch(org.apache.lucene.search.BooleanQuery q, String spec, boolean mmAutoRelax)static String[]split(String value)Split a value between spaces and/or commas.static CharSequencestripIllegalOperators(CharSequence s)Strips operators that are used illegally, otherwise returns its input.static CharSequencestripUnbalancedQuotes(CharSequence s)Returns its input if there is an even (ie: balanced) number of '"' characters -- otherwise returns a String in which all '"' characters are striped out.
 
- 
- 
- 
Field Detail- 
UNKNOWN_VALUEprotected static final String UNKNOWN_VALUE - See Also:
- Constant Field Values
 
 
- 
 - 
Method Detail- 
setDefaultspublic static void setDefaults(SolrQueryRequest req, org.apache.solr.common.params.SolrParams defaults, org.apache.solr.common.params.SolrParams appends, org.apache.solr.common.params.SolrParams invariants) Set default-ish params on a SolrQueryRequest.RequestHandlers can use this method to ensure their defaults and overrides are visible to other components such as the response writer - Parameters:
- req- The request whose params we are interested i
- defaults- values to be used if no values are specified in the request params
- appends- values to be appended to those from the request (or defaults) when dealing with multi-val params, or treated as another layer of defaults for singl-val params.
- invariants- values which will be used instead of any request, or default values, regardless of context.
 
 - 
setDefaultspublic static void setDefaults(SolrRequestHandler handler, SolrQueryRequest req, org.apache.solr.common.params.SolrParams defaults, org.apache.solr.common.params.SolrParams appends, org.apache.solr.common.params.SolrParams invariants) 
 - 
numDocspublic static int numDocs(SolrIndexSearcher s, org.apache.lucene.search.Query q, org.apache.lucene.search.Query f) throws IOException SolrIndexSearch.numDocs(Query,Query) freaks out if the filtering query is null, so we use this workarround.- Throws:
- IOException
 
 - 
splitpublic static String[] split(String value) Split a value between spaces and/or commas. No need to trim anything.
 - 
optimizePreFetchDocspublic static void optimizePreFetchDocs(ResponseBuilder rb, DocList docs, org.apache.lucene.search.Query query, SolrQueryRequest req, SolrQueryResponse res) throws IOException Pre-fetch documents into the index searcher's document cache.This is an entirely optional step which you might want to perform for the following reasons: - Locates the document-retrieval costs in one spot, which helps detailed performance measurement
- Determines a priori what fields will be needed to be fetched by various subtasks, like response writing and highlighting. This minimizes the chance that many needed fields will be loaded lazily. (it is more efficient to load all the field we require normally).
 - Throws:
- IOException
 
 - 
getDebugInterestspublic static Set<String> getDebugInterests(String[] params, ResponseBuilder rb) 
 - 
doStandardDebugpublic static org.apache.solr.common.util.NamedList<Object> doStandardDebug(SolrQueryRequest req, String userQuery, org.apache.lucene.search.Query query, DocList results, boolean dbgQuery, boolean dbgResults) throws IOException Returns a NamedList containing many "standard" pieces of debugging information.- rawquerystring - the 'q' param exactly as specified by the client
- querystring - the 'q' param after any preprocessing done by the plugin
- parsedquery - the main query executed formated by the Solr QueryParsing utils class (which knows about field types)
- parsedquery_toString - the main query executed formatted by its own toString method (in case it has internal state Solr doesn't know about)
- explain - the list of score explanations for each document in results against query.
- otherQuery - the query string specified in 'explainOther' query param.
- explainOther - the list of score explanations for each document in results against 'otherQuery'
 - Parameters:
- req- the request we are dealing with
- userQuery- the users query as a string, after any basic preprocessing has been done
- query- the query built from the userQuery (and perhaps other clauses) that identifies the main result set of the response.
- results- the main result set of the response
- Returns:
- The debug info
- Throws:
- IOException- if there was an IO error
 
 - 
doStandardQueryDebugpublic static void doStandardQueryDebug(SolrQueryRequest req, String userQuery, org.apache.lucene.search.Query query, boolean dbgQuery, org.apache.solr.common.util.NamedList<Object> dbg) 
 - 
doStandardResultsDebugpublic static void doStandardResultsDebug(SolrQueryRequest req, org.apache.lucene.search.Query query, DocList results, boolean dbgResults, org.apache.solr.common.util.NamedList<Object> dbg) throws IOException - Throws:
- IOException
 
 - 
explanationToNamedListpublic static org.apache.solr.common.util.NamedList<Object> explanationToNamedList(org.apache.lucene.search.Explanation e) 
 - 
explanationsToNamedListspublic static org.apache.solr.common.util.NamedList<org.apache.solr.common.util.NamedList<Object>> explanationsToNamedLists(org.apache.solr.common.util.NamedList<org.apache.lucene.search.Explanation> explanations) 
 - 
getExplanationspublic static org.apache.solr.common.util.NamedList<org.apache.lucene.search.Explanation> getExplanations(org.apache.lucene.search.Query query, DocList docs, SolrIndexSearcher searcher, IndexSchema schema) throws IOExceptionGenerates an NamedList of Explanations for each item in a list of docs.- Parameters:
- query- The Query you want explanations in the context of
- docs- The Documents you want explained relative that query
- Throws:
- IOException
 
 - 
doSimpleQuerypublic static DocList doSimpleQuery(String sreq, SolrQueryRequest req, int start, int limit) throws IOException Executes a basic query- Throws:
- IOException
 
 - 
parseFieldBoostspublic static Map<String,Float> parseFieldBoosts(String in) Given a string containing fieldNames and boost info, converts it to a Map from field name to boost info.Doesn't care if boost info is negative, you're on your own. Doesn't care if boost info is missing, again: you're on your own. - Parameters:
- in- a String like "fieldOne^2.3 fieldTwo fieldThree^-0.4"
- Returns:
- Map of fieldOne => 2.3, fieldTwo => null, fieldThree => -0.4
 
 - 
parseFieldBoostspublic static Map<String,Float> parseFieldBoosts(String[] fieldLists) LikeparseFieldBoosts(String), but parses all the strings in the provided array (which may be null).- Parameters:
- fieldLists- an array of Strings eg.- {"fieldOne^2.3", "fieldTwo", fieldThree^-0.4}
- Returns:
- Map of fieldOne => 2.3, fieldTwo => null, fieldThree => -0.4
 
 - 
parseFieldBoostsAndSloppublic static List<FieldParams> parseFieldBoostsAndSlop(String[] fieldLists, int wordGrams, int defaultSlop) /** LikeparseFieldBoosts(java.lang.String), but allows for an optional slop value prefixed by "~".- Parameters:
- fieldLists- - an array of Strings eg.- {"fieldOne^2.3", "fieldTwo", fieldThree~5^-0.4}
- wordGrams- - (0=all words, 2,3 = shingle size)
- defaultSlop- - the default slop for this param
- Returns:
- - FieldParams containing the fieldname,boost,slop,and shingle size
 
 - 
setMinShouldMatchpublic static void setMinShouldMatch(org.apache.lucene.search.BooleanQuery.Builder q, String spec, boolean mmAutoRelax)Checks the number of optional clauses in the query, and compares it with the specification string to determine the proper value to use.If mmAutoRelax=true, we'll perform auto relaxation of mm if tokens are removed from some but not all DisMax clauses, as can happen when stopwords or punctuation tokens are removed in analysis. Details about the specification format can be found here A few important notes... - If the calculations based on the specification determine that no optional clauses are needed, BooleanQuerysetMinMumberShouldMatch will never be called, but the usual rules about BooleanQueries still apply at search time (a BooleanQuery containing no required clauses must still match at least one optional clause)
- No matter what number the calculation arrives at, BooleanQuery.setMinShouldMatch() will never be called with a value greater then the number of optional clauses (or less then 1)
 :TODO: should optimize the case where number is same as clauses to just make them all "required" - Parameters:
- q- The query as a BooleanQuery.Builder
- spec- The mm spec
- mmAutoRelax- whether to perform auto relaxation of mm if tokens are removed from some but not all DisMax clauses
 
 - 
setMinShouldMatchpublic static void setMinShouldMatch(org.apache.lucene.search.BooleanQuery.Builder q, String spec)
 - 
setMinShouldMatchpublic static org.apache.lucene.search.BooleanQuery setMinShouldMatch(org.apache.lucene.search.BooleanQuery q, String spec)
 - 
setMinShouldMatchpublic static org.apache.lucene.search.BooleanQuery setMinShouldMatch(org.apache.lucene.search.BooleanQuery q, String spec, boolean mmAutoRelax)
 - 
flattenBooleanQuerypublic static void flattenBooleanQuery(org.apache.lucene.search.BooleanQuery.Builder to, org.apache.lucene.search.BooleanQuery from)Recursively walks the "from" query pulling out sub-queries and adding them to the "to" query.Boosts are multiplied as needed. Sub-BooleanQueryies which are not optional will not be flattened. From will be mangled durring the walk, so do not attempt to reuse it. 
 - 
partialEscapepublic static CharSequence partialEscape(CharSequence s) Escapes all special characters except '"', '-', and '+'
 - 
stripIllegalOperatorspublic static CharSequence stripIllegalOperators(CharSequence s) Strips operators that are used illegally, otherwise returns its input. Some examples of illegal user queries are: "chocolate +- chip", "chocolate - - chip", and "chocolate chip -".
 - 
stripUnbalancedQuotespublic static CharSequence stripUnbalancedQuotes(CharSequence s) Returns its input if there is an even (ie: balanced) number of '"' characters -- otherwise returns a String in which all '"' characters are striped out.
 - 
removeNullspublic static <T> org.apache.solr.common.util.NamedList<T> removeNulls(Map.Entry<String,T>[] entries, org.apache.solr.common.util.NamedList<T> dest) Adds todestall the not-null elements ofentriesthat have non-null names- Parameters:
- entries- The array of entries to be added to the- NamedList- dest
- dest- The- NamedListinstance where the not-null elements of entries are added
- Returns:
- Returns The destinput object
 
 - 
copyNamedListIntoArrayByDocPosInResponsepublic static void copyNamedListIntoArrayByDocPosInResponse(org.apache.solr.common.util.NamedList<Object> namedList, Map<Object,ShardDoc> resultIds, Map.Entry<String,Object>[] destArr) Copies the givennamedListassumed to have doc uniqueKey keyed data intodestArrat the position of the document in the response. destArr is assumed to be the same size asresultIdsis.resultIdscomes fromResponseBuilder.resultIds. If the doc key isn't inresultIdsthen it is ignored. Note: most likely you will callremoveNulls(Map.Entry[], NamedList)sometime after calling this.
 - 
getSortpublic static org.apache.lucene.search.Sort getSort(SolrQueryRequest req) Determines the correct Sort based on the request parameter "sort"- Returns:
- null if no sort is specified.
 
 - 
parseQueryStringspublic static List<org.apache.lucene.search.Query> parseQueryStrings(SolrQueryRequest req, String[] queries) throws SyntaxError Turns an array of query strings into a List of Query objects.- Returns:
- null if no queries are generated
- Throws:
- SyntaxError
 
 - 
invokeSetterspublic static void invokeSetters(Object bean, Iterable<? extends Map.Entry<String,?>> initArgs) 
 - 
invokeSetterspublic static void invokeSetters(Object bean, Iterable<? extends Map.Entry<String,?>> initArgs, boolean lenient) 
 - 
getRequestPurposepublic static String getRequestPurpose(Integer reqPurpose) Given the integer purpose of a request generates a readable value corresponding the request purposes (there can be more than one on a single request). If there is a purpose parameter present that's not known this method will return "Unknown"- Parameters:
- reqPurpose- Numeric request purpose
- Returns:
- a comma separated list of purposes or "Unknown"
 
 - 
forEachRequestPurposepublic static void forEachRequestPurpose(Integer reqPurpose, Consumer<String> consumer) Given the integer purpose of a request generates a readable value corresponding the request purposes (there can be more than one on a single request). If there is a purpose parameter present that's not known this method will invoke the consumer with "Unknown".- Parameters:
- reqPurpose- Numeric request purpose
- consumer- recipient of a string
 
 
- 
 
-