How to filter by date range elastic search query aggregate?

Viewed 13

I'm new to Elastic search and trying to adapt my query to build aggregate buckets based on a different date range.

I have two objects that specify the date range prevRange & range, both of them have only two properties {to, from}

My goal is to try filter different aggerations by the date range (e.g. incoming -> rangeStatuses & incoming -> prevRangeStatuses & belongsTo), but I'm not sure how to incorporate the filtering logic

I was trying to use hard_bounds instead of extended_bounds, but it gives me the wrong results. Filters aggregation doesn't seem to have any options to pass date ranges

 {
            query: {
              bool: {
                filter: [
                  {
                    range: {
                      createdAt: {
                        time_zone: timezone,
                        gte: prevRange.from,
                        lte: range.to,
                      },
                    },
                  },
                ],
              },
            },
            aggs: {
              incoming: {
                terms: { field: 'status' },
                aggs: {
                  rangeStatuses: {
                    terms: { field: 'status' },
                    aggs: {
                      histogram: {
                        date_histogram: {
                          field: 'createdAt',
                          calendar_interval: granularity,
                          min_doc_count: 0,
                          time_zone: timezone,
                          extended_bounds: {
                            min: range.from.getTime(),
                            max: range.to.getTime(),
                          },
                        },
                      },
                    },
                  },
                  prevRangeStatuses: {
                    terms: { field: 'status' },
                    aggs: {
                      histogram: {
                        date_histogram: {
                          field: 'createdAt',
                          calendar_interval: granularity,
                          min_doc_count: 0,
                          time_zone: timezone,
                          extended_bounds: {
                            min: prevRange.from.getTime(),
                            max: prevRange.to.getTime(),
                          },
                        },
                      },
                    },
                  },
                },
              },
              belongsTo: {
                filter: {
                  exists: {
                    field: 'type',
                  },
                },
                aggs: {
                  activities: {
                    terms: {
                      field: 'type',
                    },
                    aggs: {
                      direction: {
                        terms: {
                          field: 'direction',
                        },
                        aggs: {
                          histogram: {
                            date_histogram: {
                              field: 'createdAt',
                              time_zone: timezone,
                              calendar_interval: granularity,
                              min_doc_count: 0,
                              extended_bounds: {
                                min: range.from.getTime(),
                                max: range.to.getTime(),
                              },
                            },
                          },
                        },
                      },
                      duration: {
                        date_histogram: {
                          field: 'createdAt',
                          time_zone: timezone,
                          calendar_interval: granularity,
                          min_doc_count: 0,
                          extended_bounds: {
                            min: range.from.getTime(),
                            max: range.to.getTime(),
                          },
                        },
                        aggs: {
                          duration: {
                            sum: {
                              field: 'duration',
                            },
                          },
                        },
                      },
                    },
                  },
                },
              },
            },
          },
0 Answers
Related