# Elasticsearch sort on combined fields

**URL:** <https://discuss.elastic.co/t/elasticsearch-sort-on-combined-fields/11316>\
**Category:** Elasticsearch\
**Created:** [March 26, 2013, 11:51am UTC](https://discuss.elastic.co/t/elasticsearch-sort-on-combined-fields/11316 "2013-03-26T11:51:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vannya](https://avatars.discourse-cdn.com/v4/letter/v/e495f1/32.png) [@vannya](https://discuss.elastic.co/u/vannya)\
**Post date:** [March 26, 2013, 11:51am UTC](https://discuss.elastic.co/t/elasticsearch-sort-on-combined-fields/11316/1 "2013-03-26T11:51:37Z")

</div>

Hi all,

I'm having troubles with sorting based on a combination of fields. For  
instance, i have a document:

{ "name": "blah",  
"age": 1,  
"id": "132456"}

I need to get all documents sorted by the combination of name and id. I  
don't want sort based first on name and then on id... I really need the  
combination of both!

Here:  
[http://www.elasticsearch.org/guide/reference/api/search/sort.html](http://www.elasticsearch.org/guide/reference/api/search/sort.html)

Is referred sorting based on scripting, but is not working.

Can anyone help?

Thanks,

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![DH1](https://avatars.discourse-cdn.com/v4/letter/d/65b543/32.png) [@DH1](https://discuss.elastic.co/u/DH1)\
**Post date:** [March 26, 2013, 12:52pm UTC](https://discuss.elastic.co/t/elasticsearch-sort-on-combined-fields/11316/2 "2013-03-26T12:52:30Z")

</div>

Did you specify the type? If I am not mistaken, something like this should  
work :

{  
"query": {  
"match\_all": {}  
},  
"sort": {  
"\_script": {  
"script": "doc['name'].value + doc['id'].value",  
"type": "string",  
"order": "asc"  
}  
}  
}

But I think that kind of sort will be quite heavy. If I had to do this, I  
would index another field (not analyzed) that would be that combination,  
and I'd sort on it instead. Should be more efficient.

Le mardi 26 mars 2013 12:51:37 UTC+1, Vania S. a écrit :

> Hi all,
> 
> I'm having troubles with sorting based on a combination of fields. For  
> instance, i have a document:
> 
> { "name": "blah",  
> "age": 1,  
> "id": "132456"}
> 
> I need to get all documents sorted by the combination of name and id. I  
> don't want sort based first on name and then on id... I really need the  
> combination of both!
> 
> Here:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/sort.html)
> 
> Is referred sorting based on scripting, but is not working.
> 
> Can anyone help?
> 
> Thanks,

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:44am UTC](https://discuss.elastic.co/t/elasticsearch-sort-on-combined-fields/11316/3 "2017-07-06T02:44:24Z")

</div>


