SolrJ和自定义Solr处理程序

时间:2019-02-09 06:13:31

标签: solr solrj

我正在尝试在Solr 7.3中实现一个简单的自定义请求处理程序。我需要对通过Solr Java API可用的方法进行一些说明。

据我了解,我已经用“ SearchHandler”扩展了Java类,然后覆盖了“ handleRequestBody”方法。我试图从一开始就了解流程。这是浏览器中的示例查询。

    http://localhost:8983/solr/customcollection/customhandler? 
    q=John&fl=id,last_name&maxRows=10 
1) Once you enter the above query in the browser and press 
"return" the Solr customhandler will be triggered. It will look 
for the necessary jars from where the handler is created.

2) Once it finds the main class it will execute the following 
method, which is overridden from the "SearchHandler" parent 
class.
        public void handleRequestBody(SolrRequest req, SolrResponse 
        resp) throws Exception

3) The SolrRequest req object will hold all the Solr Parameters 
on the query, in this case, q,fl and maxRows.

4) Using the following code I unpack these parameters.

        SolrParams params = req.getParams();
        String q = params.get(CommonParams.Q);
        String fl = params.getParams(CommonParams.FL);
        String rows = params.get(CommonParams.ROWS);

5)I create a Solr object that let's me connect to my Solr Cloud
        String zkHostString = "localhost:5181";
        SolrClient solr = new 
        CloudSolrClient.Builder().withZkHost(zkHostString).build();

6) Here is where I need help
            a) How do I use the unpacked Solr Parameters from the 
            original query and make a call to the "solr" object to 
            return results.
            b) How do I make use of the "resp" object?
            c) Most of the examples that I found on the internet show 
            how to print the results to STDOUT. However, since I am 
            using a custom handler I would like to display the results 
            back to the user (in this case, SOLR Admin or the browser).```

Any help is truly appreciated.

Thanks

        public class SolrQueryTest extends 
        org.apache.solr.handler.component.SearchHandler {

    String zkHostString = "localhost:5181";
    SolrClient solr = new 
        CloudSolrClient.Builder().withZkHost(zkHostString).build();

    private static final Logger log = 
        Logger.getLogger(SolrQueryTest.class.getName());


    public void handleRequestBody(SolrRequest req, SolrResponse 
        resp) throws Exception {

        SolrParams params = req.getParams();
    String q = params.get(CommonParams.Q);
    String rows = params.get(CommonParams.ROWS);

    SolrQuery query = new SolrQuery(q);
    query.setShowDebugInfo(true);
    query.set("indent", "true");
    // need to know how to call SOLR using the above query 
        parameters
    //Once the response is received how to send it back to the 
        browser and NOT STDOUT


    }
}



0 个答案:

没有答案