How does sorting on _id work?

Hi all,

I want to determine the doc with max and min _id value. So, when I run this
query:
GET /my_index/order/_search
{
"fields": [ "_id" ],
"sort": [
{ "_uid": { "order": "desc" } }
],
"size": 1
}
I get a result:
{
...
"hits": {
...
"hits": [
{
"_index": "my_index",
"_type": "order",
"_id": "999999",
"_score": null,
"sort": [
"order#999999"
]
}
]
}
}

There is definitevely a doc with _id value 11132106 in index which I would
have expected as result.

And, when I run the same search with order asc I get a result with
_id 1000000 which is higher than 999999...?

What am I doing wrong?

Regards,

Abid

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/92429b89-0741-40cc-9d76-8e1a36e5c1f5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

... can it be that _id is treated as string? If so, is there any way
retrieve the max _id field with treating _id as integer?

Am Dienstag, 27. Januar 2015 19:24:41 UTC+1 schrieb Abid Hussain:

Hi all,

I want to determine the doc with max and min _id value. So, when I run
this query:
GET /my_index/order/_search
{
"fields": [ "_id" ],
"sort": [
{ "_uid": { "order": "desc" } }
],
"size": 1
}
I get a result:
{
...
"hits": {
...
"hits": [
{
"_index": "my_index",
"_type": "order",
"_id": "999999",
"_score": null,
"sort": [
"order#999999"
]
}
]
}
}

There is definitevely a doc with _id value 11132106 in index which I would
have expected as result.

And, when I run the same search with order asc I get a result with
_id 1000000 which is higher than 999999...?

What am I doing wrong?

Regards,

Abid

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/d3e540ef-8e40-4f38-b655-db9da9b64946%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Yes, the _id field is a string. You are not limited to numbers. In fact, an
automatically generated ID has many non-numeric characters in it.

For what you want, you should create an id field, map it to a long integer,
and then copy your _id into that id field when you load the document. Then
when you sort on the id field, you will get a numeric sort.

Hope this helps.

Brian

On Tuesday, January 27, 2015 at 1:28:44 PM UTC-5, Abid Hussain wrote:

... can it be that _id is treated as string? If so, is there any way
retrieve the max _id field with treating _id as integer?

Am Dienstag, 27. Januar 2015 19:24:41 UTC+1 schrieb Abid Hussain:

Hi all,

I want to determine the doc with max and min _id value. So, when I run
this query:
GET /my_index/order/_search
{
"fields": [ "_id" ],
"sort": [
{ "_uid": { "order": "desc" } }
],
"size": 1
}
I get a result:
{
...
"hits": {
...
"hits": [
{
"_index": "my_index",
"_type": "order",
"_id": "999999",
"_score": null,
"sort": [
"order#999999"
]
}
]
}
}

There is definitevely a doc with _id value 11132106 in index which I
would have expected as result.

And, when I run the same search with order asc I get a result with
_id 1000000 which is higher than 999999...?

What am I doing wrong?

Regards,

Abid

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/7842cdcf-67ee-48ed-8ef6-e8be2bb63a4a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

1 Like