# Sorting in facets

**URL:** <https://discuss.elastic.co/t/sorting-in-facets/9510>\
**Category:** Elasticsearch\
**Created:** [October 31, 2012, 2:34pm UTC](https://discuss.elastic.co/t/sorting-in-facets/9510 "2012-10-31T14:34:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_Bednar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/martin_bednar/32/2664_2.png) [@Martin\_Bednar](https://discuss.elastic.co/u/Martin_Bednar)\
**Post date:** [October 31, 2012, 2:34pm UTC](https://discuss.elastic.co/t/sorting-in-facets/9510/1 "2012-10-31T14:34:23Z")

</div>

Hi,

When I use custom facets (based on imotov/elasticsearch-facet-script), are  
data in mapping phase sorted, when I use sort:{} in query ?  
Or how can I get sorted data in mapping phase ?

Thx Bedy

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [October 31, 2012, 9:24pm UTC](https://discuss.elastic.co/t/sorting-in-facets/9510/2 "2012-10-31T21:24:50Z")

</div>

No, it's not sorted. Moreover, mappers are running on individual shards and  
possible different nodes, so they don't even see all data. If you want data  
sorted, you would have to sort it yourself in mappers and then merge sort  
results in the reducer.

On Wednesday, October 31, 2012 10:34:23 AM UTC-4, Martin Bednář wrote:

> Hi,
> 
> When I use custom facets (based on imotov/elasticsearch-facet-script),  
> are data in mapping phase sorted, when I use sort:{} in query ?  
> Or how can I get sorted data in mapping phase ?
> 
> Thx Bedy

--

---

<div class="post-metadata">

**Author:** ![Martin\_Bednar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/martin_bednar/32/2664_2.png) [@Martin\_Bednar](https://discuss.elastic.co/u/Martin_Bednar)\
**Post date:** [October 31, 2012, 11:51pm UTC](https://discuss.elastic.co/t/sorting-in-facets/9510/3 "2012-10-31T23:51:48Z")

</div>

Hi Igor,

I have data on shards distributed using \_routing attribute, so it guarantee that data with same key are only on that shard, right ?  
But there will be also data with different \_routing value, and I need order it by this attribute and process during mapping phase in that order.

Example: I have something like this, routed by cuid attribute:

{ "cuid":"A", "price":1," amount":20}  
{ "cuid":"B", "price":2," amount":20}  
{ "cuid":"C", "price":3," amount":20}  
{ "cuid":"A", "price":4," amount":20}  
{ "cuid":"C", "price":5," amount":20}  
{ "cuid":"A", "price":6," amount":20}

After distribution it's stored on shard 1 and 3 for example:

Shard1:  
{ "cuid":"A", "price":1," amount":20}  
{ "cuid":"B", "price":2," amount":20}  
{ "cuid":"A", "price":4," amount":20}  
{ "cuid":"A", "price":6," amount":20}

Shard2:  
{ "cuid":"C", "price":3," amount":20}  
{ "cuid":"C", "price":5," amount":20}

And I need process it during mapping as:  
Shard1:  
{ "cuid":"A", "price":1," amount":20}  
{ "cuid":"A", "price":4," amount":20}  
{ "cuid":"A", "price":6," amount":20}  
{ "cuid":"B", "price":2," amount":20}

Shard2:  
{ "cuid":"C", "price":3," amount":20}  
{ "cuid":"C", "price":5," amount":20}

Are there some possibility to do that?  
And thanks for facet-script plugin, I'm just waiting for something like this.

Thx Bedy

1. 
  1. 2012 v 22:24, Igor Motov [imotov@gmail.com](mailto:imotov@gmail.com):

> No, it's not sorted. Moreover, mappers are running on individual shards and possible different nodes, so they don't even see all data. If you want data sorted, you would have to sort it yourself in mappers and then merge sort results in the reducer.
> 
> On Wednesday, October 31, 2012 10:34:23 AM UTC-4, Martin Bednář wrote:  
> Hi,
> 
> When I use custom facets (based on imotov/elasticsearch-facet-script), are data in mapping phase sorted, when I use sort:{} in query ?  
> Or how can I get sorted data in mapping phase ?
> 
> Thx Bedy
> 
> --

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [November 1, 2012, 12:13am UTC](https://discuss.elastic.co/t/sorting-in-facets/9510/4 "2012-11-01T00:13:16Z")

</div>

I think you will have to collect all data during mapping phase and then  
sort and process it in the combine phase. I can't really think of any other  
way to do it.

On Wednesday, October 31, 2012 7:51:58 PM UTC-4, Martin Bednář wrote:

> Hi Igor,
> 
> I have data on shards distributed using \_routing attribute, so it  
> guarantee that data with same key are only on that shard, right ?  
> But there will be also data with different \_routing value, and I need  
> order it by this attribute and process during mapping phase in that order.
> 
> Example: I have something like this, routed by cuid attribute:
> 
> { "cuid":"A", "price":1," amount":20}  
> { "cuid":"B", "price":2," amount":20}  
> { "cuid":"C", "price":3," amount":20}  
> { "cuid":"A", "price":4," amount":20}  
> { "cuid":"C", "price":5," amount":20}  
> { "cuid":"A", "price":6," amount":20}
> 
> After distribution it's stored on shard 1 and 3 for example:
> 
> Shard1:  
> { "cuid":"A", "price":1," amount":20}  
> { "cuid":"B", "price":2," amount":20}  
> { "cuid":"A", "price":4," amount":20}  
> { "cuid":"A", "price":6," amount":20}
> 
> Shard2:  
> { "cuid":"C", "price":3," amount":20}  
> { "cuid":"C", "price":5," amount":20}
> 
> And I need process it during mapping as:  
> Shard1:  
> { "cuid":"A", "price":1," amount":20}  
> { "cuid":"A", "price":4," amount":20}  
> { "cuid":"A", "price":6," amount":20}  
> { "cuid":"B", "price":2," amount":20}
> 
> Shard2:  
> { "cuid":"C", "price":3," amount":20}  
> { "cuid":"C", "price":5," amount":20}
> 
> Are there some possibility to do that?  
> And thanks for facet-script plugin, I'm just waiting for something like  
> this.
> 
> Thx Bedy
> 
> 1. 
> 1. 2012 v 22:24, Igor Motov \<[imo...@gmail.com](mailto:imo...@gmail.com) \<javascript:\>\>:
> 
> No, it's not sorted. Moreover, mappers are running on individual shards  
> and possible different nodes, so they don't even see all data. If you want  
> data sorted, you would have to sort it yourself in mappers and then merge  
> sort results in the reducer.
> 
> On Wednesday, October 31, 2012 10:34:23 AM UTC-4, Martin Bednář wrote:
> 
> > Hi,
> > 
> > When I use custom facets (based on imotov/elasticsearch-facet-script),  
> > are data in mapping phase sorted, when I use sort:{} in query ?  
> > Or how can I get sorted data in mapping phase ?
> > 
> > Thx Bedy
> 
> --

--

---

<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, 3:06am UTC](https://discuss.elastic.co/t/sorting-in-facets/9510/5 "2017-07-06T03:06:39Z")

</div>


