# Sort on different fields depending on type

**URL:** <https://discuss.elastic.co/t/sort-on-different-fields-depending-on-type/9283>\
**Category:** Elasticsearch\
**Created:** [October 9, 2012, 8:43am UTC](https://discuss.elastic.co/t/sort-on-different-fields-depending-on-type/9283 "2012-10-09T08:43:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Per](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Per](https://discuss.elastic.co/u/Per)\
**Post date:** [October 9, 2012, 8:43am UTC](https://discuss.elastic.co/t/sort-on-different-fields-depending-on-type/9283/1 "2012-10-09T08:43:52Z")

</div>

I would like to search in an index over multiple types and then sort based  
on different (date) fields depending on type. Example:

index/type1/dateField1  
index/type2/dateField2

Is this possible to get a single result list sorted on dateField1 or  
dateField2? A workaround would be introducing a new field  
"dateFieldCommon" for type1 and type2 but is there a better way?

--

---

<div class="post-metadata">

**Author:** ![Per](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Per](https://discuss.elastic.co/u/Per)\
**Post date:** [October 9, 2012, 1:27pm UTC](https://discuss.elastic.co/t/sort-on-different-fields-depending-on-type/9283/2 "2012-10-09T13:27:41Z")

</div>

I was able to get single sorted result list using script based sorting.  
Don't know if it's a good solution though:

curl -XGET '[http://localhost:9200/index/\_search](http://localhost:9200/index/_search)' -d '{  
"size" : 10,  
"query" : {  
"term" : { "content" : "foo" }  
},  
"sort" : {  
"\_script" : {  
"script" : "if(doc["dateField1"].empty) {  
doc. dateField2.value } else {doc. dateField1.value}",  
"type" : "number",  
"order" : "desc"  
}  
}  
}  
'

On Tuesday, October 9, 2012 10:43:52 AM UTC+2, Per wrote:

> I would like to search in an index over multiple types and then sort based  
> on different (date) fields depending on type. Example:
> 
> index/type1/dateField1  
> index/type2/dateField2
> 
> Is this possible to get a single result list sorted on dateField1 or  
> dateField2? A workaround would be introducing a new field  
> "dateFieldCommon" for type1 and type2 but is there a better way?

--

---

<div class="post-metadata">

**Author:** ![Raffaele\_Sena](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raffaele_sena/32/1759_2.png) [@Raffaele\_Sena](https://discuss.elastic.co/u/Raffaele_Sena)\
**Post date:** [October 9, 2012, 4:10pm UTC](https://discuss.elastic.co/t/sort-on-different-fields-depending-on-type/9283/3 "2012-10-09T16:10:17Z")

</div>

Given the way you structured your script you could also try sorting on  
dateField1, dateField2 if you specify that missing fields go last:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

something like this (and I am not sure if you'll also need the  
ignore\_unmapped) :

"sort" : [  
{ "dateField1" : {"missing" : "\_last"} },  
{ "dateField2" : {"missing" : "\_last"} },  
],

-- Raffaele

On Tue, Oct 9, 2012 at 6:27 AM, Per [dev2null@gmail.com](mailto:dev2null@gmail.com) wrote:

> I was able to get single sorted result list using script based sorting.  
> Don't know if it's a good solution though:
> 
> curl -XGET '[http://localhost:9200/index/\_search](http://localhost:9200/index/_search)' -d '{  
> "size" : 10,  
> "query" : {  
> "term" : { "content" : "foo" }  
> },  
> "sort" : {  
> "\_script" : {  
> "script" : "if(doc["dateField1"].empty) { doc.  
> dateField2.value } else {doc. dateField1.value}",  
> "type" : "number",  
> "order" : "desc"  
> }  
> }  
> }  
> '
> 
> On Tuesday, October 9, 2012 10:43:52 AM UTC+2, Per wrote:
> 
> > I would like to search in an index over multiple types and then sort based  
> > on different (date) fields depending on type. Example:
> > 
> > index/type1/dateField1  
> > index/type2/dateField2
> > 
> > Is this possible to get a single result list sorted on dateField1 or  
> > dateField2? A workaround would be introducing a new field "dateFieldCommon"  
> > for type1 and type2 but is there a better way?
> 
> --

--

---

<div class="post-metadata">

**Author:** ![Per](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Per](https://discuss.elastic.co/u/Per)\
**Post date:** [October 9, 2012, 6:42pm UTC](https://discuss.elastic.co/t/sort-on-different-fields-depending-on-type/9283/4 "2012-10-09T18:42:56Z")

</div>

Thanks for the suggestion but I don't think that it would work quite as I  
wanted. With  
"sort" : [  
{ "dateField1" : {"missing" : "\_last"} },  
{ "dateField2" : {"missing" : "\_last"} },  
],

es would first sort based on dateField1 then on dateField2. \_last seems to  
map to 0x7FFFFFFFFFFFFFFF ..

Here are a bunch of curl commands to create test data then sort:

curl -XPUT '[http://localhost:9200/myindex/type1/1](http://localhost:9200/myindex/type1/1)' -d '{  
"dateField1" : "2009-11-15T14:12:12",  
"name" : "type1.1"  
}'

curl -XPUT '[http://localhost:9200/myindex/type1/2](http://localhost:9200/myindex/type1/2)' -d '{  
"dateField1" : "2012-01-11T14:12:12",  
"name" : "type1.2"  
}'

curl -XPUT '[http://localhost:9200/myindex/type2/1](http://localhost:9200/myindex/type2/1)' -d '{  
"dateField2" : "2011-02-12T14:12:12",  
"name" : "type2.1"  
}'

curl -XPUT '[http://localhost:9200/myindex/type2/2](http://localhost:9200/myindex/type2/2)' -d '{  
"dateField2" : "2010-03-22T14:12:12",  
"name" : "type2.2"  
}'

