Understanding request handlers

Most interactions with Solr, including indexing and searching, are processed by what Solr calls request handlers. Request handlers are configured in the solrconfig.xml file and are clearly labeled as such. Many of them exist for special purposes, such as handling a CSV import, for example. Here is how the default request handler is configured:

<requestHandler name="/select" class="solr.SearchHandler">
  <!-- default values for query parameters can be specified, these will be overridden by parameters in the request -->
  <lst name="defaults">
    <str name="echoParams">explicit</str>
    <int name="rows">10</int>
    <str name="df">text</str>
  </lst>
… 

The request handlers that perform searches allow configuration of two things:

  • Establishing default parameters and making some unchangeable
  • Registering Solr search components such as faceting and highlighting

Tip

Create a request handler configuration for your application

Instead of using /select for all your application's searches, we recommend that you create a request handler for each type of search that your application requires (for example, separate out a standard search from auto-complete). In doing so, you can more easily change search options through the request handler configuration and reduce hard-wired configuration in the application. This approach gives you better granularity of search statistics on Solr's Plugins / Stats screen and it makes your web server logs more discernable.

Let's say that in the MusicBrainz search interface, we have a search form that searches for bands. We have a Solr core just for artists named mbartists, but this contains not only bands but also individual band members. When the field named a_type is group, we have a band. To start, copy the default configuration, and give it a name such as /bands. We can now use this request handler with /bands in the URL as follows:

/solr/mbartists/bands&q=Smashing&.....

An older alternative that we don't recommend is to name the handler without a leading / and then use the qt parameter to reference it, but still use the /select path. This requires a change in solrconfig, where you set <requestDispatcher handleSelect="true"> earlier in the file. The URL then looks like this:

/solr/mbartists/select?qt=bands&q=Smashing&.....

Let's now configure this request handler to filter searches to find only the bands, without the search application having to specify this. We'll also set a few other options as follows:

<requestHandler name="/bands" class="solr.SearchHandler">
  <lst name="defaults">
    <str name="echoParams">none</str>
    <int name="rows">20</int>
  </lst>
  <lst name="appends">
    <str name="fq">a_type:group</str>
  </lst>
  <lst name="invariants">
    <str name="facet">false</str>
  </lst>
</requestHandler>

Request handlers have several lists to configure. These use Solr's generic XML data structure, which was described earlier.

  • defaults: These simply establish default values for various request parameters. Parameters in the request will override them.
  • appends: For parameters that can be set multiple times, such as fq, this section specifies values that will be set in addition to any that may be specified by the request.
  • invariants: This sets defaults that cannot be overridden. It is useful for security purposes—a topic for Chapter 11, Deployment. It can also be used to override what the client sends when you don't have control over the client application; for instance, if the application is deployed and you can't easily re-deploy a new client.
  • first-components, components, last-components: These list the Solr search components to be registered for possible use with this request handler. By default, a set of search components is already registered to enable functionality such as querying and faceting. Setting first-components or last-components prepends or appends to this list respectively, whereas setting components overrides the list completely. For more information about search components, read Chapter 8, Search Components.

Solr 4.2 contains a new query parser named switch that is useful when the defaults and appends options above are insufficiently flexible. Consult Solr's new reference guide on it for more information.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.109.151