Overriding the Query Pipeline, Pt III - Capture Club
kmcowan 3 May, 2018 0

Overriding the Query Pipeline, Pt III

data-pipeline
When we last left our rogue query pipeline in Pt II, we had made our Solr query and had tucked the response into a JSON object.  This call was made from our “main” function, which is incidentally the last function — an anonymous function — declared at the end of your stage.  For example:

  function doFoo(){
  }
  funciton Bar(){
    return doFoo();
  }
  funtion(){
      var bar = new Bar();
   }

In the above example, the only function called directly by Fusion is the anonymous function at the end, but moving on.
Thus we have our documents in hand. Let’s do something with them now:

    // make the Solr query call:
    var docs = doHttpQuery(request);
    // get the result, returns type ConString
    var result = docs.get("result");
    // create a JSON object using the Nashorn parser.
    var resultJson = JSON.parse(result.toString());
    // this is your docs array that you'll modify and load back into the request.
    var jsonDocs = resultJson.response.docs;
   // loop through the docs and make whatever changes.
    for(var i=0; i<jsonDocs.length; i++){
        jsonDoc[i]["some_field_name"] = "";
    }
    // add the docs to our result.
    json.put("docs_result", JSON.stringify(jsonDocs));
    var someObject = new java.lang.String("{\"response\":{\"numFound\":7,\"start\":0,\"maxScore\":1.0,\"over\":" + json.toString() + " }}"); //
    var stream = new java.io.ByteArrayInputStream(someObject.getBytes("UTF-8"));
    var v2 = new com.lucidworks.apollo.solr.response.RawResponse(stream, "application/json", "UTF-8");
    // send the response.  If we wanted to send headers, we'd replace the null arg with a map.
    var newResponse = new com.lucidworks.apollo.pipeline.query.Response(null, v2);
    var queryRequestAndResponse = com.lucidworks.apollo.pipeline.query.QueryRequestAndResponse.create(request, newResponse, 1);
    return queryRequestAndResponse;

And, Viola! We have successfully made an asynchrouns REST query call, modified the result, and set that result on along down the pipeline.
The query pipeline override is a powerful tool, but as with any significant hack, you should use it wisely. Mis-use or over-use can cause performance trouble. With great power comes great responsibility. Always use Best Practices, and don’t go hog wild. JavaScript stages are powerful tools, but need to be used in moderation.
Happy Searching!

Leave a Reply