# \[Aggregations\] Average for document count per term field

**URL:** https://discuss.elastic.co/t/aggregations-average-for-document-count-per-term-field/28509
**Category:** Elasticsearch
**Created:** [September 2, 2015, 10:04am UTC](https://discuss.elastic.co/t/aggregations-average-for-document-count-per-term-field/28509 "2015-09-02T10:04:06Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![eliasah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eliasah/32/34741_2.png) [@eliasah](https://discuss.elastic.co/u/eliasah)
#### Post date: [September 2, 2015, 10:04am UTC](https://discuss.elastic.co/t/aggregations-average-for-document-count-per-term-field/28509/1 "2015-09-02T10:04:07Z")

</div>

It might be a silly question, so excuse me.

I have the following index _events_ with a type _click_ and the simplified following mapping:

```
 {
    "events": {
       "mappings": {
          "click": {
             "properties": {
                "evenType": {
                   "type": "string"
                },
                "eventDate": {
                   "type": "date",
                   "format": "dateOptionalTime"
                },
                "userId": {
                   "type": "long"
                }
             }
          }
       }
    }
 }

```

I'm trying to compute the number of clicks for the top user compared to number of of unique uses.

I am kind of stuck with the following which returns the number of clicks for the top user :

```
 curl -XGET "http://localhost:9200/events/click/_search" -d'
 {
   "size": 0,
   "aggs": {
     "users with most clicks": {
       "terms": {
         "field": "userId",
         "size": 10
       }
     }
   }
 }'

```

and computing the number of unique users :

```
 curl -XGET "http://localhost:9200/events/click/_search" -d'
 {
   "size": 0,
   "aggs": {
     "uniques users": {
       "cardinality": {
         "field": "userId"
       }
     }
   }
 }' 

```

Is it possible to combine those two aggregations so it can returns the average number of clicks per user?

Thanks in advance.

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [September 2, 2015, 10:45am UTC](https://discuss.elastic.co/t/aggregations-average-for-document-count-per-term-field/28509/2 "2015-09-02T10:45:24Z")

</div>

I'm not 100% sure what you are after but if you want `average number of clicks per user` then that would be the total number of clicks recorded divided by the number of unique users. Both of these numbers are returned in the results of your last example query in the JSON fields `hits/total` and `aggregations/uniques user/value`

Cheers  
Mark

---

<div class="post-metadata">

### Author: ![eliasah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eliasah/32/34741_2.png) [@eliasah](https://discuss.elastic.co/u/eliasah)
#### Post date: [September 2, 2015, 11:38am UTC](https://discuss.elastic.co/t/aggregations-average-for-document-count-per-term-field/28509/3 "2015-09-02T11:38:34Z")

</div>

I know how to process them outside of elasticsearch, if I send both queries I have mentioned above.

What I'm trying to do is the following :

Let's consider that a user I made a number of clicks Y\_i what means that we have Y\_i observations on the userId = I.  
What I'm trying to compute is the density of the user X clicks Y upon all the observations.

- I compute Y\_i with the terms aggregations for a specific user I, described above.
- U is the number of unique users computed with the second cardinality aggregation.
- I am looking want to compute Y\_i / U for the top 10 users.

I don't know if that is clear enough.

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [September 2, 2015, 12:09pm UTC](https://discuss.elastic.co/t/aggregations-average-for-document-count-per-term-field/28509/4 "2015-09-02T12:09:41Z")

</div>

Less clear I'm afraid.

> U is the number of unique users  
> Y\_i / U for the top 10 users.

`U` here is a constant. So you want to scale all of the doc\_counts reported for each of the top 10 users by this constant?  
I don't get why this would be useful. It's like finding the top 10 vehicles with the most miles on the clock and then dividing these numbers by the total number of car manufacturers. It does not look to serve any purpose.

Can we start with stating the business problem you are trying to solve rather than _how_ you intend to solve it?

---

<div class="post-metadata">

### Author: ![eliasah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eliasah/32/34741_2.png) [@eliasah](https://discuss.elastic.co/u/eliasah)
#### Post date: [September 2, 2015, 12:21pm UTC](https://discuss.elastic.co/t/aggregations-average-for-document-count-per-term-field/28509/5 "2015-09-02T12:21:34Z")

</div>

`U` can move over time, that's why I don't want to consider it as a constant.

I'm not interested in the business value as much as the way to do this operation.

We can simplify the problem to the following :

```
"buckets": [
        {
           "key": 2096544,
           "doc_count": 48168
        },
        {
           "key": 68753,
           "doc_count": 33208
        },
        {
           "key": 34,
           "doc_count": 31665
        },
        [...]
}

```

As a result of the first terms aggregation.

I want to compute the `doc_count / number of hits` where the keys are the userId.

How can I perform this operation?

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [September 2, 2015, 1:12pm UTC](https://discuss.elastic.co/t/aggregations-average-for-document-count-per-term-field/28509/6 "2015-09-02T13:12:49Z")

</div>

> I'm not interested in the business value  
> I want to compute the doc\_count / number of hits where the keys are the userId.

This changes the previous definition: ` Y_i / U` is not the same as `doc_count / number of hits`

> U can move over time, that's why I don't want to consider it as a constant.

I meant it can be considered as a constant for the purposes of your single request. It's equivalent to saying "I want to multiply all reported doc\_counts by 0.234235" - it is an arbitrary fixed boost applied to rebase all doc\_count values and does nothing to change the ranking order used to select the top 10 users.  
We do see real-world examples of using elasticsearch on click data and there are powerful analysis techniques available but unfortunately they do not extend to your example of re-basing doc\_count numbers for display purposes.

---

<div class="post-metadata">

### Author: ![eliasah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eliasah/32/34741_2.png) [@eliasah](https://discuss.elastic.co/u/eliasah)
#### Post date: [September 2, 2015, 1:15pm UTC](https://discuss.elastic.co/t/aggregations-average-for-document-count-per-term-field/28509/7 "2015-09-02T13:15:53Z")

</div>

Ok. Thank you for trying to help!

---

<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, 11:52pm UTC](https://discuss.elastic.co/t/aggregations-average-for-document-count-per-term-field/28509/8 "2017-07-05T23:52:36Z")

</div>


