{"id":1546,"date":"2018-05-03T13:05:21","date_gmt":"2018-05-03T13:05:21","guid":{"rendered":"http:\/\/capture.ccio.us\/?p=1546"},"modified":"2018-05-03T13:05:21","modified_gmt":"2018-05-03T13:05:21","slug":"overriding-query-pipeline-pt-ii","status":"publish","type":"post","link":"https:\/\/capture.club\/portal\/2018\/05\/03\/overriding-query-pipeline-pt-ii\/","title":{"rendered":"Overriding the Query Pipeline, Pt II"},"content":{"rendered":"<body><p><\/p><img decoding=\"async\" class=\"alignnone size-full wp-image-1462\" src=\"http:\/\/capture.ccio.us\/wp-content\/uploads\/2018\/05\/Data-Pipeline.jpg\" alt=\"data-pipeline\" width=\"405\" height=\"228\" loading=\"lazy\"><br>\nIn our prior discussion, I showed you how to <a href=\"http:\/\/capture.ccio.us\/overriding-query-pipeline-lucidworks-fusion-3-x\/\">override the query pipeline in Fusion<\/a>. So what do we do now? Most often times, the reason people want to override is to manipulate the documents, or perform some sort of transformation on the result. The problem is that in a query pipeline you never actually have a hook to get your hands on the results. To do this, we need to make an asynchronous REST call and run our query, pull back the results and perform our tranformation, and then send them on along.<br>\nTo accomplish that, we\u2019ll build on our query pipeline override.<br>\nIn order to make our rest call, we\u2019re going to need to authenticate. To do that, we\u2019ll have to spin up our <a href=\"http:\/\/hc.apache.org\/httpcomponents-client-ga\/httpclient\/apidocs\/org\/apache\/http\/client\/HttpClient.html\">Apache HttpClient<\/a>, like so:\n<pre>function getAuthHttpClient(){\nvar UsernamePasswordCredentials = org.apache.http.auth.UsernamePasswordCredentials;\n        var AuthScope = org.apache.http.auth.AuthScope;\n        var BasicCredentialsProvider = org.apache.http.impl.client.BasicCredentialsProvider;\n        var HttpClientBuilder = org.apache.http.impl.client.HttpClientBuilder;\n        var HttpPost = org.apache.http.client.methods.HttpPost;\n        var StringEntity = org.apache.http.entity.StringEntity;\n        var pwd = \"YOUR_PASSWORD\";\n        var user = \"ADMIN_USER_NAME\";\n        var fusionUrl = \"http:\/\/localhost:8764\";\/\/ base url to Fusion\n        var client = null;\n        var e = java.lang.Exception;\n        try{\n         \/\/ create our auth json to send\n        var authJson = \"{\\\"username\\\":\\\"\" + user + \"\\\", \\\"password\\\":\\\"\" + pwd + \"\\\"}\";\n                var authUrl = fusionUrl + \"\/api\/session?realmName=native\";\n                var provider = new BasicCredentialsProvider();\n                var credentials = new UsernamePasswordCredentials(user, pwd);\n                provider.setCredentials(AuthScope.ANY, credentials);\n                \/\/ spin up our HttpClient\n                var client = HttpClientBuilder.create()\n                .setDefaultCredentialsProvider(provider)\n                .build();\n                var session = new HttpPost(authUrl);\n                var params = new StringEntity(authJson);\n                session.addHeader(\"content-type\", \"application\/json\");\n                session.setEntity(params);\n                \/\/ execute the login request.\n                var response = client.execute(session);\n        } catch (e){\n             logger.error(\"Error getting httpclient \" + e.getLocalizedMessage());\n         }\nreturn client;\n        }\n<\/pre>\n<p>Specifically, I don\u2019t use the native <a href=\"https:\/\/docs.oracle.com\/javame\/8.0\/api\/gcf\/api\/javax\/microedition\/io\/HttpConnection.html\">Java HttpConnection<\/a> here, because I want to maintain state. The call we make to the <a href=\"https:\/\/doc.lucidworks.com\/fusion\/3.1\/REST_API_Reference\/Authentication_and_Authorization_APIs\/Sessions-API.html\">Session API<\/a>\u00a0sets a cookie in our HttpClient, that will be used for subsequent requests.<br>\nWith an authenticated client, we can go about making our Solr Query.<\/p>\n<pre> function doHttpQuery(request) {\n    var BufferedReader = java.io.BufferedReader;\n    var InputStreamReader = java.io.InputStreamReader;\n    var HttpGet = org.apache.http.client.methods.HttpGet;\n    var e = java.lang.Exception;\n    var url = \"http:\/\/localhost:8764\/api\/apollo\/solr\/_test_\/select?q=*:*&amp;wt=json&amp;rows=1&amp;fl=id\";\n    var JSONObject = org.json.JSONObject;\n    var json = new JSONObject();\n    try {\n        var client = getAuthHttpClient();\n        json.put(\"msg1\", \"got auth client okay\");\n        var httpget = new HttpGet(url);\n        var response = client.execute(httpget);\n        json.put(\"msg2\", \"execute httpget okay...\");\n        if (response !== null) {\n            var rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));\n            var result = \"\";\n            var line = \"\";\n            while ((line = rd.readLine()) !== null) {\n                result += line;\n            }\n            json.put(\"result\", result);\n        }\n    } catch (e) {\n        json.put(\"error\", \"some error occurred...\");\n    }\n    return json;\n}\n<\/pre>\n<p>So here we grab our newly-authenticated HttpClient, and make a SolrQuery of our own choosing, which returns the basic response, which we thus set as \u201cresult\u201d in our JSONObject.<br>\nNow that we have our documents in had, we can do our massaging and\/or transformation and send them on along in the request, as we did in <a href=\"http:\/\/capture.ccio.us\/overriding-query-pipeline-lucidworks-fusion-3-x\/\">Part I<\/a>.<br>\nHappy Searching!<\/p>\n<\/body>","protected":false},"excerpt":{"rendered":"<p>In our prior discussion, I showed you how to override the query pipeline in Fusion. So what do we do now? Most often times, the reason people want to override is to manipulate the documents, or perform some sort of transformation on the result. The problem is that in a query pipeline you never actually [&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-1546","post","type-post","status-publish","format-standard","hentry"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/posts\/1546","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=1546"}],"version-history":[{"count":0,"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/posts\/1546\/revisions"}],"wp:attachment":[{"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/media?parent=1546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/categories?post=1546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/tags?post=1546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}