# Elasticsearch: Aggregations request timezone field not working

**URL:** <https://discuss.elastic.co/t/elasticsearch-aggregations-request-timezone-field-not-working/249061>\
**Category:** Elasticsearch\
**Created:** [September 18, 2020, 5:46am UTC](https://discuss.elastic.co/t/elasticsearch-aggregations-request-timezone-field-not-working/249061 "2020-09-18T05:46:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![yunut](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yunut/32/75850_2.png) [@yunut](https://discuss.elastic.co/u/yunut)\
**Post date:** [September 18, 2020, 5:46am UTC](https://discuss.elastic.co/t/elasticsearch-aggregations-request-timezone-field-not-working/249061/1 "2020-09-18T05:46:20Z")

</div>

Elasticsearch Version: 7.7.0v  
Rest client Vesion: 7.7.0v

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

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

```

here is java request source

```auto
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();
        }

```

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

```auto
{
   "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 worked well.

```auto
{
    "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.

```auto
{
   "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?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 16, 2020, 5:46am UTC](https://discuss.elastic.co/t/elasticsearch-aggregations-request-timezone-field-not-working/249061/2 "2020-10-16T05:46:28Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
