Solr : Write custom request Handler

2019-08-28 13:49发布

问题:

Well , I want to write custom request handler. So I thought of reviewing code of 'standard request handler' come with solr. Where can I find source code of handler. i didn't find it in my solr directory.

回答1:

You can view the source code of the StandardRequestHandler from this link. The entire tree of the source code can be accessed in readonly view via http://svn.apache.org/viewvc/lucene/dev/

I would encourage you to check out the RequestHandler documentation on the Solr Wiki as well for reference and guidance.



回答2:

In order to create and use a custom request handler in solr you need to:

  1. Write a class that extends from SearchHandler and handles the custom logic.
  2. Update solrconfig.xml to add en endpoint that uses the custom request handler.

Custom Solr Request Handler Class

public class MyCustomRequestHandler extends SearchHandler {

  @Override
  public void handleRequestBody(SolrQueryRequest solrRequest,
    SolrQueryResponse solrResponse) throws Exception {

    /// modify solr request object

    // let solr handle the modified request
    super.handleRequestBody(solrRequest, solrResponse); 

    // optionally modify solr response object
  }
}

Solr configuration

<requestHandler name="/custom_endpoint"
  class="org.example.MyCustomRequestHandler" default="true">
  <lst name="defaults">
    <str name="echoParams">explicit</str>
    <str name="wt">json</str>
    <str name="defType">edismax</str>

    ... rest of configuration

  </lst>
</requestHandler>


标签: solr