# ElasticSearch distinct results by multiple fields with pagination

**URL:** <https://discuss.elastic.co/t/elasticsearch-distinct-results-by-multiple-fields-with-pagination/277455>\
**Category:** Elasticsearch\
**Created:** [June 30, 2021, 12:23pm UTC](https://discuss.elastic.co/t/elasticsearch-distinct-results-by-multiple-fields-with-pagination/277455 "2021-06-30T12:23:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![adolbik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/adolbik/32/78248_2.png) [@adolbik](https://discuss.elastic.co/u/adolbik)\
**Post date:** [June 30, 2021, 12:23pm UTC](https://discuss.elastic.co/t/elasticsearch-distinct-results-by-multiple-fields-with-pagination/277455/1 "2021-06-30T12:23:24Z")

</div>

I have index with bunch of events (user viewed page, clicked something and etc.). I have a page in the system where I need to display uniq results (only one row per user + event + itemId). It means if we have 7 rows like these

```auto
 {'userId': 1, 'event': 'view', 'itemId': "11"}
 {'userId': 1, 'event': 'click', 'itemId': "11"}
 {'userId': 1, 'event': 'view', 'itemId': "11"} // duplicate
 {'userId': 2, 'event': 'view', 'itemId': "12"}
 {'userId': 2, 'event': 'view', 'itemId': "11"}
 {'userId': 3, 'event': 'click', 'itemId': "13"}
 {'userId': 2, 'event': 'view', 'itemId': "12"} // duplicate

```

I want to get from Elastic Search only uniq rows where combination of `userId + event + itemId` is uniq

```auto
 {'userId': 1, 'event': 'view', 'itemId': "11"}
 {'userId': 1, 'event': 'click', 'itemId': "11"}
 {'userId': 2, 'event': 'view', 'itemId': "12"}
 {'userId': 2, 'event': 'view', 'itemId': "11"}
 {'userId': 3, 'event': 'click', 'itemId': "13"}

```

Also I need to support pagination for results.

I tried to use buckets aggregation, collapse but these options ether doesn't support pagination or multiple fields aggregation.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [July 1, 2021, 2:24pm UTC](https://discuss.elastic.co/t/elasticsearch-distinct-results-by-multiple-fields-with-pagination/277455/2 "2021-07-01T14:24:02Z")

</div>

Hey,

while you could use [field collapsing](https://www.elastic.co/guide/en/elasticsearch/reference/7.13/collapse-search-results.html) for this, you may want to do this in a more clean way and take a look at [data transforms](https://www.elastic.co/guide/en/elasticsearch/reference/7.13/transforms.html) allowing you to create a continouos process running in the background that collects and groups the latest data resulting in an easy to query index.

Hope this helps!

--Alex

---

<div class="post-metadata">

**Author:** ![adolbik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/adolbik/32/78248_2.png) [@adolbik](https://discuss.elastic.co/u/adolbik)\
**Post date:** [July 3, 2021, 7:52am UTC](https://discuss.elastic.co/t/elasticsearch-distinct-results-by-multiple-fields-with-pagination/277455/3 "2021-07-03T07:52:08Z")

</div>

Hey Alex,

thanks for response! I will check this options.

---

<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 31, 2021, 7:52am UTC](https://discuss.elastic.co/t/elasticsearch-distinct-results-by-multiple-fields-with-pagination/277455/4 "2021-07-31T07:52:37Z")

</div>

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