# Order by name doesn't work as expected

**URL:** <https://discuss.elastic.co/t/order-by-name-doesnt-work-as-expected/15083>\
**Category:** Elasticsearch\
**Created:** [January 2, 2014, 4:20pm UTC](https://discuss.elastic.co/t/order-by-name-doesnt-work-as-expected/15083 "2014-01-02T16:20:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nikolay\_Chankov](https://avatars.discourse-cdn.com/v4/letter/n/a8b319/32.png) [@Nikolay\_Chankov](https://discuss.elastic.co/u/Nikolay_Chankov)\
**Post date:** [January 2, 2014, 4:20pm UTC](https://discuss.elastic.co/t/order-by-name-doesnt-work-as-expected/15083/1 "2014-01-02T16:20:17Z")

</div>

Hi guys,

for some reason, the order by name, \_score is not working as I would expect.  
I've prepared a simple example to explain what I mean.  
There are 2 records: john doe and jane doe. if there is no email in the  
index their score is the same, and the order is correct, jane goes before  
john, but if john's record has email which contain doe (the search phrase),  
john \_score is higher and the order is wrong.  
I've noticed that in the results the "sort" node is ["doe", 0.6328839], [  
"doe", 0.48819983 ] rather than ["john doe", 0.6328839], [ "jane doe",  
0.48819983 ]. if the order is name:desc the search is [ "jane", 0.6328839  
], ["john", 0.48819983]

This happen when I use query:{...}. If the query is missing the results get  
the same weight and it is working as expected.

do I need to make special sort somehow in order to get the desired order,  
or it's a bug?

Thanks in advance.

Here is the script how to see this behavior. I am using 0.90.5 if it does  
matter (tested 0.90.8 with the same effect). BTW, if the name is without a  
space e.g. johndoe, janedoe the order is correct.

curl -XDELETE '[http://localhost:9200/test\_search](http://localhost:9200/test_search)'  
curl -XPUT '[http://localhost:9200/test\_search/](http://localhost:9200/test_search/)' -d '  
{  
"mappings" : {  
"record" : {  
"properties" : {  
"object" : {  
"type" : "string"  
},  
"id" : {  
"type" : "integer"  
},  
"name" : {  
"type" : "string",  
"boost" : 6  
},  
"email" : {  
"type" : "string",  
"boost" : 5  
}  
}  
}  
}

}  
'  
curl -XPUT '[http://localhost:9200/test\_search/record/1](http://localhost:9200/test_search/record/1)' -d '{  
"object" : "User",  
"id" : 1,  
"name" : "john doe",  
"email" : "doe@doe.com"  
}'  
curl -XPUT '[http://localhost:9200/test\_search/record/2](http://localhost:9200/test_search/record/2)' -d '{  
"object" : "User",  
"id" : 2,  
"name" : "jane doe",  
"email" : "j@d.com"  
}'

