# How to Select First matching document by group by for more than 10000 records

**URL:** <https://discuss.elastic.co/t/how-to-select-first-matching-document-by-group-by-for-more-than-10000-records/220770>\
**Category:** Elasticsearch\
**Created:** [February 25, 2020, 5:41am UTC](https://discuss.elastic.co/t/how-to-select-first-matching-document-by-group-by-for-more-than-10000-records/220770 "2020-02-25T05:41:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pinky\_Agarwal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pinky_agarwal/32/49330_2.png) [@Pinky\_Agarwal](https://discuss.elastic.co/u/Pinky_Agarwal)\
**Post date:** [February 25, 2020, 5:41am UTC](https://discuss.elastic.co/t/how-to-select-first-matching-document-by-group-by-for-more-than-10000-records/220770/1 "2020-02-25T05:41:13Z")

</div>

I am using Collapse to get first element of group by with the search api. but it is working if my Limit+offset is less than 10000. for more than 10000 i am using the scroll api , collpase is not working with scroll , what laternative i can use here . My code Snippet is

return await \_elasticClient.SearchAsync(bd =\> (((input.Limit +input.Offset) \> 0 && (input.Limit + input.Offset) \< 10000)  
? bd.From(offset).Size(limit).Collapse(x =\> x.Field(f =\> f.SnippetId))  
: bd.Scroll(TimeSpan.FromMinutes(30)).Take(limit))  
.Query(TDMSnippetTableSearchQuery(input, includeCompetitors)  
));

Please help me to resolve this.

---

<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:** [March 24, 2020, 5:41am UTC](https://discuss.elastic.co/t/how-to-select-first-matching-document-by-group-by-for-more-than-10000-records/220770/2 "2020-03-24T05:41:16Z")

</div>

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