public class SolrPluginUtils
extends java.lang.Object
Utilities that may be of use to RequestHandlers.
Many of these functions have code that was stolen/mutated from StandardRequestHandler.
:TODO: refactor StandardRequestHandler to use these utilities
:TODO: Many "standard" functionality methods are not cognisant of default parameter settings.
Modifier and Type | Class and Description |
---|---|
static class |
SolrPluginUtils.DisjunctionMaxQueryParser
A subclass of SolrQueryParser that supports aliasing fields for
constructing DisjunctionMaxQueries.
|
static class |
SolrPluginUtils.IdentityRegenerator
A CacheRegenerator that can be used whenever the items in the cache
are not dependant on the current searcher.
|
Modifier and Type | Field and Description |
---|---|
private static java.util.regex.Pattern |
CONSECUTIVE_OP_PATTERN |
private static java.util.regex.Pattern |
DANGLING_OP_PATTERN |
static java.lang.String |
FL
Deprecated.
Use org.apache.solr.common.params.CommonParams.FL.
|
(package private) static org.slf4j.Logger |
log |
private static java.util.regex.Pattern |
splitList |
Constructor and Description |
---|
SolrPluginUtils() |
Modifier and Type | Method and Description |
---|---|
static void |
addOrReplaceResults(SolrQueryResponse rsp,
SolrDocumentList docs)
Given a SolrQueryResponse replace the DocList if it is in the result.
|
(package private) static int |
calculateMinShouldMatch(int optionalClauseCount,
java.lang.String spec)
helper exposed for UnitTests
|
static SolrDocumentList |
docListToSolrDocumentList(DocList docs,
SolrIndexSearcher searcher,
java.util.Set<java.lang.String> fields,
java.util.Map<SolrDocument,java.lang.Integer> ids)
Convert a DocList to a SolrDocumentList
The optional param "ids" is populated with the lucene document id
for each SolrDocument.
|
static DocList |
doSimpleQuery(java.lang.String sreq,
SolrQueryRequest req,
int start,
int limit)
Executes a basic query in lucene syntax
|
static NamedList |
doStandardDebug(SolrQueryRequest req,
java.lang.String userQuery,
org.apache.lucene.search.Query query,
DocList results)
Returns a NamedList containing many "standard" pieces of debugging
information.
|
static NamedList |
doStandardDebug(SolrQueryRequest req,
java.lang.String userQuery,
org.apache.lucene.search.Query query,
DocList results,
CommonParams params)
Deprecated.
Use doStandardDebug(SolrQueryRequest,String,Query,DocList) with setDefaults
|
static NamedList<NamedList<java.lang.Object>> |
explanationsToNamedLists(NamedList<org.apache.lucene.search.Explanation> explanations) |
private static NamedList<java.lang.String> |
explanationsToStrings(NamedList<org.apache.lucene.search.Explanation> explanations) |
static NamedList<java.lang.Object> |
explanationToNamedList(org.apache.lucene.search.Explanation e) |
static void |
flattenBooleanQuery(org.apache.lucene.search.BooleanQuery to,
org.apache.lucene.search.BooleanQuery from)
Recursively walks the "from" query pulling out sub-queries and
adding them to the "to" query.
|
static boolean |
getBooleanParam(SolrQueryRequest req,
java.lang.String param,
boolean def)
Deprecated.
use SolrParam.getBool(String,boolean)
|
static NamedList |
getExplainList(org.apache.lucene.search.Query query,
DocList docs,
SolrIndexSearcher searcher,
IndexSchema schema)
Deprecated.
this returns the explanations as Strings, instead it
is recommeded to use getExplanations and call toString()
yourself, or use explanationsToNamedLists
|
static 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 java.lang.Number |
getNumberParam(SolrQueryRequest req,
java.lang.String param,
java.lang.Number def)
Deprecated.
use SolrParam.getFloat(String,float)
|
static java.lang.String |
getParam(SolrQueryRequest req,
java.lang.String param,
java.lang.String def)
Deprecated.
use SolrParam.get(String,String)
|
static org.apache.lucene.search.Sort |
getSort(SolrQueryRequest req)
Determines the correct Sort based on the request parameter "sort"
|
static void |
invokeSetters(java.lang.Object bean,
NamedList initArgs) |
static int |
numDocs(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 void |
optimizePreFetchDocs(DocList docs,
org.apache.lucene.search.Query query,
SolrQueryRequest req,
SolrQueryResponse res)
Pre-fetch documents into the index searcher's document cache.
|
static java.util.Map<java.lang.String,java.lang.Float> |
parseFieldBoosts(java.lang.String in)
Given a string containing fieldNames and boost info,
converts it to a Map from field name to boost info.
|
static java.util.Map<java.lang.String,java.lang.Float> |
parseFieldBoosts(java.lang.String[] fieldLists)
Like
parseFieldBoosts(String) , but parses all the strings
in the provided array (which may be null). |
static java.util.List<org.apache.lucene.search.Query> |
parseFilterQueries(SolrQueryRequest req)
Builds a list of Query objects that should be used to filter results
|
static java.util.List<org.apache.lucene.search.Query> |
parseFuncs(IndexSchema s,
java.lang.String in)
Deprecated.
|
static java.util.List<org.apache.lucene.search.Query> |
parseQueryStrings(SolrQueryRequest req,
java.lang.String[] queries)
Turns an array of query strings into a List of Query objects.
|
static java.lang.CharSequence |
partialEscape(java.lang.CharSequence s)
Escapes all special characters except '"', '-', and '+'
|
static java.lang.String |
resolveUpdateChainParam(SolrParams params,
org.slf4j.Logger log)
Deprecated.
|
static void |
setDefaults(SolrQueryRequest req,
SolrParams defaults)
Set defaults on a SolrQueryRequest.
|
static void |
setDefaults(SolrQueryRequest req,
SolrParams defaults,
SolrParams appends,
SolrParams invariants)
Set default-ish params on a SolrQueryRequest.
|
static void |
setMinShouldMatch(org.apache.lucene.search.BooleanQuery q,
java.lang.String spec)
Checks the number of optional clauses in the query, and compares it
with the specification string to determine the proper value to use.
|
static int |
setReturnFields(SolrQueryRequest req,
SolrQueryResponse res)
Assumes the standard query param of "fl" to specify the return fields
|
static int |
setReturnFields(java.lang.String fl,
SolrQueryResponse res)
Given a space seperated list of field names, sets the field list on the
SolrQueryResponse.
|
static java.lang.String[] |
split(java.lang.String value)
Split a value that may contain a comma, space of bar separated list.
|
static java.lang.CharSequence |
stripIllegalOperators(java.lang.CharSequence s)
Strips operators that are used illegally, otherwise reuturns it's
input.
|
static java.lang.CharSequence |
stripUnbalancedQuotes(java.lang.CharSequence s)
Returns it's input if there is an even (ie: balanced) number of
'"' characters -- otherwise returns a String in which all '"'
characters are striped out.
|
static final org.slf4j.Logger log
@Deprecated public static java.lang.String FL
private static final java.util.regex.Pattern splitList
private static final java.util.regex.Pattern DANGLING_OP_PATTERN
private static final java.util.regex.Pattern CONSECUTIVE_OP_PATTERN
public static void setDefaults(SolrQueryRequest req, SolrParams defaults)
public static void setDefaults(SolrQueryRequest req, SolrParams defaults, SolrParams appends, SolrParams invariants)
req
- The request whose params we are interested idefaults
- values to be used if no values are specified in the request paramsappends
- 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.public static int numDocs(SolrIndexSearcher s, org.apache.lucene.search.Query q, org.apache.lucene.search.Query f) throws java.io.IOException
java.io.IOException
@Deprecated public static java.lang.String getParam(SolrQueryRequest req, java.lang.String param, java.lang.String def)
@Deprecated public static java.lang.Number getNumberParam(SolrQueryRequest req, java.lang.String param, java.lang.Number def)
@Deprecated public static boolean getBooleanParam(SolrQueryRequest req, java.lang.String param, boolean def)
public static java.lang.String[] split(java.lang.String value)
public static int setReturnFields(SolrQueryRequest req, SolrQueryResponse res)
public static int setReturnFields(java.lang.String fl, SolrQueryResponse res)
public static void optimizePreFetchDocs(DocList docs, org.apache.lucene.search.Query query, SolrQueryRequest req, SolrQueryResponse res) throws java.io.IOException
java.io.IOException
@Deprecated public static NamedList doStandardDebug(SolrQueryRequest req, java.lang.String userQuery, org.apache.lucene.search.Query query, DocList results, CommonParams params) throws java.io.IOException
Returns a NamedList containing many "standard" pieces of debugging information.
req
- the request we are dealing withuserQuery
- the users query as a string, after any basic
preprocessing has been donequery
- 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 responsejava.io.IOException
public static NamedList doStandardDebug(SolrQueryRequest req, java.lang.String userQuery, org.apache.lucene.search.Query query, DocList results) throws java.io.IOException
Returns a NamedList containing many "standard" pieces of debugging information.
req
- the request we are dealing withuserQuery
- the users query as a string, after any basic
preprocessing has been donequery
- 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 responsejava.io.IOException
public static NamedList<java.lang.Object> explanationToNamedList(org.apache.lucene.search.Explanation e)
public static NamedList<NamedList<java.lang.Object>> explanationsToNamedLists(NamedList<org.apache.lucene.search.Explanation> explanations)
public static NamedList<org.apache.lucene.search.Explanation> getExplanations(org.apache.lucene.search.Query query, DocList docs, SolrIndexSearcher searcher, IndexSchema schema) throws java.io.IOException
query
- The Query you want explanations in the context ofdocs
- The Documents you want explained relative that queryjava.io.IOException
private static NamedList<java.lang.String> explanationsToStrings(NamedList<org.apache.lucene.search.Explanation> explanations)
@Deprecated public static NamedList getExplainList(org.apache.lucene.search.Query query, DocList docs, SolrIndexSearcher searcher, IndexSchema schema) throws java.io.IOException
query
- The Query you want explanations in the context ofdocs
- The Documents you want explained relative that queryjava.io.IOException
public static DocList doSimpleQuery(java.lang.String sreq, SolrQueryRequest req, int start, int limit) throws java.io.IOException
java.io.IOException
public static java.util.Map<java.lang.String,java.lang.Float> parseFieldBoosts(java.lang.String in)
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.
in
- a String like "fieldOne^2.3 fieldTwo fieldThree^-0.4"public static java.util.Map<java.lang.String,java.lang.Float> parseFieldBoosts(java.lang.String[] fieldLists)
parseFieldBoosts(String)
, but parses all the strings
in the provided array (which may be null).fieldLists
- an array of Strings eg. {"fieldOne^2.3", "fieldTwo", fieldThree^-0.4}
@Deprecated public static java.util.List<org.apache.lucene.search.Query> parseFuncs(IndexSchema s, java.lang.String in) throws org.apache.lucene.queryParser.ParseException
NOTE: intra-function whitespace is not allowed.
org.apache.lucene.queryParser.ParseException
parseFieldBoosts(java.lang.String)
public static void setMinShouldMatch(org.apache.lucene.search.BooleanQuery q, java.lang.String spec)
Details about the specification format can be found here
A few important notes...
:TODO: should optimize the case where number is same as clauses to just make them all "required"
static int calculateMinShouldMatch(int optionalClauseCount, java.lang.String spec)
public static void flattenBooleanQuery(org.apache.lucene.search.BooleanQuery to, org.apache.lucene.search.BooleanQuery from)
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.
public static java.lang.CharSequence partialEscape(java.lang.CharSequence s)
QueryParser.escape(java.lang.String)
public static java.lang.CharSequence stripIllegalOperators(java.lang.CharSequence s)
public static java.lang.CharSequence stripUnbalancedQuotes(java.lang.CharSequence s)
public static org.apache.lucene.search.Sort getSort(SolrQueryRequest req)
public static java.util.List<org.apache.lucene.search.Query> parseFilterQueries(SolrQueryRequest req) throws org.apache.lucene.queryParser.ParseException
org.apache.lucene.queryParser.ParseException
CommonParams.FQ
public static java.util.List<org.apache.lucene.search.Query> parseQueryStrings(SolrQueryRequest req, java.lang.String[] queries) throws org.apache.lucene.queryParser.ParseException
org.apache.lucene.queryParser.ParseException
public static SolrDocumentList docListToSolrDocumentList(DocList docs, SolrIndexSearcher searcher, java.util.Set<java.lang.String> fields, java.util.Map<SolrDocument,java.lang.Integer> ids) throws java.io.IOException
docs
- The DocList
to convertsearcher
- The SolrIndexSearcher
to use to load the docs from the Lucene indexfields
- The names of the Fields to loadids
- A map to store the ids of the docsSolrDocumentList
containing all the loaded docsjava.io.IOException
- if there was a problem loading the docspublic static void addOrReplaceResults(SolrQueryResponse rsp, SolrDocumentList docs)
public static void invokeSetters(java.lang.Object bean, NamedList initArgs)
public static java.lang.String resolveUpdateChainParam(SolrParams params, org.slf4j.Logger log)
params
- an instance of SolrParams from the requestlog
- an instance of a slf4j logger to log a warning in case of deprecated param usage