# Paginate elastic results on specific field

**URL:** <https://discuss.elastic.co/t/paginate-elastic-results-on-specific-field/133724>\
**Category:** Elasticsearch\
**Created:** [May 29, 2018, 4:38pm UTC](https://discuss.elastic.co/t/paginate-elastic-results-on-specific-field/133724 "2018-05-29T16:38:03Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![daganm](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@daganm](https://discuss.elastic.co/u/daganm)\
**Post date:** [May 29, 2018, 4:38pm UTC](https://discuss.elastic.co/t/paginate-elastic-results-on-specific-field/133724/1 "2018-05-29T16:38:03Z")

</div>

Hello !  
I have data like this in my elasticsearch :

```
{
    doc_name : 'Name 1',
    member_id: 'id1'
}
{
    doc_name : 'Name 2',
    member_id: 'id1'
},
{
    doc_name : 'Name 3',
    member_id: 'id2'
}

```

I have my own scoring function which is very important so I need to retrieve data in the correct order. No problem here but I now want to limit my results by number of members, to get only the ordered results but for only the first 10 members. Then make a from clause to make a load more button.

I'm not even sure it is possible... If you have any idea on how I can make something like this, please feel free !

Thank you !

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [May 31, 2018, 12:36pm UTC](https://discuss.elastic.co/t/paginate-elastic-results-on-specific-field/133724/2 "2018-05-31T12:36:15Z")

</div>

I'm not quite sure I understand your requirements. Are you saying you want the hits sorted using your custom scoring function, but that you only want to see one hit per unique `member_id`? In that case, field collapsing is exactly what you are looking for: [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-collapse.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-collapse.html)

---

<div class="post-metadata">

**Author:** ![daganm](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@daganm](https://discuss.elastic.co/u/daganm)\
**Post date:** [May 31, 2018, 12:44pm UTC](https://discuss.elastic.co/t/paginate-elastic-results-on-specific-field/133724/3 "2018-05-31T12:44:28Z")

</div>

Thanks for your answer ! That's not exactly that.

I want to retrieve my hits ordered by my score, and stop as I reach for example 10 distinct `member_ids`. My limit would not be on the document count but on a specific field.  
More difficult : I want to add a Load More button, so that I could retrieve the next hits and so on.

I'm not quite sure I can perform something like this with the field collapsing...

Thank you

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [May 31, 2018, 1:01pm UTC](https://discuss.elastic.co/t/paginate-elastic-results-on-specific-field/133724/4 "2018-05-31T13:01:44Z")

</div>

What is it that you want to retrieve for each hit? Just the `member_id`s? Or all documents that belong to each of the top 10 `member_id`s?

What would happen with a document of a member in the top 10 that scores lower than a document of member #11? Would it be returned when you click " Load More"?

Sorry for these questions, but I'm trying to understand what it is that you're trying to do 🙂

---

<div class="post-metadata">

**Author:** ![daganm](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@daganm](https://discuss.elastic.co/u/daganm)\
**Post date:** [May 31, 2018, 1:19pm UTC](https://discuss.elastic.co/t/paginate-elastic-results-on-specific-field/133724/5 "2018-05-31T13:19:08Z")

</div>

No problem thank you for your time !

It's hard to explain haha ! I'll try to make an example. Supposing that without limit, elastic gets me as an answer the documents below :

```
{
    doc_name : 'Name 1',
    member_id: 'id1'
}
{
    doc_name : 'Name 4',
    member_id: 'id4'
},
{
    doc_name : 'Name 2',
    member_id: 'id1'
},
{
    doc_name : 'Name 5',
    member_id: 'id3'
}
{
    doc_name : 'Name 3',
    member_id: 'id2'
}

```

I'm also assuming it's returned by a bool query in a function score array, so ordered as I wish.

In my case, I want to get my results for the first 3 `member_id`s for example. My elastic should give me :

```
{
    doc_name : 'Name 1',
    member_id: 'id1'
}
{
    doc_name : 'Name 4',
    member_id: 'id4'
},
{
    doc_name : 'Name 2',
    member_id: 'id1'
},
{
    doc_name : 'Name 5',
    member_id: 'id3'
}

```

Then, I want to have sort of a "pagination" to be able to add a Load More button, which sould result in the next hits for the 3 next `member_id`s (in this case only the last result, but in reality, a lot more.

Hope I'm understandable ! English is not my native language 😉

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [May 31, 2018, 1:46pm UTC](https://discuss.elastic.co/t/paginate-elastic-results-on-specific-field/133724/6 "2018-05-31T13:46:54Z")

</div>

English is not my native language either, but I think we're doing just fine 😉

I don't think Elasticsearch offers you something out of the box without you having to do some bookkeeping on the client side. I do think field collapsing on the `member_id` field can be helpful, but it's not the complete solution for your case.

You will have to use the `from` and `size` parameters to paginate and get to pages of exactly 10 unique `member_id`s per result page. Tracking the number of members returned and remembering where you are in the pagination set is something you need to do yourself in the client application.

---

<div class="post-metadata">

**Author:** ![daganm](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@daganm](https://discuss.elastic.co/u/daganm)\
**Post date:** [May 31, 2018, 1:53pm UTC](https://discuss.elastic.co/t/paginate-elastic-results-on-specific-field/133724/7 "2018-05-31T13:53:56Z")

</div>

And using from and size to get 10 unique member\_id per result page is something I can do with field collapsing ?

Thank you,

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [May 31, 2018, 1:55pm UTC](https://discuss.elastic.co/t/paginate-elastic-results-on-specific-field/133724/8 "2018-05-31T13:55:25Z")

</div>

No, you will have to keep paginating until you get to 10 unique member\_ids. Field collapsing will just make it a bit easier to keep track of how many IDs are being returned.

---

<div class="post-metadata">

**Author:** ![daganm](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@daganm](https://discuss.elastic.co/u/daganm)\
**Post date:** [May 31, 2018, 1:57pm UTC](https://discuss.elastic.co/t/paginate-elastic-results-on-specific-field/133724/9 "2018-05-31T13:57:42Z")

</div>

So my only way to achieve that is to make it client-side, do I understand correctly ?

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [May 31, 2018, 2:02pm UTC](https://discuss.elastic.co/t/paginate-elastic-results-on-specific-field/133724/10 "2018-05-31T14:02:43Z")

</div>

Yes, I think that's the way to go, unless someone else has a better idea.

---

<div class="post-metadata">

**Author:** ![daganm](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@daganm](https://discuss.elastic.co/u/daganm)\
**Post date:** [May 31, 2018, 2:05pm UTC](https://discuss.elastic.co/t/paginate-elastic-results-on-specific-field/133724/11 "2018-05-31T14:05:18Z")

</div>

Ok ! That's what I thought. Thanks for your help, I'm totally new to elasticsearch and it can make so many things I just had to know !

Than you very much and have a nice day,  
Dagan

---

<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:** [June 28, 2018, 2:05pm UTC](https://discuss.elastic.co/t/paginate-elastic-results-on-specific-field/133724/12 "2018-06-28T14:05:19Z")

</div>

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