# Sorting across multiple types

**URL:** <https://discuss.elastic.co/t/sorting-across-multiple-types/5406>\
**Category:** Elasticsearch\
**Created:** [September 19, 2011, 12:37pm UTC](https://discuss.elastic.co/t/sorting-across-multiple-types/5406 "2011-09-19T12:37:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Baxter](https://avatars.discourse-cdn.com/v4/letter/c/f19dbf/32.png) [@Chris\_Baxter](https://discuss.elastic.co/u/Chris_Baxter)\
**Post date:** [September 19, 2011, 12:37pm UTC](https://discuss.elastic.co/t/sorting-across-multiple-types/5406/1 "2011-09-19T12:37:15Z")

</div>

I'm currently using elastic search with two types of entities being  
indexed. Both entities have a field called "name". I'm issuing a  
query with a sort on "name" (actually using "nameSort" which is non-  
analyzed) and the results are returning sorted correctly within each  
type, but not universally across the two types. Is there any way to  
get the results to return sorted completely across the two types?

---

<div class="post-metadata">

**Author:** ![davrob](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@davrob](https://discuss.elastic.co/u/davrob)\
**Post date:** [September 19, 2011, 1:17pm UTC](https://discuss.elastic.co/t/sorting-across-multiple-types/5406/2 "2011-09-19T13:17:20Z")

</div>

You could use a custom sort script, like here:  
[http://elasticsearch-users.115913.n3.nabble.com/Native-Script-Help-td2980754.html#a2981370](http://elasticsearch-users.115913.n3.nabble.com/Native-Script-Help-td2980754.html#a2981370)  
but that might impact your performance.

On Sep 19, 1:37 pm, Chris Baxter [cbax...@gmail.com](mailto:cbax...@gmail.com) wrote:

> I'm currently using Elasticsearch with two types of entities being  
> indexed. Both entities have a field called "name". I'm issuing a  
> query with a sort on "name" (actually using "nameSort" which is non-  
> analyzed) and the results are returning sorted correctly within each  
> type, but not universally across the two types. Is there any way to  
> get the results to return sorted completely across the two types?

---

<div class="post-metadata">

**Author:** ![Chris\_Baxter](https://avatars.discourse-cdn.com/v4/letter/c/f19dbf/32.png) [@Chris\_Baxter](https://discuss.elastic.co/u/Chris_Baxter)\
**Post date:** [September 19, 2011, 1:54pm UTC](https://discuss.elastic.co/t/sorting-across-multiple-types/5406/3 "2011-09-19T13:54:15Z")

</div>

So I'm guessing by your response that this is not just supported out  
of the box.

On Sep 19, 9:17 am, davrob2 [davirobe...@gmail.com](mailto:davirobe...@gmail.com) wrote:

> You could use a custom sort script, like here:[http://elasticsearch-users.115913.n3.nabble.com/Native-Script-Help-td](http://elasticsearch-users.115913.n3.nabble.com/Native-Script-Help-td)...  
> but that might impact your performance.
> 
> On Sep 19, 1:37 pm, Chris Baxter [cbax...@gmail.com](mailto:cbax...@gmail.com) wrote:
> 
> > I'm currently using Elasticsearch with two types of entities being  
> > indexed. Both entities have a field called "name". I'm issuing a  
> > query with a sort on "name" (actually using "nameSort" which is non-  
> > analyzed) and the results are returning sorted correctly within each  
> > type, but not universally across the two types. Is there any way to  
> > get the results to return sorted completely across the two types?

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [September 20, 2011, 8:20am UTC](https://discuss.elastic.co/t/sorting-across-multiple-types/5406/4 "2011-09-20T08:20:20Z")

</div>

This is supported, you can sort across types on the same field. (internally,  
different types ends up being indexed as documents in the same Lucene  
index). Can you gist a sample for a failure, including some data setup? (  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/help)).

On Mon, Sep 19, 2011 at 4:54 PM, Chris Baxter [cbax007@gmail.com](mailto:cbax007@gmail.com) wrote:

> So I'm guessing by your response that this is not just supported out  
> of the box.
> 
> On Sep 19, 9:17 am, davrob2 [davirobe...@gmail.com](mailto:davirobe...@gmail.com) wrote:
> 
> > You could use a custom sort script, like here:  
> > [http://elasticsearch-users.115913.n3.nabble.com/Native-Script-Help-td](http://elasticsearch-users.115913.n3.nabble.com/Native-Script-Help-td)...  
> > but that might impact your performance.
> > 
> > On Sep 19, 1:37 pm, Chris Baxter [cbax...@gmail.com](mailto:cbax...@gmail.com) wrote:
> > 
> > > I'm currently using Elasticsearch with two types of entities being  
> > > indexed. Both entities have a field called "name". I'm issuing a  
> > > query with a sort on "name" (actually using "nameSort" which is non-  
> > > analyzed) and the results are returning sorted correctly within each  
> > > type, but not universally across the two types. Is there any way to  
> > > get the results to return sorted completely across the two types?

---

<div class="post-metadata">

**Author:** ![Chris\_Baxter](https://avatars.discourse-cdn.com/v4/letter/c/f19dbf/32.png) [@Chris\_Baxter](https://discuss.elastic.co/u/Chris_Baxter)\
**Post date:** [September 20, 2011, 11:19am UTC](https://discuss.elastic.co/t/sorting-across-multiple-types/5406/5 "2011-09-20T11:19:03Z")

</div>

I figured out my issue. I had mixed case in my names across the two  
different types, with one of the types always being all lower.  
Because the field I am sorting on is not analyzed (only way to  
properly sort on it), the case was not being normalized when the  
entity was indexed. I fixed this in my app code to lowercase this  
field specifically before indexing and that fixed the problem. Thanks  
for letting me know that this should have worked as that let me know  
that the problem was probably on my side.

On Sep 20, 4:20 am, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:

> This is supported, you can sort across types on the same field. (internally,  
> different types ends up being indexed as documents in the same Lucene  
> index). Can you gist a sample for a failure, including some data setup? ([Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/help)).
> 
> On Mon, Sep 19, 2011 at 4:54 PM, Chris Baxter [cbax...@gmail.com](mailto:cbax...@gmail.com) wrote:
> 
> > So I'm guessing by your response that this is not just supported out  
> > of the box.
> 
> > On Sep 19, 9:17 am, davrob2 [davirobe...@gmail.com](mailto:davirobe...@gmail.com) wrote:
> > 
> > > You could use a custom sort script, like here:  
> > > [http://elasticsearch-users.115913.n3.nabble.com/Native-Script-Help-td](http://elasticsearch-users.115913.n3.nabble.com/Native-Script-Help-td)...  
> > > but that might impact your performance.
> 
> > > On Sep 19, 1:37 pm, Chris Baxter [cbax...@gmail.com](mailto:cbax...@gmail.com) wrote:
> 
> > > > I'm currently using Elasticsearch with two types of entities being  
> > > > indexed. Both entities have a field called "name". I'm issuing a  
> > > > query with a sort on "name" (actually using "nameSort" which is non-  
> > > > analyzed) and the results are returning sorted correctly within each  
> > > > type, but not universally across the two types. Is there any way to  
> > > > get the results to return sorted completely across the two types?

---

<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:54am UTC](https://discuss.elastic.co/t/sorting-across-multiple-types/5406/6 "2017-07-06T03:54:02Z")

</div>