curl -XGET '[http://localhost:9200/test\_search/\_search?pretty=true](http://localhost:9200/test_search/_search?pretty=true)' -d  
'{"query":{"filtered":{"query":{"queryString":{"query":"doe"}}}},"sort":[{"name":"asc"},"\_score"]}'

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/f8fbba45-186a-42b2-86be-970ef65e60a5%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/f8fbba45-186a-42b2-86be-970ef65e60a5%40googlegroups.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:** [January 2, 2014, 8:23pm UTC](https://discuss.elastic.co/t/order-by-name-doesnt-work-as-expected/15083/2 "2014-01-02T20:23:46Z")

</div>

The "name" field is an analyzed string field, so it can generate numerous  
tokens depending on the text. In your case, the email is being split into  
two tokens:

[http://localhost:9200/\_analyze?text=john%20doe](http://localhost:9200/_analyze?text=john%20doe)

{"tokens": [{"token": "john","start\_offset": 0,"end\_offset": 4,"type": "  
","position": 1},{"token": "doe","start\_offset": 5,"end\_offset": 8  
,"type": "","position": 2}]}

Very likely, the sort is using sorting using the "doe" token to sort,  
making the two documents equal. Their equality is the reason why to order  
changes, since something else is breaking the tie (or random). Sorting does  
not work well on analyzed fields, so you should either:

1. set the field as not\_analyzed which might affect searching against it
2. use a keyword tokenizer. Same as above, you can apply more filters such  
as lowecase and ascii/icu folding
3. Use the multi field type [1]. This type will allow you to output  
different fields: one to search one and one to sort on.

Also note that scoring is disabled when you sort against a field. You would  
need to enable score traking. [2]

[1]

> **[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.

[2]

> **[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.

Cheers,

Ivan

On Thu, Jan 2, 2014 at 8:20 AM, Nikolay Chankov [nchankov@gmail.com](mailto:nchankov@gmail.com) wrote:

> Hi guys,
> 
> for some reason, the order by name, \_score is not working as I would  
> expect.  
> I've prepared a simple example to explain what I mean.  
> There are 2 records: john doe and jane doe. if there is no email in the  
> index their score is the same, and the order is correct, jane goes before  
> john, but if john's record has email which contain doe (the search phrase),  
> john \_score is higher and the order is wrong.  
> I've noticed that in the results the "sort" node is [ "doe", 0.6328839  
> ], ["doe", 0.48819983] rather than ["john doe", 0.6328839], [ "jane  
> doe", 0.48819983 ]. if the order is name:desc the search is [ "jane",  
> 0.6328839 ], ["john", 0.48819983]
> 
> This happen when I use query:{...}. If the query is missing the results  
> get the same weight and it is working as expected.
> 
> do I need to make special sort somehow in order to get the desired order,  
> or it's a bug?
> 
> Thanks in advance.
> 
> Here is the script how to see this behavior. I am using 0.90.5 if it does  
> matter (tested 0.90.8 with the same effect). BTW, if the name is without a  
> space e.g. johndoe, janedoe the order is correct.
> 
> curl -XDELETE '[http://localhost:9200/test\_search](http://localhost:9200/test_search)'  
> curl -XPUT '[http://localhost:9200/test\_search/](http://localhost:9200/test_search/)' -d '  
> {  
> "mappings" : {  
> "record" : {  
> "properties" : {  
> "object" : {  
> "type" : "string"  
> },  
> "id" : {  
> "type" : "integer"  
> },  
> "name" : {  
> "type" : "string",  
> "boost" : 6  
> },  
> "email" : {  
> "type" : "string",  
> "boost" : 5  
> }  
> }  
> }  
> }
> 
> }  
> '  
> curl -XPUT '[http://localhost:9200/test\_search/record/1](http://localhost:9200/test_search/record/1)' -d '{  
> "object" : "User",  
> "id" : 1,  
> "name" : "john doe",  
> "email" : "[doe@doe.com](mailto:doe@doe.com)"  
> }'  
> curl -XPUT '[http://localhost:9200/test\_search/record/2](http://localhost:9200/test_search/record/2)' -d '{  
> "object" : "User",  
> "id" : 2,  
> "name" : "jane doe",  
> "email" : "[j@d.com](mailto:j@d.com)"  
> }'
> 
> curl -XGET '[http://localhost:9200/test\_search/\_search?pretty=true](http://localhost:9200/test_search/_search?pretty=true)' -d  
> '{"query":{"filtered":{"query":{"queryString":{"query":"doe"}}}},"sort":[{"name":"asc"},"\_score"]}'
> 
> --  
> 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).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/f8fbba45-186a-42b2-86be-970ef65e60a5%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/f8fbba45-186a-42b2-86be-970ef65e60a5%40googlegroups.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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQBePZArZCSFnjUFS25rKtGLkcECA36c%3DJ-wYSxpXXkY\_w%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQBePZArZCSFnjUFS25rKtGLkcECA36c%3DJ-wYSxpXXkY_w%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Nikolay\_Chankov](https://avatars.discourse-cdn.com/v4/letter/n/a8b319/32.png) [@Nikolay\_Chankov](https://discuss.elastic.co/u/Nikolay_Chankov)\
**Post date:** [January 2, 2014, 8:46pm UTC](https://discuss.elastic.co/t/order-by-name-doesnt-work-as-expected/15083/3 "2014-01-02T20:46:43Z")

</div>

Thank you, it's working.

On Thursday, January 2, 2014 4:20:17 PM UTC, Nikolay Chankov wrote:

> Hi guys,
> 
> for some reason, the order by name, \_score is not working as I would  
> expect.  
> I've prepared a simple example to explain what I mean.  
> There are 2 records: john doe and jane doe. if there is no email in the  
> index their score is the same, and the order is correct, jane goes before  
> john, but if john's record has email which contain doe (the search phrase),  
> john \_score is higher and the order is wrong.  
> I've noticed that in the results the "sort" node is [ "doe", 0.6328839  
> ], ["doe", 0.48819983] rather than ["john doe", 0.6328839], [ "jane  
> doe", 0.48819983 ]. if the order is name:desc the search is [ "jane",  
> 0.6328839 ], ["john", 0.48819983]
> 
> This happen when I use query:{...}. If the query is missing the results  
> get the same weight and it is working as expected.
> 
> do I need to make special sort somehow in order to get the desired order,  
> or it's a bug?
> 
> Thanks in advance.
> 
> Here is the script how to see this behavior. I am using 0.90.5 if it does  
> matter (tested 0.90.8 with the same effect). BTW, if the name is without a  
> space e.g. johndoe, janedoe the order is correct.
> 
> curl -XDELETE '[http://localhost:9200/test\_search](http://localhost:9200/test_search)'  
> curl -XPUT '[http://localhost:9200/test\_search/](http://localhost:9200/test_search/)' -d '  
> {  
> "mappings" : {  
> "record" : {  
> "properties" : {  
> "object" : {  
> "type" : "string"  
> },  
> "id" : {  
> "type" : "integer"  
> },  
> "name" : {  
> "type" : "string",  
> "boost" : 6  
> },  
> "email" : {  
> "type" : "string",  
> "boost" : 5  
> }  
> }  
> }  
> }
> 
> }  
> '  
> curl -XPUT '[http://localhost:9200/test\_search/record/1](http://localhost:9200/test_search/record/1)' -d '{  
> "object" : "User",  
> "id" : 1,  
> "name" : "john doe",  
> "email" : "[doe@doe.com](mailto:doe@doe.com)"  
> }'  
> curl -XPUT '[http://localhost:9200/test\_search/record/2](http://localhost:9200/test_search/record/2)' -d '{  
> "object" : "User",  
> "id" : 2,  
> "name" : "jane doe",  
> "email" : "[j@d.com](mailto:j@d.com)"  
> }'
> 
> curl -XGET '[http://localhost:9200/test\_search/\_search?pretty=true](http://localhost:9200/test_search/_search?pretty=true)' -d  
> '{"query":{"filtered":{"query":{"queryString":{"query":"doe"}}}},"sort":[{"name":"asc"},"\_score"]}'

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/ae51e048-42d0-4ad6-a5f3-710063521959%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/ae51e048-42d0-4ad6-a5f3-710063521959%40googlegroups.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, 1:58am UTC](https://discuss.elastic.co/t/order-by-name-doesnt-work-as-expected/15083/4 "2017-07-06T01:58:35Z")

</div>


