curl -XGET "http://127.0.0.1:9200/messages/_search?pretty" -d '
{
"size" : 10,
"query" : {
"bool" : {
"must" : [ {
"match" : {
"id" : {
"query" : "[some id]",
"type" : "boolean"
}
}
}, {
"nested" : {
"query" : {
"bool" : {
"must" : {
"match" : {
"agent" : {
"query" : "[some agent name]",
"type" : "boolean"
}
}
}
}
},
"path" : "agents"
}
} ]
}
}
}
}
The command requires a json body to be sent to the elastic search via the "GET" restful call. After some trial and error, I got this to work, below is the method implemented in Java.
public static String httpGet(String urlToRead, String data) {
URL url;
HttpURLConnection conn;
BufferedReader rd;
String line;
StringBuilder result = new StringBuilder();
try {
url = new URL(urlToRead);
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("GET");
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));
writer.write(data);
writer.flush();
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = rd.readLine()) != null) {
result.append(line);
}
rd.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return result.toString();
}
To call the above method and realize the curl query to ES above. just implement the following:
int size = 10;
String ipAddress = "127.0.0.1";
String url = "http://"+ipAddress+":9200/messages/_search?pretty";
String data = " { \"size\" : "+size+", \"query\" : { \"bool\" : { \"must\" : [ { \"match\" : { \"id\" : { \"query\" : \"[some id]\", \"type\" : \"boolean\" } } }, { \"nested\" : { \"query\" : { \"bool\" : { \"must\" : { \"match\" : { \"agent\" : { \"query\" : \"[some agent name]\", \"type\" : \"boolean\" } } } } }, \"path\" : \"agents\" } } ] } } } ";
String response = httpGet(url, data);
No comments:
Post a Comment