# Sorting by \_uid

**URL:** https://discuss.elastic.co/t/sorting-by--uid/12956
**Category:** Elasticsearch
**Created:** [July 26, 2013, 3:09pm UTC](https://discuss.elastic.co/t/sorting-by--uid/12956 "2013-07-26T15:09:08Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Roy\_Russo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roy_russo/32/44835_2.png) [@Roy\_Russo](https://discuss.elastic.co/u/Roy_Russo)
#### Post date: [July 26, 2013, 3:09pm UTC](https://discuss.elastic.co/t/sorting-by--uid/12956/1 "2013-07-26T15:09:08Z")

</div>

Hello all,

When sorting by ES's internal ID (\_uid), I've noticed that the uid field is  
not treated/sorted as numeric, even though ES assigns it a numeric value.  
Is this by design?

Example: (Note that "2" comes after "144")

{"query":{"filtered":{"query":{"query\_string":{"query":"captain"}}}},"from":  
0,"size":50,"sort":{"\_uid":{"order":"asc"}}}:

output:

## 5: { \_index: "comicbook" \_type: "superhero" \_id: "144" \_score: null \_source: { name: "Bart Allen" }- sort: [1] 0: "superhero#144"

## }- 6: { \_index: "comicbook" \_type: "superhero" \_id: "2" \_score: null \_source: { name: "Spider-Man" }- sort: [1] 0: "superhero#2"

}-  
7: {  
\_index: "comicbook"  
\_type: "superhero"  
\_id: "20"  
\_score: null  
\_source: {  
name: "Captain Marvel"

--  
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: ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)
#### Post date: [July 26, 2013, 9:34pm UTC](https://discuss.elastic.co/t/sorting-by--uid/12956/2 "2013-07-26T21:34:37Z")

</div>

I am not positive, but I don't believe the \_uid is indexed by default (\_id  
is not) and therefore you will not be able to sort on it.

--  
Ivan

On Fri, Jul 26, 2013 at 8:09 AM, Roy Russo [royrusso@gmail.com](mailto:royrusso@gmail.com) wrote:

