Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElasticSearch: Nested buckets aggregation

I'm new to ElasticSearch, so this question could be quite trivial for you, but here I go:

I'm using kibana_sample_data_ecommerce, which documents have a mapping like this

{
    ...
    "order_date" : <datetime>
    "taxful_total_price" : <double>
    ...
}

I want to get a basic daily behavior of the data:

Sales a day behavior

Expecting documents like this:

[
  {
    "qtime" : "00:00",
    "mean" : 20,
    "std" : 40
  },
  {
    "qtime" : "01:00",
    "mean" : 150,
    "std" : 64
  }, 
  ...
]

So, the process I think that I need to do is:

Group by day all records -> 
  Group by time window for each day -> 
    Sum all record in each time window -> 
      Cumulative Sum for each sum by time window, thus, I get behavior of a day ->
        Extended_stats by the same time window across all days

And that can be expressed like this:

Nested bucket aggregation

But I can't unwrap those buckets to process those statistics. May you give me some advice to do that operation and get that result?

Here is my current query(kibana developer tools):

POST kibana_sample_data_ecommerce/_search
{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "order_date": {
              "gt": "now-1M",
              "lte": "now"
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "day_histo": {
      "date_histogram": {
        "field": "order_date",
        "calendar_interval": "day"
      },
      "aggs": {
        "qmin_histo": {
          "date_histogram": {
            "field": "order_date",
            "calendar_interval": "hour"
          },
          "aggs": {
            "qminute_sum": {
              "sum": {
                "field": "taxful_total_price"
              }
            },
            "cumulative_qminute_sum": {
              "cumulative_sum": {
                "buckets_path": "qminute_sum"
              }
            }
          }
        }
      }
    }
  }
}
like image 522
CamiloSalomon Avatar asked Jul 08 '20 03:07

CamiloSalomon


People also ask

What is nested aggregation?

Nested aggregationeditA special single bucket aggregation that enables aggregating nested documents. For example, lets say we have an index of products, and each product holds the list of resellers - each having its own price for the product.

What is Bucket aggregation in Elasticsearch?

Bucket aggregations don't calculate metrics over fields like the metrics aggregations do, but instead, they create buckets of documents. Each bucket is associated with a criterion (depending on the aggregation type) which determines whether or not a document in the current context "falls" into it.

Is Elasticsearch good for aggregation?

Elasticsearch Aggregations provide you with the ability to group and perform calculations and statistics (such as sums and averages) on your data by using a simple search query. An aggregation can be viewed as a working unit that builds analytical information across a set of documents.

What is nested in Elasticsearch?

The nested type is a specialised version of the object data type that allows arrays of objects to be indexed in a way that they can be queried independently of each other.


1 Answers

Here's how you pull off the extended stats:

{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "order_date": {
              "gt": "now-4M",
              "lte": "now"
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "by_day": {
      "date_histogram": {
        "field": "order_date",
        "calendar_interval": "day"
      },
      "aggs": {
        "by_hour": {
          "date_histogram": {
            "field": "order_date",
            "calendar_interval": "hour"
          },
          "aggs": {
            "by_taxful_total_price": {
              "extended_stats": {
                "field": "taxful_total_price"
              }
            }
          }
        }
      }
    }
  }
}

yielding

enter image description here

like image 129
Joe - Elasticsearch Handbook Avatar answered Oct 09 '22 15:10

Joe - Elasticsearch Handbook