我有一个ElasticSearch查询,通过它我可以通过Kibana获取数据。我在Java应用程序中也需要同样的请求。Kibana生成的查询如下:
{
"version": true,
"size": 500,
"sort": [
{
"@timestamp": {
"order": "desc",
"unmapped_type": "boolean"
}
}
],
"_source": {
"excludes": []
},
"aggs": {
"2": {
"date_histogram": {
"field": "@timestamp",
"fixed_interval": "30s",
"time_zone": "Europe/Berlin",
"min_doc_count": 1
}
}
},
"stored_fields": [
"*"
],
"script_fields": {},
"docvalue_fields": [
{
"field": "@timestamp",
"format": "date_time"
}
],
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "ORDERID=120019",
"analyze_wildcard": true,
"time_zone": "Europe/Berlin"
}
}
],
"filter": [
{
"match_phrase": {
"service": {
"query": "some-service-app"
}
}
},
{
"range": {
"@timestamp": {
"format": "strict_date_optional_time",
"gte": "2020-02-24T09:12:41.685Z",
"lte": "2020-02-24T09:27:41.685Z"
}
}
}
],
"should": [],
"must_not": []
}
},
"highlight": {
"pre_tags": [
"@kibana-highlighted-field@"
],
"post_tags": [
"@/kibana-highlighted-field@"
],
"fields": {
"*": {}
},
"fragment_size": 2147483647
}
}
我试图使用ElasticSearch Java API编写相同的查询,但得到的结果完全不同,根本不符合预期的输出。
你能帮我想出一个正确的吗?我现在的做法如下。
final QueryBuilder query = QueryBuilders.boolQuery()
.must(QueryBuilders.simpleQueryStringQuery("some-service-app").field("service"))
.must(QueryBuilders.simpleQueryStringQuery("INFO").field("severity"))
.must(QueryBuilders.rangeQuery("@timestamp").from(now.minusDays(15)))
.must(QueryBuilders.simpleQueryStringQuery("ORDERID=120019"));
final SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(query);
final SearchRequest searchRequest = new SearchRequest(targetIndexName);
searchRequest.source(sourceBuilder);
final SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
不幸的是,即使使用错误的"服务"值,我的代码也会返回错误的结果。
你能帮我把Java请求和JSON请求对齐吗?
1(分数不会相同。Filter不计入分数,但在java代码中,您使用的是query,因此排序会有所不同。
阅读:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html
2( 我想你不想完全重现基巴纳的质疑。例如,我在json中没有找到"severity"字段,并且您的java中缺少聚合部分,您到底在寻找什么?。。。。请分享您的映射
3( 请比较kibana和您的文档时间戳之间的日期时间。有时随着时区的变化,kibana会显示不同的时间。
4( 正如评论中所说,调试时必须有生成的JSON,这可能会对您有所帮助。