How can I perform n-dimensional statistics aggregation in one Query DSL in Elastic?

Viewed 18

I just learned Elastic, and I want to write some code that analyses the user requests API in different time dimensions.

  • How many times does the user request in one day?
  • How many times does the user request in one week?
  • How many times does the user request in one month?
  • How many times does the user request in one year?

The code has shown below:

POST my-index/_count
{
  "query": {
    "bool": {
      "filter": [
        {
          "terms": {
            "user_id.keyword": ["USER_ID"]
          }
        },
        {
          "wildcard": {
            "uri.keyword": "/api-path*"
          }
        },
        {
          "range": {
            "@timestamp": {
              "gte": "now-1y/y",
              "lte": "now/d"
            }
          }
        }
      ]
    }
  }
}

But I should send the code to elastic four times in different @timestamp parameter(now-1d/d, now-1w/w, now-1m/m, now-1y/y). That will cost a lot of networks and computing resources.

Have any method that puts them in one request?

1 Answers

Tldr;

It is possible, with the multi-search api of Elasticsearch

It looks like that:

GET my-index-000001/_msearch
{ }
{"query" : {"match" : { "message": "this is a test"}}}
{"index": "my-index-000002"}
{"query" : {"match_all" : {}}}

The client library of Elasticsearch do implement this feature.

Related