弹性搜索:聚合请求中的时区字段不起作用

huangapple go评论89阅读模式
英文:

Elasticsearch: Aggregations request timezone field not working

问题

Sure, here's the translated content:

Postman创建的查询和QueryBuilder创建的请求是相同的,但是TimeZone字段不适用于Java请求。

以下是请求目标映射信息
```json
"timeArchived": {
    "type": "date",
    "format": "yyyy-MM-dd HH:mm:ss||epoch_millis"
}

以下是Java请求源代码

SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder query = QueryBuilders.boolQuery();
sourceBuilder.trackTotalHits(true);
sourceBuilder.size(0);
sourceBuilder.query(query);
RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("timeArchived").timeZone("Asia/Seoul").gte(sTime).lte(eTime);
query.filter(rangeQuery);
query.must(QueryBuilders.termQuery("domain", domain));

sourceBuilder.aggregation(AggregationBuilders.dateHistogram("mailDate").field("timeArchived").fixedInterval(DateHistogramInterval.hours(1)).timeZone(ZoneId.of("ROK")));

SearchRequest request = new SearchRequest();
request.source(sourceBuilder);
SearchResponse response = null;
try {
    response = client.search(request, RequestOptions.DEFAULT);
} catch (IOException e) {
    e.printStackTrace();
}

以下是创建的请求查询。Java构建器请求和Postman请求具有相同的查询。

{
   "size": 0,
   "query": {
      "bool": {
         "must": [
            {
               "term": {
                  "domain": {
                     "value": "test.com",
                     "boost": 1.0
                  }
               }
            }
         ],
         "filter": [
            {
               "range": {
                  "timeArchived": {
                     "from": "2020-09-18 04:00:00",
                     "to": "2020-09-18 10:09:46",
                     "include_lower": true,
                     "include_upper": true,
                     "time_zone": "Asia/Seoul",
                     "boost": 1.0
                  }
               }
            }
         ],
         "adjust_pure_negative": true,
         "boost": 1.0
      }
   },
   "track_total_hits": 2147483647,
   "aggregations": {
      "mailDate": {
         "date_histogram": {
            "field": "timeArchived",
            "time_zone": "ROK",
            "fixed_interval": "1h",
            "offset": 21600000,
            "order": {
               "_key": "asc"
            },
            "keyed": false,
            "min_doc_count": 0
         }
      }
   }
}

但是请求响应不相同。Java请求的timeZone选项不起作用。
这是Postman请求结果,timeZone选项起作用。

{
    "took": 484,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 177,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "mailDate": {
            "buckets": [
                {
                    "key_as_string": "2020-09-18 04:00:00",
                    "key": 1600369200000,
                    "doc_count": 8
                },
                {
                    "key_as_string": "2020-09-18 05:00:00",
                    "key": 1600372800000,
                    "doc_count": 16
                },
                {
                    "key_as_string": "2020-09-18 06:00:00",
                    "key": 1600376400000,
                    "doc_count": 4
                },
                {
                    "key_as_string": "2020-09-18 07:00:00",
                    "key": 1600380000000,
                    "doc_count": 10
                },
                {
                    "key_as_string": "2020-09-18 08:00:00",
                    "key": 1600383600000,
                    "doc_count": 17
                },
                {
                    "key_as_string": "2020-09-18 09:00:00",
                    "key": 1600387200000,
                    "doc_count": 98
                },
                {
                    "key_as_string": "2020-09-18 10:00:00",
                    "key": 1600390800000,
                    "doc_count": 24
                }
            ]
        }
    }
}

但是Java请求的timeZone选项不起作用。响应是UTC时间。

{
    "took": 918,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 223,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "date_histogram#mailDate": {
            "buckets": [
                {
                    "key_as_string": "2020-09-17T19:00:00.000Z",
                    "key": 1600369200000,
                    "doc_count": 8
                },
                {
                    "key_as_string": "2020-09-17T20:00:00.000Z",
                    "key": 1600372800000,
                    "doc_count": 16
                },
                {
                    "key_as_string": "2020-09-17T21:00:00.000Z",
                    "key": 1600376400000,
                    "doc_count": 4
                },
                {
                    "key_as_string": "2020-09-17T22:00:00.000Z",
                    "key": 1600380000000,
                    "doc_count": 10
                },
                {
                    "key_as_string": "2020-09-17T23:00:00.000Z",
                    "key": 1600383600000,
                    "doc_count": 17
                },
                {
                    "key_as_string": "2020-09-18T00:00:00.000Z",
                    "key": 1600387200000,
                    "doc_count": 98
                },
                {
                    "key_as_string": "2020-09-18T01:00:00.000Z",
                    "key": 1600390800000,
                    "doc_count": 70
                }
            ]
        }
    }
}

如何在Java中设置时区?


<details>
<summary>英文:</summary>

The query created by Postman and the request created by QueryBuilder are the same, but the TimeZone field is not applied to java requests.

here is request target mapping info

"timeArchived": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||epoch_millis"
}


here is java request source

SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder query = QueryBuilders.boolQuery();
sourceBuilder.trackTotalHits(true);
sourceBuilder.size(0);
sourceBuilder.query(query);
RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("timeArchived").timeZone("Asia/Seoul").gte(sTime).lte(eTime);
query.filter(rangeQuery);
query.must(QueryBuilders.termQuery("domain", domain));

	sourceBuilder.aggregation(AggregationBuilders.dateHistogram(&quot;mailDate&quot;).field(&quot;timeArchived&quot;).fixedInterval(DateHistogramInterval.hours(1)).timeZone(ZoneId.of(&quot;ROK&quot;)));
	
	SearchRequest request = new SearchRequest();
	request.source(sourceBuilder);
	SearchResponse response = null;
	try {
		response = client.search(request, RequestOptions.DEFAULT);
	} catch (IOException e) {
		e.printStackTrace();
	}


here is created request query. java builder request and postman request same query.

{
"size":0,
"query":{
"bool":{
"must":[
{
"term":{
"domain":{
"value":"test.com",
"boost":1.0
}
}
}
],
"filter":[
{
"range":{
"timeArchived":{
"from":"2020-09-18 04:00:00",
"to":"2020-09-18 10:09:46",
"include_lower":true,
"include_upper":true,
"time_zone":"Asia/Seoul",
"boost":1.0
}
}
}
],
"adjust_pure_negative":true,
"boost":1.0
}
},
"track_total_hits":2147483647,
"aggregations":{
"mailDate":{
"date_histogram":{
"field":"timeArchived",
"time_zone":"ROK",
"fixed_interval":"1h",
"offset":21600000,
"order":{
"_key":"asc"
},
"keyed":false,
"min_doc_count":0
}
}
}
}


but request response not same. java request timeZone option not working.

this postman request result timeZone option working.

{
"took": 484,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 177,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"mailDate": {
"buckets": [
{
"key_as_string": "2020-09-18 04:00:00",
"key": 1600369200000,
"doc_count": 8
},
{
"key_as_string": "2020-09-18 05:00:00",
"key": 1600372800000,
"doc_count": 16
},
{
"key_as_string": "2020-09-18 06:00:00",
"key": 1600376400000,
"doc_count": 4
},
{
"key_as_string": "2020-09-18 07:00:00",
"key": 1600380000000,
"doc_count": 10
},
{
"key_as_string": "2020-09-18 08:00:00",
"key": 1600383600000,
"doc_count": 17
},
{
"key_as_string": "2020-09-18 09:00:00",
"key": 1600387200000,
"doc_count": 98
},
{
"key_as_string": "2020-09-18 10:00:00",
"key": 1600390800000,
"doc_count": 24
}
]
}
}
}

but java request timeZone option not working. response is UTC time.

{
"took":918,
"timed_out":false,
"_shards":{
"total":3,
"successful":3,
"skipped":0,
"failed":0
},
"hits":{
"total":{
"value":223,
"relation":"eq"
},
"max_score":null,
"hits":[

  ]

},
"aggregations":{
"date_histogram#mailDate":{
"buckets":[
{
"key_as_string":"2020-09-17T19:00:00.000Z",
"key":1600369200000,
"doc_count":8
},
{
"key_as_string":"2020-09-17T20:00:00.000Z",
"key":1600372800000,
"doc_count":16
},
{
"key_as_string":"2020-09-17T21:00:00.000Z",
"key":1600376400000,
"doc_count":4
},
{
"key_as_string":"2020-09-17T22:00:00.000Z",
"key":1600380000000,
"doc_count":10
},
{
"key_as_string":"2020-09-17T23:00:00.000Z",
"key":1600383600000,
"doc_count":17
},
{
"key_as_string":"2020-09-18T00:00:00.000Z",
"key":1600387200000,
"doc_count":98
},
{
"key_as_string":"2020-09-18T01:00:00.000Z",
"key":1600390800000,
"doc_count":70
}
]
}
}
}


How do I set timezone in java?

</details>


# 答案1
**得分**: 1

查询看起来不错,我从我的端测试过了。可能的原因是在第 `request.source(mailSourceBuilder);` 行传递了错误的 SearchSourceBuilder。我尝试了以下代码:

**代码:**
```java
SearchRequest searchRequest = new SearchRequest("my_index");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.aggregation(AggregationBuilders.dateHistogram("histogramtest").field("createdOn")
    .fixedInterval(DateHistogramInterval.hours(1)).timeZone(ZoneId.of("ROK")));
