Solr: Use UpdateRequestProcessorChain to Execute Processor Chain Before Import Data


Scenario
I am sending query to a middle proxy layer which talks with remote Solr server, import the received documents into Local Solr server.

There are different ways to put data into Local Solr server.
1. Use core.getUpdateHandler(), we can add SolrInputDocuemt, delete by id or query.
2. We can use SolrRequestHandler like below:
SolrRequestHandler updateHandler = core.getRequestHandler("/update");
SolrQueryRequest req = new LocalSolrQueryRequest(core,solrParams);
updateHandler.handleRequest(req, solrQueryRsp);
Be sure to close the created SolrQueryRequest. Please refer to.

Compared with method 2, method 1 is better, as this pass the phrases- 1: Create a XML or JSON documents playload. 2: Parse the playload. 3: Create a SolrInputDocuemt from playload.
But with method 1, we can 't tell it to execute one processor chain.
Solution
If we want to tell Solr to execute one processor chain, before import data, we can get one UpdateRequestProcessorChain, create a processor, and use the processor to add a new SolrInputDocuemt. 
UpdateRequestProcessorChain updateChian = request.getCore().getUpdateProcessingChain("dedup");
UpdateRequestProcessor processor = updateChian.createProcessor(request,response);
ddUpdateCommand command = new AddUpdateCommand(request);
command.solrDoc = toSolrServerSolrDoc;
processor.processAdd(command);
The complete code looks like below

private static boolean importDocs(String remoteDocsStr,
      SolrQueryRequest request) throws IOException {
    Object obj = ObjectBuilder.fromJSON(remoteDocsStr);
    HashMap map = (HashMap) obj;
    HashMap responseMap = (HashMap) map.get("response");
    List lists = (List) responseMap.get("docs");
    
    UpdateRequestProcessorChain updateChian = request.getCore()
        .getUpdateProcessingChain("dedup");
    for (HashMap docJsonMap : lists) {
      SolrInputDocument toSolrServerSolrDoc = convertToSolrDoc(docJsonMap);
      AddUpdateCommand command = new AddUpdateCommand(request);
      command.solrDoc = toSolrServerSolrDoc;
      SolrQueryResponse response = new SolrQueryResponse();
      UpdateRequestProcessor processor = updateChian.createProcessor(request,
          response);
      processor.processAdd(command);
    }
}

Labels

adsense (5) Algorithm (69) Algorithm Series (35) Android (7) ANT (6) bat (8) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) Java (186) JavaScript (27) JSON (7) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) regex (5) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) xml (5)