Overriding the Query Pipeline in Lucidworks Fusion 3.x - Capture Club
kmcowan 2 May, 2018 1

Overriding the Query Pipeline in Lucidworks Fusion 3.x

data-pipeline
One of the most common questions I come across when discussing a query pipeline solution would have to be this: “How do I alter or transform the final result set?”
The short answer, in Fusion 3, is that you’re not able to modify that in a query pipeline, but that you would need a middle tier process.
A middle-tier process is advisable in any case, but I digress.
So let’s say that you really did need to modify the results, or send alternate headers, or even send a custom JSON formatted response. Generally, this is something people will tell you can’t be done.
The truth, though, is that it is entirely possible, albeit a bit of a hack. 😉
So moving on: We start by creating a new query pipeline in Fusion:
screenshot-2018-05-02-06-18-39
Notice that there are four stages created for you by default. We’re not interested in any of these, so we’ll turn them all off and create our own JavaScript stage.
screenshot-2018-05-02-06-22-14
This is where all the heavy lifting in our query pipeline will take place. Enabling any of the other stages will complicate things, and could potentially cause performance issues. The key here is that we don’t want to make two queries, just one, and a query pipeline tends to want to make some sort of query by default, thus we need to temper this inclination with a JavaScript stage.
A JavaScript stage in a query pipeline is void. We don’t return any documents from the function itself. What we’ll do is write directly to our output stream.
The foundation for this is to alter the unfortunatley-named QueryRequestAndResponse object.

    function(request){
        var someObject = new java.lang.String("{\"response\":{\"numFound\":7,\"start\":0,\"maxScore\":1.0,\"over\":"+json.toString()+" }}");//
  var headers = {"X-Fusion": "Word" };
  var  stream = new java.io.ByteArrayInputStream(someObject.getBytes("UTF-8"));
  var v2 = new com.lucidworks.apollo.solr.response.RawResponse(stream,"application/json","UTF-8");
  var newResponse = new com.lucidworks.apollo.pipeline.query.Response(new javax.ws.rs.core.MultivaluedHashMap(headers), v2);
  var queryRequestAndResponse = com.lucidworks.apollo.pipeline.query.QueryRequestAndResponse.create(request,newResponse,1);
   return queryRequestAndResponse;
     }

You’ll note something odd here: We are returning something. Generally, that’s not something you see in a query pipeline. In this case, though, that is what we want to do. You’ll also notice that we’re writing both custom headers and custom data here. You should note, though, that headers sent will ultimately be lower-cased. Thus our “X-Fusion” header will wind up being “x-fusion”.
With this, we have now effectively overridden our query pipeline response. The next big question would be, what do we do with it? We’ll disucss that in our next blog, Part II,  Overriding the Query Pipeline: Making an Asynchronous Solr Query.

One thought on “Overriding the Query Pipeline in Lucidworks Fusion 3.x

Leave a Reply

Your email address will not be published. Required fields are marked *