Tag Archives: QueryComponent

Solr provides ability to customize handling of Solr requests by extending built in or by writing completely new Request handlers and Search Components. Search components define the logic for running Solr queries and make the results available in the response which will be returned to users. There are several default search components that work with all SearchHandlers without any additional configuration and these are executed by default, in the following order

Screen Shot 2015-09-08 at 10.16.11 am

If you register a new search component with one of these default names, the newly defined component will be used instead of the default. The search component is defined in solrconfig.xml separate from the request handlers, and then registered with a request handler as needed. By default Solr uses a search component named “query” for performing search operations.  Below sample configuration is for overwriting default search component with a new search component ‘MyQueryComponent’.

/products/solrconfig.xml

Executing multiple search queries

By default QueryComponent can execute one Solr search per request. For supporting multiple Solr queries we need to write a search component either by extending QueryComponent or SearchComponent.

Write a class by extending QueryComponent

Overwrite prepare() and process() methods to customize our needs.

prepare() method:

We can write logic for validating request and prepare component for processing queries.  The custom query component we are writing do not support grouping and validating mandatory query parameters.

process () method:

Process method first extracts raw solr queries (buildRawQueries())  and build SolrParams (parseRawQueries) out of raw Solr queries. Each SolrParams instance resembles one query.  Execute one query at a time and the query result is added to ResponseBuilder. The logic for query execution is delegated to QueryComponent as there is nothing much we need to override for our requirement (processQuery).  Queries are executed in order specified and until we retrieve max rows required.  If rows parameter is passed -1 all queries are executed.

Prepare the final result by merging all the results of all queries executed and return to user.

Sample Solr query:

http://localhost:8983/solr/products/select?qn=3&mQry1=…&mQry2=….&mQry3=…&rows=25&bebug=true&sort=…&qt=multiQuery

Parameters:
qt  :   Name of query handler that process multi query Solr request .
qn :   Number of Solr queries in the request
mQryX: Solr query which can be executed independently. Where X in parameter name should be an integer which denotes the order of execution of query. If ‘qn’ parameter is 4, we have to pass mQry1, mQry2, mQry3 and mQry4 parameters.
debug:  Defaults to false. If passed true Solr query debug information is returned in response.
sort:  Default Sort parameter if sort parameter is not available in individual queries (mQryX).

Register custom search component in solr  by adding the following in solrconfig,xml

MultipleQueryComponent has to be register with RequestHandler to be used. We can configure a new request handler using existing StandardRequestHandler or write a custom request handler.

Use built-in request handler and configure MultipleQueryComponent:

As a custom request handler:

Code of MultipleQuerySearchHandler:

Complete code of MultipleQueryComponent:

Handling HTTP 413 error status from Solr while executing multiple Solr queries.

When sending multiple queries in one request, Solr may return HTTP 413 status code.

Solr returns HTTP 413 status code when the client sends Solr queries in GET request and total number of characters are more than allowed.  The maximum number of characters allowed depends on server configuration Solr is running. Mostly all servers allow 1024 characters in GET request. To avoid HTTP 413 error, clients should send Solr queries using POST method when request payload contains more than 1024 characters.