# How to do pipeline range aggregation on terms aggregation?

**URL:** <https://discuss.elastic.co/t/how-to-do-pipeline-range-aggregation-on-terms-aggregation/58265>\
**Category:** Elasticsearch\
**Created:** [August 17, 2016, 2:27pm UTC](https://discuss.elastic.co/t/how-to-do-pipeline-range-aggregation-on-terms-aggregation/58265 "2016-08-17T14:27:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![phoenixgao](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/phoenixgao/32/11429_2.png) [@phoenixgao](https://discuss.elastic.co/u/phoenixgao)\
**Post date:** [August 17, 2016, 2:27pm UTC](https://discuss.elastic.co/t/how-to-do-pipeline-range-aggregation-on-terms-aggregation/58265/1 "2016-08-17T14:27:55Z")

</div>

For example,

I have "events" indexed into elasticsearch, like,

```auto
    {
        "event": "visit",
        "user_id": 123,
        "timestamp": "2016-08-16T00:02:03"
    }

```

With bucket terms aggregation on field `user_id` I can get the how many times a user visits during a specific time period

```auto
    "aggregations" : {
        "user_visits" : {
            "doc_count_error_upper_bound": 0, 
            "sum_other_doc_count": 0, 
            "buckets" : [ 
                {
                    "key" : 1,
                    "doc_count" : 13
                },
                {
                    "key" : 2,
                    "doc_count" : 22
                },
                {
                    "key" : 3,
                    "doc_count" : 26
                },
                {
                    "key" : 4,
                    "doc_count" : 35
                },
            ]
        }
    }

```

while the key is user id, but the data I actually want is a range aggregation on how many users have visited between x to y times and how many between y to z:

```auto
// This is the expected results:
    "aggregations": {
        "user_visit_ranges" : {
            "buckets": [
                {
                    "to": 10,
                    "doc_count": 0
                },
                {
                    "from": 10
                    "to": 20,
                    "doc_count": 1
                },
                {
                    "from": 20,
                    "to": 30,
                    "doc_count": 2
                },
                {
                    "from": 30,
                    "doc_count": 1
                }
            ]
        }
    }

```

Is it possible to do this kind of aggregation in elasticsearch (without client scripting) and how?

Thanks

---

<div class="post-metadata">

**Author:** ![Gustavo\_Orsi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gustavo_orsi/32/33161_2.png) [@Gustavo\_Orsi](https://discuss.elastic.co/u/Gustavo_Orsi)\
**Post date:** [September 23, 2016, 3:29pm UTC](https://discuss.elastic.co/t/how-to-do-pipeline-range-aggregation-on-terms-aggregation/58265/2 "2016-09-23T15:29:30Z")

</div>

why do you need pipeline for this ? can't just use "date\_range" with a nested "terms" or "cardinality" ?

```
{
  "size": 0,
  "aggs": {
    "filter_event_type": {
      "filter": {
        "term": {
          "event": "visit"
        }
      },
      "aggs": {
        "user_visit_ranges": {
          "date_range": {
            "field": "timestamp",
            "ranges": [
              {
                "from": "2016-03-17T00:02:03",
                "to": "2016-08-21T00:02:03",
                "key": "first-period"
              },
              {
                "from": "2016-08-22T00:02:03",
                "to": "2016-12-17T00:02:03",
                "key": "second-period"
              }
            ]
          },
          "aggs": {
            "user_visits": {
              "terms": {
                "field": "user_id"
              }
            }
          }
        }
      }
    }
  }
}
```

---

<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:** [July 5, 2017, 10:16pm UTC](https://discuss.elastic.co/t/how-to-do-pipeline-range-aggregation-on-terms-aggregation/58265/4 "2017-07-05T22:16:19Z")

</div>