> Hello all,
> 
> When sorting by ES's internal ID (\_uid), I've noticed that the uid field  
> is not treated/sorted as numeric, even though ES assigns it a numeric  
> value. Is this by design?
> 
> Example: (Note that "2" comes after "144")
> 
> {"query":{"filtered":{"query":{"query\_string":{"query":"captain"}}}},  
> "from":0,"size":50,"sort":{"\_uid":{"order":"asc"}}}:
> 
> output:
> 
> ## 5: { \_index: "comicbook" \_type: "superhero" \_id: "144" \_score: null \_source: { name: "Bart Allen" }- sort: [1] 0: "superhero#144"
> 
> ## }- 6: { \_index: "comicbook" \_type: "superhero" \_id: "2" \_score: null \_source: { name: "Spider-Man" }- sort: [1] 0: "superhero#2"
> 
> }-  
> 7: {  
> \_index: "comicbook"  
> \_type: "superhero"  
> \_id: "20"  
> \_score: null  
> \_source: {  
> name: "Captain Marvel"
> 
> --  
> 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).

--  
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: ![javanna](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javanna/32/4698_2.png) [@javanna](https://discuss.elastic.co/u/javanna)
#### Post date: [July 27, 2013, 10:22am UTC](https://discuss.elastic.co/t/sorting-by--uid/12956/3 "2013-07-27T10:22:04Z")

</div>

The \_uid field[http://www.elasticsearch.org/guide/reference/mapping/uid-field/](http://www.elasticsearch.org/guide/reference/mapping/uid-field/)is indexed by default, while the \_id  
field [http://www.elasticsearch.org/guide/reference/mapping/id-field/](http://www.elasticsearch.org/guide/reference/mapping/id-field/)isn't.

The \_uid field contains both the \_type and the \_id field. Since both of  
those fields aren't necessarily numeric, \_uid is not numeric either. That's  
why numbers are not sorted properly. Also, if you sort on the \_uid field  
when querying multiple types, then all the documents that belong to one of  
the types will be listed, then the other type...and so on.

I would define a specific field for sorting, with proper mapping so that  
it's indexed and treated as a number.

Cheers  
Luca

On Friday, July 26, 2013 11:34:37 PM UTC+2, Ivan Brusic wrote:

> I am not positive, but I don't believe the \_uid is indexed by default (\_id  
> is not) and therefore you will not be able to sort on it.
> 
> --  
> Ivan
> 
> On Fri, Jul 26, 2013 at 8:09 AM, Roy Russo \<[royr...@gmail.com](mailto:royr...@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > Hello all,
> > 
> > When sorting by ES's internal ID (\_uid), I've noticed that the uid field  
> > is not treated/sorted as numeric, even though ES assigns it a numeric  
> > value. Is this by design?
> > 
> > Example: (Note that "2" comes after "144")
> > 
> > {"query":{"filtered":{"query":{"query\_string":{"query":"captain"}}}},  
> > "from":0,"size":50,"sort":{"\_uid":{"order":"asc"}}}:
> > 
> > output:
> > 
> > ## 5: { \_index: "comicbook" \_type: "superhero" \_id: "144" \_score: null \_source: { name: "Bart Allen" }- sort: [1] 0: "superhero#144"
> > 
> > ## }- 6: { \_index: "comicbook" \_type: "superhero" \_id: "2" \_score: null \_source: { name: "Spider-Man" }- sort: [1] 0: "superhero#2"
> > 
> > }-  
> > 7: {  
> > \_index: "comicbook"  
> > \_type: "superhero"  
> > \_id: "20"  
> > \_score: null  
> > \_source: {  
> > name: "Captain Marvel"
> > 
> > --  
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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: ![Roy\_Russo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roy_russo/32/44835_2.png) [@Roy\_Russo](https://discuss.elastic.co/u/Roy_Russo)
#### Post date: [July 29, 2013, 2:52pm UTC](https://discuss.elastic.co/t/sorting-by--uid/12956/4 "2013-07-29T14:52:06Z")

</div>

Thanks Luca! This jives with what I read in another post... the \_type + \_id  
= \_uid

On Saturday, July 27, 2013 6:22:04 AM UTC-4, Luca Cavanna wrote:

> The \_uid field[http://www.elasticsearch.org/guide/reference/mapping/uid-field/](http://www.elasticsearch.org/guide/reference/mapping/uid-field/)is indexed by default, while the \_id  
> field [http://www.elasticsearch.org/guide/reference/mapping/id-field/](http://www.elasticsearch.org/guide/reference/mapping/id-field/)isn't.
> 
> The \_uid field contains both the \_type and the \_id field. Since both of  
> those fields aren't necessarily numeric, \_uid is not numeric either. That's  
> why numbers are not sorted properly. Also, if you sort on the \_uid field  
> when querying multiple types, then all the documents that belong to one of  
> the types will be listed, then the other type...and so on.
> 
> I would define a specific field for sorting, with proper mapping so that  
> it's indexed and treated as a number.
> 
> Cheers  
> Luca
> 
> On Friday, July 26, 2013 11:34:37 PM UTC+2, Ivan Brusic wrote:
> 
> > I am not positive, but I don't believe the \_uid is indexed by default  
> > (\_id is not) and therefore you will not be able to sort on it.
> > 
> > --  
> > Ivan
> > 
> > On Fri, Jul 26, 2013 at 8:09 AM, Roy Russo [royr...@gmail.com](mailto:royr...@gmail.com) wrote:
> > 
> > > Hello all,
> > > 
> > > When sorting by ES's internal ID (\_uid), I've noticed that the uid field  
> > > is not treated/sorted as numeric, even though ES assigns it a numeric  
> > > value. Is this by design?
> > > 
> > > Example: (Note that "2" comes after "144")
> > > 
> > > {"query":{"filtered":{"query":{"query\_string":{"query":"captain"}}}},  
> > > "from":0,"size":50,"sort":{"\_uid":{"order":"asc"}}}:
> > > 
> > > output:
> > > 
> > > ## 5: { \_index: "comicbook" \_type: "superhero" \_id: "144" \_score: null \_source: { name: "Bart Allen" }- sort: [1] 0: "superhero#144"
> > > 
> > > ## }- 6: { \_index: "comicbook" \_type: "superhero" \_id: "2" \_score: null \_source: { name: "Spider-Man" }- sort: [1] 0: "superhero#2"
> > > 
> > > }-  
> > > 7: {  
> > > \_index: "comicbook"  
> > > \_type: "superhero"  
> > > \_id: "20"  
> > > \_score: null  
> > > \_source: {  
> > > name: "Captain Marvel"
> > > 
> > > --  
> > > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).  
> > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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:23am UTC](https://discuss.elastic.co/t/sorting-by--uid/12956/5 "2017-07-06T02:23:59Z")

</div>


