# Collapse after rescore

**URL:** <https://discuss.elastic.co/t/collapse-after-rescore/176633>\
**Category:** Elasticsearch\
**Created:** [April 12, 2019, 1:26pm UTC](https://discuss.elastic.co/t/collapse-after-rescore/176633 "2019-04-12T13:26:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maxim\_Sychenko](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/maxim_sychenko/32/44026_2.png) [@Maxim\_Sychenko](https://discuss.elastic.co/u/Maxim_Sychenko)\
**Post date:** [April 12, 2019, 1:26pm UTC](https://discuss.elastic.co/t/collapse-after-rescore/176633/1 "2019-04-12T13:26:40Z")

</div>

Hello everyone  
I know that in ver. 5.3 (?) it would be possible to run collapse with rescore [https://github.com/elastic/elasticsearch/pull/28521](https://github.com/elastic/elasticsearch/pull/28521) and it was reverted, but the cases that I have, forcing me to develop this functionality. The main idea is to create a plugin with this behavior.  
I studied ElasticSearch source codes and this is what I have at the moment:

- QueryPhase.java has a strict execution order in `execute` method and rescore phase is executed after all collectors (in `private static boolean execute`) and before the second phase of aggregation `aggregationPhase.execute(searchContext)` is executed; Looks like the only way with plugin is to use `AggregationSpec`
- so, all i need is to create class that inherited from `InternalAggregations` to implement the `reduce` method in which i should (?) do Collapsing by id of the item sorted by score

My question is whether there are other ways to implement functionality like that (I don’t want to change the elastic code, at least for now). If not, how can you pull the score from during the aggregation (I understand that it is possible through nested aggregation with scripting that does not look so good)

My setup is elastic ver. 6.6  
Thanks!

---

<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:** [May 10, 2019, 1:26pm UTC](https://discuss.elastic.co/t/collapse-after-rescore/176633/2 "2019-05-10T13:26:42Z")

</div>

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