# Frequency capped sum

**URL:** <https://discuss.elastic.co/t/frequency-capped-sum/41536>\
**Category:** Elasticsearch\
**Created:** [February 11, 2016, 6:08pm UTC](https://discuss.elastic.co/t/frequency-capped-sum/41536 "2016-02-11T18:08:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jean.logeart](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@jean.logeart](https://discuss.elastic.co/u/jean.logeart)\
**Post date:** [February 11, 2016, 6:08pm UTC](https://discuss.elastic.co/t/frequency-capped-sum/41536/1 "2016-02-11T18:08:12Z")

</div>

I have indexed documents of the form:

```
{
  "device_id": "abc",
  "views": 123,
  + other criteria
}

```

I can compute:

- The count of unique devices using a [`cardinality aggregation`](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html) on `device_id`
- The overall number of views using a [`sum aggregation`](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-sum-aggregation.html) on `views`

**How can I compute the sum of the views such that a given device can only account for a maximum of `n` views?**

For example, if my docs are:

```
{"device_id": "a", "views": 3, ...}
{"device_id": "a", "views": 4, ...}
{"device_id": "a", "views": 1, ...}
{"device_id": "b", "views": 2, ...}
{"device_id": "c", "views": 6, ...}

```

And my `n` is `5`, then the result should be `12` = (5 for `a` even though its total is 8 + 2 for `b` + 5 for `c`)

My indices contain ~500,000 distinct devices.

The result does not need to be exact and can be approximate within _reasonable bounds_.

I do not mind using my own script using a combinations of techniques (HLL, Count-Min Sketch, Bloomfilters, Min Hash, ...)

---

<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:17pm UTC](https://discuss.elastic.co/t/frequency-capped-sum/41536/2 "2017-07-05T23:17:02Z")

</div>

Closed on July 5, 2017, 11:17pm UTC


