gr.uoc.nlp.opinion.analysis.suggestion.ElasticSearchIntegration.java Source code

Java tutorial

Introduction

Here is the source code for gr.uoc.nlp.opinion.analysis.suggestion.ElasticSearchIntegration.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gr.uoc.nlp.opinion.analysis.suggestion;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author smyrgeorge
 */
public class ElasticSearchIntegration {

    private final String elasticSearchURI = "http://localhost:9200/comments/sentences/";
    private final String elasticSearchQueryURI = "http://localhost:9200/_all/_search?";

    /**
     *
     */
    public ElasticSearchIntegration() {

    }

    /**
     *
     * @param commentID
     * @param sentenceID
     * @param document
     */
    public void sendToElasticSearch(String commentID, String sentenceID, String document) {

        try {
            //fix uri
            String url = elasticSearchURI + commentID + "_" + sentenceID;

            //open connection
            URL obj = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("PUT");

            //append data to http packet
            String data = "{\"comment\": \"" + commentID + "\"," + " \"sentence\": \"" + sentenceID + "\","
                    + " \"document\": \"" + document + "\"}";

            //write data to http socket
            try (OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream())) {
                out.write(data);
                out.close();
            }

            //get response data
            String line = "";
            try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {

                while ((line = in.readLine()) != null) {
                    System.out.println(line);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *
     * @param commentID
     * @param sentenceID
     */
    public void removeFromElasticSearch(String commentID, String sentenceID) {
        CloseableHttpClient httpclient = HttpClients.createDefault();

        String deleteURI = this.elasticSearchURI + commentID + "_" + sentenceID;
        System.out.println(deleteURI);

        // create DELETE REQUEST
        HttpDelete httpDelete = new HttpDelete(deleteURI);

        // send request
        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httpDelete);
        } catch (IOException ex) {
            Logger.getLogger(ElasticSearchIntegration.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {
            // close response (ideally inside a finally clause in a try/catch)
            response.close();
        } catch (IOException ex) {
            Logger.getLogger(ElasticSearchIntegration.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     *
     * @param query
     * @return
     */
    public double executeQuery(String query) {

        double score = 0.0;

        try {
            String url = elasticSearchQueryURI;

            //open connection
            URL obj = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");

            //write data to socket
            try (OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream())) {
                out.write(query);
                out.close();
            }

            String json = "";
            String line = "";
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

            //get response
            while ((line = in.readLine()) != null) {
                json = json + line + " ";
            }

            //            System.out.println(json);
            //transfor response to JSONObject
            JSONObject jsonObj = new JSONObject(json);
            try {
                //get max_score
                score = jsonObj.getJSONObject("hits").getDouble("max_score");
            } catch (JSONException ex) {
                score = 0.0;
            }

            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException ex) {
            Logger.getLogger(ElasticSearchIntegration.class.getName()).log(Level.SEVERE, null, ex);
        }

        return score;
    }

    //    /**
    //     *
    //     * @param commentID
    //     * @param sentenceID
    //     * @param query
    //     * @return
    //     */
    //    public String queryBuilder(String commentID, String sentenceID, String query){
    //        
    //        if(commentID.contains(".")){
    //            commentID = commentID.substring(0, commentID.indexOf("."));
    //        }
    //        
    //        if(!sentenceID.contains("s")){
    //            sentenceID = "s"+sentenceID;
    //        }
    //        
    //        //create elastisearch query
    //        String q = "{\"query\":{\"filtered\":{\"query\":{\"bool\":{\"should\":[{\"query_string\":{\"query\":\""+query
    //                +"\"}}]}},\"filter\":{\"bool\":{\"must\":[{\"fquery\":{\"query\":{\"query_string\":{\"query\":\"comment:(\\\""
    //                +commentID+"\\\")\"}},\"_cache\":true}},{\"fquery\":{\"query\":{\"query_string\":{\"query\":\"sentence:(\\\""
    //                +sentenceID+"\\\")\"}},\"_cache\":true}}],\"must_not\":[{\"fquery\":{\"query\":{\"query_string\":{\"query\":\"Qst\"}},\"_cache\":true}}]}}}},"
    //                +"\"highlight\":{\"fields\":{},\"fragment_size\":2147483647,\"pre_tags\":[\"@start-highlight@\"],\"post_tags\":[\"@end-highlight@\"]},\"size\":500,"
    //                +"\"sort\":[{\"_score\":{\"order\":\"desc\",\"ignore_unmapped\":true}}]}";
    //        
    //        return q;
    //    }

    /**
     *
     * @param commentID
     * @param sentenceID
     * @param query
     * @return
     */
    public String queryBuilder(String commentID, String sentenceID, String query) {
        if (commentID.contains(".")) {
            commentID = commentID.substring(0, commentID.indexOf("."));
        }
        //        System.out.println(commentID);
        if (!sentenceID.contains("s")) {
            sentenceID = "s" + sentenceID;
        }
        //        System.out.println(sentenceID);

        String q = "{\n" + "  \"query\": {\n" + "    \"filtered\": {\n" + "      \"query\": {\n"
                + "        \"bool\": {\n" + "          \"should\": [\n" + "            {\n"
                + "              \"query_string\": {\n" + "                \"query\": \"" + query + "\"\n"
                + "              }\n" + "            }\n" + "          ]\n" + "        }\n" + "      },\n"
                + "      \"filter\": {\n" + "        \"bool\": {\n" + "          \"must\": [\n" + "            {\n"
                + "              \"fquery\": {\n" + "                \"query\": {\n"
                + "                  \"query_string\": {\n" + "                    \"query\": \"comment:(\\\""
                + commentID + "\\\")\"\n" + "                  }\n" + "                },\n"
                + "                \"_cache\": true\n" + "              }\n" + "            },\n"
                + "            {\n" + "              \"fquery\": {\n" + "                \"query\": {\n"
                + "                  \"query_string\": {\n" + "                    \"query\": \"sentence:(\\\""
                + sentenceID + "\\\")\"\n" + "                  }\n" + "                },\n"
                + "                \"_cache\": true\n" + "              }\n" + "            }\n" + "          ],\n"
                + "          \"must_not\": [\n" + "            {\n" + "              \"fquery\": {\n"
                + "                \"query\": {\n" + "                  \"query_string\": {\n"
                + "                    \"query\": \"Qst\"\n" + "                  }\n" + "                },\n"
                + "                \"_cache\": true\n" + "              }\n" + "            }\n" + "          ]\n"
                + "        }\n" + "      }\n" + "    }\n" + "  },\n" + "  \"highlight\": {\n"
                + "    \"fields\": {},\n" + "    \"fragment_size\": 2147483647,\n" + "    \"pre_tags\": [\n"
                + "      \"@start-highlight@\"\n" + "    ],\n" + "    \"post_tags\": [\n"
                + "      \"@end-highlight@\"\n" + "    ]\n" + "  },\n" + "  \"size\": 500,\n" + "  \"sort\": [\n"
                + "    {\n" + "      \"_score\": {\n" + "        \"order\": \"desc\",\n"
                + "        \"ignore_unmapped\": true\n" + "      }\n" + "    }\n" + "  ]\n" + "}";

        return q;
    }
}