{"id":1461,"date":"2018-05-02T12:39:29","date_gmt":"2018-05-02T12:39:29","guid":{"rendered":"http:\/\/capture.ccio.us\/?p=1461"},"modified":"2018-05-02T12:39:29","modified_gmt":"2018-05-02T12:39:29","slug":"overriding-query-pipeline-lucidworks-fusion-3-x","status":"publish","type":"post","link":"https:\/\/capture.club\/portal\/2018\/05\/02\/overriding-query-pipeline-lucidworks-fusion-3-x\/","title":{"rendered":"Overriding the Query Pipeline in Lucidworks Fusion 3.x"},"content":{"rendered":"<body><p><\/p><img decoding=\"async\" class=\"alignnone size-full wp-image-1462\" style=\"align: center; margin-left: 50px;\" src=\"http:\/\/capture.ccio.us\/wp-content\/uploads\/2018\/05\/Data-Pipeline.jpg\" alt=\"data-pipeline\" width=\"405\" height=\"228\" loading=\"lazy\"><br>\nOne of the most common questions I come across when discussing a query pipeline solution would have to be this: \u201cHow do I alter or transform the final result set?\u201d<br>\nThe short answer, in Fusion 3, is that you\u2019re not able to modify that in a query pipeline, but that you would need a middle tier process.<br>\nA middle-tier process is advisable in any case, but I digress.<br>\nSo let\u2019s 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\u2019t be done.<br>\nThe truth, though, is that it is entirely possible, albeit a bit of a hack. \ud83d\ude09<br>\nSo moving on: We start by creating a new query pipeline in Fusion:<br>\n<img decoding=\"async\" class=\"alignnone size-full wp-image-1465\" src=\"http:\/\/capture.ccio.us\/wp-content\/uploads\/2018\/05\/Screenshot-2018-05-02-06.18.39.png\" alt=\"screenshot-2018-05-02-06-18-39\" width=\"1240\" height=\"706\" loading=\"lazy\"><br>\nNotice that there are four stages created for you by default. We\u2019re not interested in any of these, so we\u2019ll turn them all off and create our own JavaScript stage.<br>\n<img decoding=\"async\" class=\"alignnone size-full wp-image-1467\" src=\"http:\/\/capture.ccio.us\/wp-content\/uploads\/2018\/05\/Screenshot-2018-05-02-06.22.14.png\" alt=\"screenshot-2018-05-02-06-22-14\" width=\"904\" height=\"519\" loading=\"lazy\"><br>\nThis 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\u2019t 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.<br>\nA\u00a0JavaScript stage in a query pipeline is void. We don\u2019t return any documents from the function itself. What we\u2019ll do is write directly to our output stream.<br>\nThe foundation for this is to alter the unfortunatley-named QueryRequestAndResponse object.\n<pre>    function(request){\n        var someObject = new java.lang.String(\"{\\\"response\\\":{\\\"numFound\\\":7,\\\"start\\\":0,\\\"maxScore\\\":1.0,\\\"over\\\":\"+json.toString()+\" }}\");\/\/\n  var headers = {\"X-Fusion\": \"Word\" };\n  var  stream = new java.io.ByteArrayInputStream(someObject.getBytes(\"UTF-8\"));\n  var v2 = new com.lucidworks.apollo.solr.response.RawResponse(stream,\"application\/json\",\"UTF-8\");\n  var newResponse = new com.lucidworks.apollo.pipeline.query.Response(new javax.ws.rs.core.MultivaluedHashMap(headers), v2);\n  var queryRequestAndResponse = com.lucidworks.apollo.pipeline.query.QueryRequestAndResponse.create(request,newResponse,1);\n   return queryRequestAndResponse;\n     }\n<\/pre>\n<p>You\u2019ll note something odd here: We are returning something. Generally, that\u2019s not something you see in a query pipeline. In this case, though, that is what we want to do. You\u2019ll also notice that we\u2019re writing both custom headers and custom data here. You should note, though, that headers sent will ultimately be lower-cased. Thus our \u201cX-Fusion\u201d header will wind up being \u201cx-fusion\u201d.<br>\nWith this, we have now effectively overridden our query pipeline response. The next big question would be, what do we do with it? We\u2019ll disucss that in our next blog, <a href=\"http:\/\/capture.ccio.us\/overriding-query-pipeline-pt-ii\/\">Part II,\u00a0 Overriding the Query Pipeline: Making an Asynchronous Solr Query<\/a>.<\/p>\n<\/body>","protected":false},"excerpt":{"rendered":"<p>One of the most common questions I come across when discussing a query pipeline solution would have to be this: \u201cHow do I alter or transform the final result set?\u201d The short answer, in Fusion 3, is that you\u2019re not able to modify that in a query pipeline, but that you would need a middle [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[],"tags":[],"class_list":["post-1461","post","type-post","status-publish","format-standard","hentry"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/posts\/1461","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/comments?post=1461"}],"version-history":[{"count":0,"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/posts\/1461\/revisions"}],"wp:attachment":[{"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/media?parent=1461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/categories?post=1461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/tags?post=1461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}