Hat hout
Hat hout

Reputation: 491

How to adjust date range aggregation values on date field?

Iam trying to group docs using a date field in each docs. I need to groub them by docs from last 30days, from 30-60 days, from 60-90. by applying the following range aggs. I got a totaly wrong date ranges.

{
  "aggs": {
    "dateRanges": {
      "date_range": {
        "field": "date",
        "keyed": true,
        "format": "yyyy-mm-dd", 
        "ranges": [
            {"from": "now/d",
              "to": "now/d-1M",
              "key": "Last 30 Days"
            },
            {"from": "now/d-1M",
              "to": "now/d-2M",
              "key": "Last 60 Days"
            },
            {"from": "now/d-2M",
              "to": "now/d-3M",
              "key": "Last 90 Days"
            }
          ]

      }
    }
  }
}

Current results:

  "aggregations" : {
    "dateRanges" : {
      "buckets" : {
        "Last 90 Days" : {
          "from" : 1.585008E12,
          "from_as_string" : "2020-00-24",
          "to" : 1.5825024E12,
          "to_as_string" : "2020-00-24",
          "doc_count" : 0
        },
        "Last 60 Days" : {
          "from" : 1.5876864E12,
          "from_as_string" : "2020-00-24",
          "to" : 1.585008E12,
          "to_as_string" : "2020-00-24",
          "doc_count" : 0
        },
        "Last 30 Days" : {
          "from" : 1.5902784E12,
          "from_as_string" : "2020-00-24",
          "to" : 1.5876864E12,
          "to_as_string" : "2020-00-24",
          "doc_count" : 0
        }
      }
    }
  }
}

why does each bucket has the same 'from' and 'to' values?

Upvotes: 0

Views: 56

Answers (1)

Val
Val

Reputation: 217274

All seems correct except that your date format is wrong, you need to make the following change

    "format": "yyyy-MM-dd", 
                    ^^
                    ||
          month instead of minutes

Upvotes: 2

Related Questions