searchRequest.source(searchSourceBuilder);

响应:

{"buckets":[{"key_as_string":"2020-05-04T16:00:00.000+09:00","key":1588575600000,"doc_count":3},{"key_as_string":"2020-05-04T17:00:00.000+09:00","key":1588579200000,"doc_count":0},{"key_as_string":"2020-05-04T18:00:00.000+09:00","key":1588582800000,"doc_count":0},{"key_as_string":"2020-05-04T19:00:00.000+09:00","key":1588586400000,"doc_count":3},{"key_as_string":"2020-05-04T20:00.00.000+09:00","key":1588590000000,"doc_count":0},{"key_as_string":"2020-05-04T21:00:00.000+09:00","key":1588593600000,"doc_count":3},
英文:

The query looks good and I tested this out from my end. The possible reason is the incorrect SearchSourceBuilder being passed at line request.source(mailSourceBuilder);. I tried the following:

Code:

SearchRequest searchRequest = new SearchRequest(&quot;my_index&quot;);
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		searchSourceBuilder.aggregation(AggregationBuilders.dateHistogram(&quot;histogramtest&quot;).field(&quot;createdOn&quot;)
				.fixedInterval(DateHistogramInterval.hours(1)).timeZone(ZoneId.of(&quot;ROK&quot;)));
		searchRequest.source(searchSourceBuilder);

Response:

{&quot;buckets&quot;:[{&quot;key_as_string&quot;:&quot;2020-05-04T16:00:00.000+09:00&quot;,&quot;key&quot;:1588575600000,&quot;doc_count&quot;:3},{&quot;key_as_string&quot;:&quot;2020-05-04T17:00:00.000+09:00&quot;,&quot;key&quot;:1588579200000,&quot;doc_count&quot;:0},{&quot;key_as_string&quot;:&quot;2020-05-04T18:00:00.000+09:00&quot;,&quot;key&quot;:1588582800000,&quot;doc_count&quot;:0},{&quot;key_as_string&quot;:&quot;2020-05-04T19:00:00.000+09:00&quot;,&quot;key&quot;:1588586400000,&quot;doc_count&quot;:3},{&quot;key_as_string&quot;:&quot;2020-05-04T20:00:00.000+09:00&quot;,&quot;key&quot;:1588590000000,&quot;doc_count&quot;:0},{&quot;key_as_string&quot;:&quot;2020-05-04T21:00:00.000+09:00&quot;,&quot;key&quot;:1588593600000,&quot;doc_count&quot;:3},

huangapple
  • 本文由 发表于 2020年9月18日 09:54:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63948291.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定