简体   繁体   中英

elasticsearch date_histogram offset and extended_bounds don't work together?

I'm doing a date_histogram with week interval. i need my weeks to start on sundays rather than mondays, and i need the result to include weeks in which there are no docs (empty records).

to get that i use offset = -1d to change the start to sunday, and extended_bounds to get the empty records.

elasticsearch nicely figures out the first day of the interval, so if i supply a start date that's, say wednesday, i get a record for the week starting the previous sunday.

the problem is, if i set offset = -1d, i get an extra week. my hypothesis is that it calculates the first day of the interval without taking the offset into account.

in the example shown, i would not expect to get the 2017-09-24 record:

query:

{
  "size": 0,
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "utility.utility_uuid.orig": "17245998142979832061"
          }
        },
        {
          "range": {
            "user.date_created": {
              "gte": "2017-10-01",
              "lt": "2017-10-31"
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "eow_accounts_and_users": {
      "date_histogram": {
        "format": "yyyy-MM-dd",
        "interval": "week",
        "offset": "-1d",
        "time_zone": "US/Pacific",
        "field": "user.date_created",
        "min_doc_count": 0,
        "extended_bounds": {
          "min": "2017-10-01",
          "max": "2017-10-31"
        }
      }
    }
  }
}

result:

{
  "took": 9,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "eow_accounts_and_users": {
      "buckets": [
        {
          "key_as_string": "2017-09-24",
          "key": 1506236400000,
          "doc_count": 0
        },
        {
          "key_as_string": "2017-10-01",
          "key": 1506841200000,
          "doc_count": 0
        },
        {
          "key_as_string": "2017-10-08",
          "key": 1507446000000,
          "doc_count": 0
        },
        {
          "key_as_string": "2017-10-15",
          "key": 1508050800000,
          "doc_count": 0
        },
        {
          "key_as_string": "2017-10-22",
          "key": 1508655600000,
          "doc_count": 0
        },
        {
          "key_as_string": "2017-10-29",
          "key": 1509260400000,
          "doc_count": 0
        }
      ]
    }
  }
}

Add an extra day to the extended bounds:

"extended_bounds": {
    "min": dateParams.startTime+86400000, // an extra day
    "max": dateParams.endTime
}

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