curl -XGET '[http://localhost:9200/myindex/\_search](http://localhost:9200/myindex/_search)' -d '{  
"size" : 10,  
"query" : {  
"match\_all" : {}  
},  
"sort" : {  
"\_script" : {  
"script" : "if(!doc["dateField1"].empty) { doc.  
dateField1.value } else {doc. dateField2.value}",  
"type" : "number",  
"order" : "desc"  
}  
}  
}'

On Tuesday, October 9, 2012 6:10:22 PM UTC+2, Raffaele Sena wrote:

> Given the way you structured your script you could also try sorting on  
> dateField1, dateField2 if you specify that missing fields go last:
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/sort.html)
> 
> something like this (and I am not sure if you'll also need the  
> ignore\_unmapped) :
> 
> "sort" : [  
> { "dateField1" : {"missing" : "\_last"} },  
> { "dateField2" : {"missing" : "\_last"} },  
> ],
> 
> -- Raffaele
> 
> On Tue, Oct 9, 2012 at 6:27 AM, Per \<[dev2...@gmail.com](mailto:dev2...@gmail.com) \<javascript:\>\>  
> wrote:
> 
> > I was able to get single sorted result list using script based sorting.  
> > Don't know if it's a good solution though:
> > 
> > curl -XGET '[http://localhost:9200/index/\_search](http://localhost:9200/index/_search)' -d '{  
> > "size" : 10,  
> > "query" : {  
> > "term" : { "content" : "foo" }  
> > },  
> > "sort" : {  
> > "\_script" : {  
> > "script" : "if(doc["dateField1"].empty) { doc.  
> > dateField2.value } else {doc. dateField1.value}",  
> > "type" : "number",  
> > "order" : "desc"  
> > }  
> > }  
> > }  
> > '
> > 
> > On Tuesday, October 9, 2012 10:43:52 AM UTC+2, Per wrote:
> > 
> > > I would like to search in an index over multiple types and then sort  
> > > based  
> > > on different (date) fields depending on type. Example:
> > > 
> > > index/type1/dateField1  
> > > index/type2/dateField2
> > > 
> > > Is this possible to get a single result list sorted on dateField1 or  
> > > dateField2? A workaround would be introducing a new field  
> > > "dateFieldCommon"  
> > > for type1 and type2 but is there a better way?
> > 
> > --

--

---

<div class="post-metadata">

**Author:** ![Zazik](https://avatars.discourse-cdn.com/v4/letter/z/a6a055/32.png) [@Zazik](https://discuss.elastic.co/u/Zazik)\
**Post date:** [March 6, 2017, 4:07pm UTC](https://discuss.elastic.co/t/sort-on-different-fields-depending-on-type/9283/5 "2017-03-06T16:07:44Z")

</div>

Quite an old question, but I wonder if there is a native support for such sorting (mentioned by @Per) in the newest versions of Elasticsearch? (I have a demand to cover an exact scenario). Otherwise, how critical is performance degradation in script-based solution? Will it be noticeable?

---

<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 5, 2017, 10:02pm UTC](https://discuss.elastic.co/t/sort-on-different-fields-depending-on-type/9283/6 "2017-07-05T22:02:33Z")

</div>


