Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could somebody explain SOLR requestHandlers and responseWriters in detail?

People also ask

What is request handler?

Request handler is a function that determines whether an outgoing request should be mocked, and specifies its mocked response.

What is the role of Request Handler in SOLR?

A request handler processes requests coming to Solr. These might be query requests or index update requests. You will likely need several of these defined, depending on how you want Solr to handle the various requests you will make. A search component is a feature of search, such as highlighting or faceting.

What is Qt in SOLR?

Solr uses the qt parameter to determine which Query Handler should be used to process the request. Valid values are any of the names specified by declarations in solrconfig. xml. if we won't specify qt parameter then there must be a request handler in solrConfig. xml for a given request.


In Solr, a RequestHandler is essentially a plugin (module of logic) that handles incoming requests in a particular way.

Rather than "doing" some stuff to the actually search query, request handlers can provide functionality beyond queries, like for example data import.

Normally request handlers in Solr are configured to be specific endpoints (i.e. URL's), and not selected through the qt parameter. However, search specific handlers may use the query type parameter to handle a query in a specific way (i.e. one handler may be able to deal with various query types).

What handler to use, will normally depend on what you want to achieve. I would suggest that you check out the list of provided handlers, and match their descriptions (many have Wiki pages) to what you want to do.

For queries, other than the default SearchHandler, the most commonly used request handler is the DisMaxRequestHandler (qt=dismax), that will essentially provide a search across several fields using different weighting based on field significance. This is often referred to by people as "Google like" searching.

Also, you may be interested in looking at the MoreLikeThisHandler, that was designed to provide hits that are similar to a specific document (for example from a previous search result).

Note that as a source of confusion, the DisMaxRequestHandler in newer versions of Solr is actually provided by the default SearchHandler with a query type of dismax.

Handlers are configured in solrconfig.xml:

<requestHandler name="dismax" class="solr.SearchHandler" >
<lst name="defaults">
 <str name="defType">dismax</str>
 <str name="echoParams">explicit</str>
 <float name="tie">0.01</float>
 <str name="qf">
    id^10.0 keyword^1.5 title^1.0 region^0.5 country^0.5 city^0.5
 </str>
 <str name="pf">
    keyword^1.5 title^1.0 region^0.5 country^0.5 city^0.5
 </str>
 <str name="bf">
 </str>
 <str name="mm">
    2&lt;-1 5&lt;-2 6&lt;90%
 </str>
 <int name="ps">100</int>
 <str name="q.alt">*:*</str>

 <!-- example highlighter config, enable per-query with hl=true -->
 <str name="hl.fl">text features name</str>
 <!-- for this field, we want no fragmenting, just highlighting -->
 <str name="f.name.hl.fragsize">0</str>
 <!-- instructs Solr to return the field itself if no query terms are
      found -->
 <str name="f.name.hl.alternateField">name</str>
 <str name="f.text.hl.fragmenter">regex</str> <!-- defined below -->
</lst>

In this case, qf are the fields and boosts to apply for each field. The above configuration will search across id, keyword, title, region, country and city. Check out the corresponding Wiki page for more info (DisMaxRequestHandler applies).

The most effective way to use them is certainly to first know what exactly you want to achieve, then find the best match in terms of the handler, and then subsequently tune the configuration to provide the results you and your community would expect.

Response writers are plugins (modules of logic) that will provide the ways and means to write responses in specific formats. Currently this concept applies mainly to searches, where you will be dealing with derivatives of the QueryResponseWriter, which in turn will be selected by the response format parameter wt (in the query).

If you are working with PHP, then you will certainly be interested in generating PHP friendly responses to your requests, using wt=php or wt=phps.

The php response can be evaled:

$code = file_get_contents('http://localhost:8983/solr/select?q=iPod&wt=php');
eval("\$result = " . $code . ";");
print_r($result);

and the phps response can be read with PHP's serialization mechanism:

$serializedResult = file_get_contents('http://localhost:8983/solr/select?q=iPod&wt=phps');
$result = unserialize($serializedResult);
print_r($result);

There is support for others (like Ruby, Python, Java Binary, etc), which essentially have been build to make it easier for developers to integrate SOLR into their web applications using mechanisms that are faster than parsing XML or JSON.

However, there are are also other types of ResponseWriter's that actually transform the response based on a template (e.g. Velocity) or a transform (e.g. XSLT). This can be useful in certain situations, where you don't want to depend on another application to process the query results (e.g. XSLT directly into XHTML).

Response Writers are also configured in the solrconfig.xml, normally you will enable those that you are interested in e.g.:

<queryResponseWriter name="xml" class="org.apache.solr.request.XMLResponseWriter" default="true"/>
<queryResponseWriter name="json" class="org.apache.solr.request.JSONResponseWriter"/>
<queryResponseWriter name="php" class="org.apache.solr.request.PHPResponseWriter"/>
<queryResponseWriter name="phps" class="org.apache.solr.request.PHPSerializedResponseWriter"/>

The most effective way to use them depends on your environment and what you want to achieve. Obviously if you are in PHP, a phps may give you better (easy access through the language) and faster (built-in serialization mechanism, less verbose) results. If in Javascript, json may just be the way to go etc.

You see, SOLR is a powerful and versatile plattform :) If you want to figure a way through the jungle you may want to resort to some book, like "Solr 1.4 Enterprise Search Server" (although I am not sure it really does the best possible job as an introduction). Many things you can best figure out by trial-and-error as well as patience and some online help.

Hope this helps to get you going.