# How to collect docs in order in collect() method of a custom aggregator

**URL:** <https://discuss.elastic.co/t/how-to-collect-docs-in-order-in-collect-method-of-a-custom-aggregator/20388>\
**Category:** Elasticsearch\
**Created:** [October 22, 2014, 9:26pm UTC](https://discuss.elastic.co/t/how-to-collect-docs-in-order-in-collect-method-of-a-custom-aggregator/20388 "2014-10-22T21:26:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bsarkar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bsarkar/32/42967_2.png) [@bsarkar](https://discuss.elastic.co/u/bsarkar)\
**Post date:** [October 22, 2014, 9:26pm UTC](https://discuss.elastic.co/t/how-to-collect-docs-in-order-in-collect-method-of-a-custom-aggregator/20388/1 "2014-10-22T21:26:06Z")

</div>

I am developing a custom aggregator using ES 1.3.4. It extends from  
NumericMetricsAggregator.MultiValue class. Its code structure closely  
resembles that of the Stats aggregator. For my requirements, I need the doc  
Ids to be received in ascending order in the overridden collect() method.  
For most queries, I do get the doc Ids in ascending order. Interestingly  
for bool should queries with multiple clauses, I get doc Ids in descending  
order. How can I fix this? Is this a bug?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/f3fddb08-ef63-4378-8aa2-ea709612bbd0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/f3fddb08-ef63-4378-8aa2-ea709612bbd0%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![bsarkar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bsarkar/32/42967_2.png) [@bsarkar](https://discuss.elastic.co/u/bsarkar)\
**Post date:** [October 24, 2014, 2:39pm UTC](https://discuss.elastic.co/t/how-to-collect-docs-in-order-in-collect-method-of-a-custom-aggregator/20388/2 "2014-10-24T14:39:52Z")

</div>

I asked the same question on github and got the answer which worked for me.  
Here's the solution:

You can call aggregationContext.ensureScoreDocsInOrder(); to make sure that  
docs are going to come in order, have a look for instance at  
ReverseNestedAggregator which uses this method.

Queries are indeed allowed to emit documents out-of-order if allowed to do  
so and if it makes things faster. I believe the only case when it happens  
today is when you get Lucene's BooleanScorer which is used for top-level  
disjunctions, so your observation makes sense.

Link to the issue:

> <https://github.com/elastic/elasticsearch/issues/8216>

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/9beda810-af5c-4e5d-b2b2-0eda28aae470%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/9beda810-af5c-4e5d-b2b2-0eda28aae470%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 12:53am UTC](https://discuss.elastic.co/t/how-to-collect-docs-in-order-in-collect-method-of-a-custom-aggregator/20388/3 "2017-07-06T00:53:59Z")

</div>

Closed on July 6, 2017, 12:53am UTC


