简体   繁体   中英

Elasticsearch Date Histogram buckets starts from wrong date

I have ElasticSearch analytic query. It must aggregate sum by 7d buckets. Also I've use extended bounds. But result buckets starts from wrong date. Elastic 5.2.2 version. What I'm doing wrong?

{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "doc.dateExecute": {
              "from": "2017-07-11",
              "to": "2017-07-31",
              "include_lower": true,
              "include_upper": true,
              "boost": 1.0
            }
          }
        }
      ],
      "disable_coord": false,
      "adjust_pure_negative": true,
      "boost": 1.0,
      "_name": "onDates"
    }
  },
  "aggregations": {
    "time": {
      "date_histogram": {
        "field": "doc.dateExecute",
        "interval": "7d",
        "offset": 0,
        "order": {
          "_key": "asc"
        },
        "keyed": false,
        "min_doc_count": 0,
        "extended_bounds": {
          "min": "2017-07-11",
          "max": "2017-07-31"
        }
      },
      "aggregations": {
        "incomingAmount": {
          "sum": {
            "field": "doc.incomingAmount"
          }
        },
        "outgoingAmount": {
          "sum": {
            "field": "doc.outgoingAmount"
          }
        }
      }
    }
  }
}

But buckets starts from "2017-07-06T00:00:00.000Z" and goes by 7d intervals to "2017-07-27T00:00:00.000Z". What I'm missing? Thanks!

Update 1

I'd try a many variants of query. Different timezones, ranges, disabling exteneded_bounds, intervals multiple 7 day. It's works good only for 1-3d intervals.

Ok, after some conversation on elastic forum and register a bug:

1) Working with DateHistogram and ExtendedBounds is tricky in ES (5.2 at least) DateHistogram starts buckets from UnixEpoch 1970-01-01 so if you use days intervals (like 7d or 3d etc) but need to start bucket from the left bound - you need to set offset for on date of left border (in ms). 2) But we still have some bug on 5.2.2 version. With 7d intervals we have explicit first bucket on Thu (01-01-1970 is Thu). All other buckets with proper offset.

Lack of documentation and a lot of strange behavior :(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM