[英]java elasticsearch query without java API
I try to query an existing elasticsearch base in java without using the Java API. 我尝试在不使用Java API的情况下使用Java查询现有的Elasticsearch基础。 This elasticsearch base belongs to an ELK cluster.
该Elasticsearch基属于ELK集群。
The correct cURL query is : 正确的cURL查询是:
curl -XGET 'http://10.60.74.134:9200/logstash-2015.04.09/_search?pretty' -d '{
"facets": {
"0": {
"date_histogram": {
"field": "@timestamp",
"interval": "5m"
},
"global": true,
"facet_filter": {
"fquery": {
"query": {
"filtered": {
"query": {
"query_string": {
"query": "*"
}
},
"filter": {
"bool": {
"must": [
{
"range": {
"@timestamp": {
"from": 1428558001338,
"to": 1428579601338
}
}
},
{
"terms": {
"_type": [
"akaoatg-monitoring"
]
}
}
]
}
}
}
}
}
}
}
},
"size": 0
}'
which works perfectly fine and returns me my JSON results : 可以正常工作,并返回我的JSON结果:
{
"took" : 185,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 9106263,
"max_score" : 0.0,
"hits" : [ ]
},
"facets" : {
"0" : {
"_type" : "date_histogram",
"entries" : [ {
"time" : 1428458700000,
"count" : 2429
}, {
"time" : 1428459000000,
"count" : 21128
}, {
"time" : 1428459300000,
"count" : 21354
} ]
}
}
}
I tried to get the same results using an http request in java : 我试图使用java中的http请求获得相同的结果:
try {
URL url = new URL("http://10.60.74.134:9200/logstash-2015.04.09/_search?pretty'-d'{\"facets\":{\"terms\":{\"terms\":{\"field\":\"_type\",\"size\":10,\"order\":\"count\",\"exclude\":[]},\"facet_filter\":{\"fquery\":{\"query\":{\"filtered\":{\"query\":{\"bool\":{\"should\":[{\"query_string\":{\"query\":\"*\"}}]}},\"filter\":{\"bool\":{\"must\":[{\"range\":{\"@timestamp\":{\"from\":1428558001341,\"to\":1428579601341}}},{\"terms\":{\"_type\":[\"akaoatg-monitoring\"]}}]}}}}}}}},\"size\":0}");
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
String strTemp;
while((strTemp = br.readLine()) != null){
System.out.println(strTemp);
}
} catch (Exception ex) {
ex.printStackTrace();
}
The URL I am using here is the cURL request formatted to fit in an http request. 我在这里使用的URL是设置为适合http请求的cURL请求。 This request returns me a single String which does not contain the same result.
此请求返回一个不包含相同结果的字符串。 here is a part of the java result :
这是java结果的一部分:
{"took":22,
"timed_out":false,
"_shards":{"total":5,
"successful":5,
"failed":0},
"hits":{"total":4621367,
"max_score":1.0,
"hits":[{"_index":"logstash-2015.04.09",
"_type":"xxx",
"_id":"xxx",
"_score":xxx,
"_source":{"@version":"xxx",
"@timestamp":"2015-04-09T01:09:59.347Z",
"host":"xxx",
"type":"xxx",
"sys_priority":"xxx",
"sys_timestamp":"xxx",
"logsource":"xxx",
"application":"xxx",
"year":"2015",
"month":"04",
"day":"09",
"hour":"01",
"minute":"09",
"second":"58",
"trace_level":"3",
"host_name":"xxx",
"adh_port":"xxx",
"timestamp_adh":1428541798954,
"time_adh":27,
"adh_uuid":"xxx",
"Service":"xxx",
"ReturnCode":"0",
"ErrorMessage":"null",
"Site":"null",
"BaseType":"null",
"PlatForm":"0",
"Cad_sender":"",
"Domain":"xxx",
"Freshness":"9",
"ClientProcessID":"xxx",
"CallMode":"S",
"SystemMode":"R",
"Sad_receiver":"",
"ConnectionType":"IP",
"DataFormat":"",
"HeaderType":"H4",
"AdhesionVersion":"null",
"Length":"10",
"ConnectionInfo":"null",
"ConnectionInfoKey":"null",
"Comments":"null",
"ActionCode":"null",
"TimeStamp":20150409010958,
"ServerProgramName":"null",
"TransactionCode":"null",
"TraceLevel":"null",
"LU":"null",
"HostName":"xxx",
"Port":"xxx",
"Timer":20,
"SendQueue":"null",
"ReturnQueue":"",
"PDM":"",
"RFU":"null",
"FTU":"",
"ActivationFlag":"null",
"HistoryQueue":"null",
"ErrorQueue":"null",
"CallReference":"xxx",
"IPAddress":"xxx",
"MessageType":"I",
"ProgramName":"null",
"UserName":"xxx",
"BeginTime":"24:00:00",
"EndTime":"24:00:00",
"duration":0,
"cnx_running":0,
"cnx_max":0}}]}}
Any idea of what I'm doing wrong ? 有什么想法我做错了吗?
Your request sent via Java is malformed: there is no "facets": { "0": {...}} part in it. 您通过Java发送的请求格式不正确:其中没有“ facets”:{“ 0”:{...}}部分。 (At least the '0' is missing).
(至少缺少“ 0”)。 Hence the server seems to ignore that part and just give you a bunch of raw entries (the default output)
因此,服务器似乎忽略了那部分,只给了您一堆原始条目(默认输出)
The problem was the type of request, I had to send a POST request instead of a GET one. 问题是请求的类型,我不得不发送POST请求而不是GET请求。 Used this code :
使用此代码:
String url = "http://10.60.74.134:9200/logstash-2015.04.09/_search?pretty'-d'";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String urlParameters = "{\"facets\":{\"0\":{\"date_histogram\":{\"field\":\"@timestamp\",\"interval\":\"5m\"},\"global\":true,\"facet_filter\":{\"fquery\":{\"query\":{\"filtered\":{\"query\":{\"query_string\":{\"query\":\"*\"}},\"filter\":{\"bool\":{\"must\":[{\"range\":{\"@timestamp\":{\"from\":1428558001338,\"to\":1428579601338}}},{\"terms\":{\"_type\":[\"akaoatg-monitoring\"]}}]}}}}}}}},\"size\":0}";
thanks to this tutorial 感谢本教程
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.