org.apache.solr.util
Class SolrPluginUtils

java.lang.Object
  extended by org.apache.solr.util.SolrPluginUtils

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.


Nested Class Summary
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.
 
Field Summary
static java.lang.String FL
          Deprecated. 
 
Constructor Summary
SolrPluginUtils()
           
 
Method Summary
static DocList doSimpleQuery(java.lang.String sreq, SolrIndexSearcher searcher, IndexSchema schema, 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 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)
          Generates an list 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 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)
          Given a string containing functions with optional boosts, returns an array of Queries representing those functions with the specified boosts.
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 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 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FL

@Deprecated
public static java.lang.String FL
Deprecated. 
standard param for field list

Constructor Detail

SolrPluginUtils

public SolrPluginUtils()
Method Detail

setDefaults

public static void setDefaults(SolrQueryRequest req,
                               SolrParams defaults)
Set defaults on a SolrQueryRequest. RequestHandlers can use this method to ensure their defaults are visible to other components such as the response writer


setDefaults

public static void setDefaults(SolrQueryRequest req,
                               SolrParams defaults,
                               SolrParams appends,
                               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.

numDocs

public static int numDocs(SolrIndexSearcher s,
                          org.apache.lucene.search.Query q,
                          org.apache.lucene.search.Query f)
                   throws java.io.IOException
SolrIndexSearch.numDocs(Query,Query) freaks out if the filtering query is null, so we use this workarround.

Throws:
java.io.IOException

getParam

public static java.lang.String getParam(SolrQueryRequest req,
                                        java.lang.String param,
                                        java.lang.String def)
Deprecated. use SolrParam.get(String,String)

Returns the param, or the default if it's empty or not specified.


getNumberParam

public static java.lang.Number getNumberParam(SolrQueryRequest req,
                                              java.lang.String param,
                                              java.lang.Number def)
Deprecated. use SolrParam.getFloat(String,float)

Treats the param value as a Number, returns the default if nothing is there or if it's not a number.


getBooleanParam

public static boolean getBooleanParam(SolrQueryRequest req,
                                      java.lang.String param,
                                      boolean def)
Deprecated. use SolrParam.getBool(String,boolean)

Treats parameter value as a boolean. The string 'false' is false; any other non-empty string is true.


split

public static java.lang.String[] split(java.lang.String value)
Split a value that may contain a comma, space of bar separated list.


setReturnFields

public static int setReturnFields(SolrQueryRequest req,
                                  SolrQueryResponse res)
Assumes the standard query param of "fl" to specify the return fields

See Also:
setReturnFields(String,SolrQueryResponse)

setReturnFields

public static int setReturnFields(java.lang.String fl,
                                  SolrQueryResponse res)
Given a space seperated list of field names, sets the field list on the SolrQueryResponse.

Returns:
bitfield of SolrIndexSearcher flags that need to be set

optimizePreFetchDocs

public static void optimizePreFetchDocs(DocList docs,
                                        org.apache.lucene.search.Query query,
                                        SolrQueryRequest req,
                                        SolrQueryResponse res)
                                 throws java.io.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: If lazy field loading is disabled, this method does nothing.

Throws:
java.io.IOException

doStandardDebug

public static NamedList doStandardDebug(SolrQueryRequest req,
                                        java.lang.String userQuery,
                                        org.apache.lucene.search.Query query,
                                        DocList results,
                                        CommonParams params)
                                 throws java.io.IOException
Deprecated. Use doStandardDebug(SolrQueryRequest,String,Query,DocList) with setDefaults

Returns a NamedList containing many "standard" pieces of debugging information.

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
Throws:
java.io.IOException

doStandardDebug

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.

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
Throws:
java.io.IOException

getExplainList

public static NamedList getExplainList(org.apache.lucene.search.Query query,
                                       DocList docs,
                                       SolrIndexSearcher searcher,
                                       IndexSchema schema)
                                throws java.io.IOException
Generates an list 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:
java.io.IOException

doSimpleQuery

public static DocList doSimpleQuery(java.lang.String sreq,
                                    SolrIndexSearcher searcher,
                                    IndexSchema schema,
                                    int start,
                                    int limit)
                             throws java.io.IOException
Executes a basic query in lucene syntax

Throws:
java.io.IOException

parseFieldBoosts

public 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.

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

parseFieldBoosts

public 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).

Parameters:
fieldLists - an array of Strings eg. {"fieldOne^2.3", "fieldTwo"}
Returns:
Map of fieldOne => 2.3, fieldThree => -0.4

parseFuncs

public static java.util.List<org.apache.lucene.search.Query> parseFuncs(IndexSchema s,
                                                                        java.lang.String in)
                                                                 throws org.apache.lucene.queryParser.ParseException
Given a string containing functions with optional boosts, returns an array of Queries representing those functions with the specified boosts.

NOTE: intra-function whitespace is not allowed.

Throws:
org.apache.lucene.queryParser.ParseException
See Also:
parseFieldBoosts(java.lang.String)

setMinShouldMatch

public 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.

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"


flattenBooleanQuery

public 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.

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.


partialEscape

public static java.lang.CharSequence partialEscape(java.lang.CharSequence s)
Escapes all special characters except '"', '-', and '+'

See Also:
QueryParser.escape(java.lang.String)

stripUnbalancedQuotes

public 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.


getSort

public 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.

parseFilterQueries

public static java.util.List<org.apache.lucene.search.Query> parseFilterQueries(SolrQueryRequest req)
                                                                         throws org.apache.lucene.queryParser.ParseException
Builds a list of Query objects that should be used to filter results

Returns:
null if no filter queries
Throws:
org.apache.lucene.queryParser.ParseException
See Also:
SolrParams.FQ

parseQueryStrings

public static java.util.List<org.apache.lucene.search.Query> parseQueryStrings(SolrQueryRequest req,
                                                                               java.lang.String[] queries)
                                                                        throws org.apache.lucene.queryParser.ParseException
Turns an array of query strings into a List of Query objects.

Returns:
null if no queries are generated
Throws:
org.apache.lucene.queryParser.ParseException


Copyright © 2006 - 2008 The Apache Software Foundation