# Need help with ElasticSearch Pipeline Aggregation query

**URL:** https://discuss.elastic.co/t/need-help-with-elasticsearch-pipeline-aggregation-query/74954
**Category:** Elasticsearch
**Created:** [February 14, 2017, 4:10am UTC](https://discuss.elastic.co/t/need-help-with-elasticsearch-pipeline-aggregation-query/74954 "2017-02-14T04:10:29Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Sean\_Janis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sean_janis/32/15478_2.png) [@Sean\_Janis](https://discuss.elastic.co/u/Sean_Janis)
#### Post date: [February 14, 2017, 4:10am UTC](https://discuss.elastic.co/t/need-help-with-elasticsearch-pipeline-aggregation-query/74954/1 "2017-02-14T04:10:29Z")

</div>

Hi,

I'm looking to run a SQL-like group by query with ElasticSearch. I have an index where each hit maps to an action with a user id. I would like to create a histogram grouped by the # of times a user completed a specific action. I currently query where actions where type = "foobar" and group by user\_id. This produces the example output below, which indicates a bucket keyed on user id with a doc\_count equal to the amount of actions that match for that user.

What I'm missing is then taking the key & doc\_count result and bucketing into a histogram grouped by the count. For example, with the example output, the histogram bucket would be: [60 =\> 1, 63 =\> 2, 105 =\> 1, 133 =\> 1]. This indicates there are 2 users who triggered a doc\_count of 63, a.k.a. have triggered that action 63 times. I'm missing the second part of this grouping query (maybe a pipeline aggregation?) to group by user action count.

Can someone help me understand where this is possible and whether it can then be used in a Kibana visualization? Thanks! Sean

* * *

**Example Input**

```javascript

POST events-*/_search?size=0
{
   "query": {
		"bool": {
			"must": [{
				"query_string": {
					"analyze_wildcard": true,
					"query": "action=foobar"
				}
			}, {
				"range": {
					"@timestamp": {
						"gte": 1486332269,
						"lte": 1486937069,
						"format": "epoch_second"
					}
				}
			}],
			"must_not": []
		}
	},
   "aggs": {
      "groupById": {
         "terms": {
            "field": "user_id.keyword",
            "size": 5
         }
      }
   }
}

```

**Example Response:**

```javascript
{
  "took": 3711,
  "timed_out": false,
  "_shards": {
    "total": 20,
    "successful": 20,
    "failed": 0
  },
  "hits": {
    "total": 14271080,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "groupById": {
      "doc_count_error_upper_bound": 193,
      "sum_other_doc_count": 14270656,
      "buckets": [
        {
          "key": "user_1",
          "doc_count": 133
        },
        {
          "key": "user_2",
          "doc_count": 105
        },
        {
          "key": "user_3",
          "doc_count": 63
        },
        {
          "key": "user_4",
          "doc_count": 63
        },
        {
          "key": "user_5",
          "doc_count": 60
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [March 14, 2017, 4:10am UTC](https://discuss.elastic.co/t/need-help-with-elasticsearch-pipeline-aggregation-query/74954/2 "2017-03-14T04:10:51Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
