com.nasa.ElasticSearchAdapter.java Source code

Java tutorial

Introduction

Here is the source code for com.nasa.ElasticSearchAdapter.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 com.nasa;

import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author alexandru.albu
 */
public class ElasticSearchAdapter {

    private static String ES_URL = "http://elasticsearch:9200";

    private JsonObject doPost(String url, String data) throws IOException {

        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        //add reuqest header
        con.setRequestMethod("POST");

        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(data);
        wr.flush();
        wr.close();

        int responseCode = con.getResponseCode();
        //System.out.println("\nSending 'POST' request to URL : " + url);
        //System.out.println("Post parameters : " + data);
        //System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        //print result
        //System.out.println(response.toString());
        return new JsonObject(response.toString());
    }

    public JsonObject getSymptoms(String word) throws IOException {
        JsonObject result = new JsonObject();
        JsonArray matchesArr = new JsonArray();
        String data = "{\n" + "    \"size\": 0,\n" + "    \"aggs\" : {\n" + "        \"langs\" : {\n"
                + "            \"terms\" : { \"field\" : \"symptoms.name\" }\n" + "        }\n" + "    }\n" + "}";
        JsonObject response = doPost(ES_URL + "/healthy-travel/feedback/_search", data);

        JsonArray items = response.getJsonObject("aggregations").getJsonObject("langs").getJsonArray("buckets");

        for (Object item : items) {
            JsonObject obj = (JsonObject) item;
            String key = obj.getString("key");
            if (key != null && obj.getString("key").startsWith(word)) {
                matchesArr.add(key);
            }
        }

        result.put("symptoms", matchesArr);

        return result;
    }

    public JsonObject getConditions(String word) throws IOException {
        JsonObject result = new JsonObject();
        JsonArray matchesArr = new JsonArray();
        String data = "{\n" + "    \"size\": 0,\n" + "    \"aggs\" : {\n" + "        \"langs\" : {\n"
                + "            \"terms\" : { \"field\" : \"condition\" }\n" + "        }\n" + "    }\n" + "}";
        JsonObject response = doPost(ES_URL + "/healthy-travel/feedback/_search", data);

        JsonArray items = response.getJsonObject("aggregations").getJsonObject("langs").getJsonArray("buckets");

        for (Object item : items) {
            JsonObject obj = (JsonObject) item;
            String key = obj.getString("key");
            if (key != null && obj.getString("key").startsWith(word)) {
                matchesArr.add(key);
            }
        }

        result.put("conditions", matchesArr);

        return result;
    }

    public JsonObject getMap(String lat, String lon, String zoom, String count) throws IOException {
        String data = "{\n" + "    \"size\": " + count + ",\n" + "   \"query\": {\n" + "      \"filtered\": {\n"
                + "         \"query\": {\n" + "            \"match_all\": {}\n" + "         },\n"
                + "         \"filter\": {\n" + "            \"geo_distance\": {\n"
                + "               \"distance\": \"" + zoom + "km\",\n" + "               \"location\": {\n"
                + "                  \"lat\": " + lat + ",\n" + "                  \"lon\": " + lon + "\n"
                + "               }\n" + "            }\n" + "         }\n" + "      }\n" + "   }\n" + "}";

        JsonObject response = doPost(ES_URL + "/healthy-travel/feedback/_search", data);

        return response;
    }

    public JsonArray getCheck(Float lat, Float lon) throws IOException {
        JsonArray matchesArr = new JsonArray();
        String data = "{\n" + "    \"size\": 0,\n" + "   \"query\": {\n" + "      \"filtered\": {\n"
                + "         \"query\": {\n" + "            \"match_all\": {}\n" + "         },\n"
                + "         \"filter\": {\n" + "            \"geo_distance\": {\n"
                + "               \"distance\": \"100km\",\n" + "               \"location\": {\n"
                + "                  \"lat\": " + lat + ",\n" + "                  \"lon\": " + lon + "\n"
                + "               }\n" + "            }\n" + "         }\n" + "      }\n" + "   },\n"
                + "   \"aggs\": {\n" + "      \"langs\": {\n" + "         \"terms\": {\n"
                + "            \"field\": \"condition\"\n" + "         }\n" + "      }\n" + "   }\n" + "}";
        JsonObject response = doPost(ES_URL + "/healthy-travel/feedback/_search", data);

        JsonArray items = response.getJsonObject("aggregations").getJsonObject("langs").getJsonArray("buckets");

        for (Object item : items) {
            JsonObject obj = (JsonObject) item;
            String key = obj.getString("key");
            int value = obj.getInteger("doc_count");

            if (key != null && value > 0) {
                JsonObject cond = new JsonObject();
                cond.put("name", key);
                cond.put("reported_cases", value);
                matchesArr.add(cond);
            }
        }

        return matchesArr;
    }

    public JsonObject postFeedback(JsonObject feedback) throws IOException {
        JsonObject result = new JsonObject();
        String data = feedback.toString();
        // normalize or remove non ascii characters
        data = Normalizer.normalize(data, Normalizer.Form.NFD);
        data = data.replaceAll("[^\\x00-\\x7F]", "");
        JsonObject response = doPost(ES_URL + "/healthy-travel/feedback", data);

        result.put("success", response.getJsonObject("_shards").getInteger("successful") > 0);

        return result;
    }
}