B
B
bio2017-02-04 09:46:12
elasticsearch
bio, 2017-02-04 09:46:12

How to write an aggregation request correctly?

Hello.
The following documents are in the elastic:

{
    {
        "status_id" : 1,
        "client_id" : 2,
        "task_id"   : 3
    },
    {
        "status_id" : 2,
        "client_id" : 2,
        "task_id"   : 3
    },
    {
        "status_id" : 3,
        "client_id" : 2,
        "task_id"   : 3
    }
}

You need to calculate status_idwith the passed client_idand task_id.
Example in sql:
SELECT status_id, COUNT(*) AS count FROM table WHERE client_id = 2 AND task_id = 3 GROUP BY status_id;

Can you please tell me how to make such a request?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Polushkin, 2017-02-09
@bioforge

Try to generate requests here: www.nlpcn.org:9999/web
You can greatly simplify and optimize, but in general, something like this:

{
  "from": 0,
  "size": 0,
  "query": {
    "bool": {
      "must": {
        "bool": {
          "must": [
            {
              "match": {
                "client_id": {
                  "query": 2,
                  "type": "phrase"
                }
              }
            },
            {
              "match": {
                "task_id": {
                  "query": 3,
                  "type": "phrase"
                }
              }
            }
          ]
        }
      }
    }
  },
  "_source": {
    "includes": [
      "status_id",
      "COUNT"
    ],
    "excludes": []
  },
  "fields": "status_id",
  "aggregations": {
    "status_id;": {
      "terms": {
        "field": "status_id;",
        "size": 200
      },
      "aggregations": {
        "count": {
          "value_count": {
            "field": "_index"
          }
        }
      }
    }
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question