# Strange behvaiour when sorting a field that contains a special char

**URL:** <https://discuss.elastic.co/t/strange-behvaiour-when-sorting-a-field-that-contains-a-special-char/8782>\
**Category:** Elasticsearch\
**Created:** [August 18, 2012, 7:18pm UTC](https://discuss.elastic.co/t/strange-behvaiour-when-sorting-a-field-that-contains-a-special-char/8782 "2012-08-18T19:18:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roman\_Kournjaev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roman_kournjaev/32/2552_2.png) [@Roman\_Kournjaev](https://discuss.elastic.co/u/Roman_Kournjaev)\
**Post date:** [August 18, 2012, 7:18pm UTC](https://discuss.elastic.co/t/strange-behvaiour-when-sorting-a-field-that-contains-a-special-char/8782/1 "2012-08-18T19:18:28Z")

</div>

Hi

I am experiencing somewhat strange and unpredicted behaviour sorting  
documents upon name.

For instance , my documents have the following names :

- Craftsman
- Arroyo Craftsman
- Guild of Master Craftsman Publications Ltd
- Craftsman House
- _[craftsman.com](http://craftsman.com)_
- Craftsman Evolv

The query is :

```
   QueryBuilder queryBuilder = textPhrasePrefixQuery("name", query).

```

boost(1.0f);  
SearchResponse response = client.prepareSearch("tags").setTypes(  
"tag")  
.setQuery(queryBuilder)  
.setFrom(0).setSize(100).setExplain(true)//.addSort("name.sort",  
SortOrder.DESC)  
.execute()  
.actionGet();

The document with the name : \*[craftsman.com](http://craftsman.com) \*is the first when sorting DESC  
and last when sorting ASC , which is clearly something that i did not  
expect.  
Did someone expirience something similar like that ?

I also wanted to debug ES to see what the problem is , but struggle to find  
the entry poin tto put my breakpoint to. Do you know a good point where to  
start ?

Thanks  
Roman

--

---

<div class="post-metadata">

**Author:** ![Roman\_Kournjaev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roman_kournjaev/32/2552_2.png) [@Roman\_Kournjaev](https://discuss.elastic.co/u/Roman_Kournjaev)\
**Post date:** [August 18, 2012, 8:26pm UTC](https://discuss.elastic.co/t/strange-behvaiour-when-sorting-a-field-that-contains-a-special-char/8782/2 "2012-08-18T20:26:57Z")

</div>

Nevermind we found out that sorting is case sensitive by default , so that  
the issue

On Saturday, August 18, 2012 10:18:28 PM UTC+3, Roman Kournjaev wrote:

> Hi
> 
> I am experiencing somewhat strange and unpredicted behaviour sorting  
> documents upon name.
> 
> For instance , my documents have the following names :
> 
> - Craftsman
> - Arroyo Craftsman
> - Guild of Master Craftsman Publications Ltd
> - Craftsman House
> - _[craftsman.com](http://craftsman.com)_
> - Craftsman Evolv
> 
> The query is :
> 
> ```
> QueryBuilder queryBuilder = textPhrasePrefixQuery("name", query).
> 
> ```
> 
> boost(1.0f);  
> SearchResponse response = client.prepareSearch("tags").setTypes(  
> "tag")  
> .setQuery(queryBuilder)  
> .setFrom(0).setSize(100).setExplain(true)//.addSort("name.sort",  
> SortOrder.DESC)  
> .execute()  
> .actionGet();
> 
> The document with the name : \*[craftsman.com](http://craftsman.com) \*is the first when sorting  
> DESC and last when sorting ASC , which is clearly something that i did not  
> expect.  
> Did someone expirience something similar like that ?
> 
> I also wanted to debug ES to see what the problem is , but struggle to  
> find the entry poin tto put my breakpoint to. Do you know a good point  
> where to start ?
> 
> Thanks  
> Roman

--

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [August 18, 2012, 8:46pm UTC](https://discuss.elastic.co/t/strange-behvaiour-when-sorting-a-field-that-contains-a-special-char/8782/3 "2012-08-18T20:46:47Z")

</div>

It could be relative to your mapping. If you use default mapping, your name is break into 2 tokens: craftsman and com.

HTH

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 18 août 2012 à 21:18, Roman Kournjaev [kournjaev@gmail.com](mailto:kournjaev@gmail.com) a écrit :

Hi

I am experiencing somewhat strange and unpredicted behaviour sorting documents upon name.

For instance , my documents have the following names :  
Craftsman  
Arroyo Craftsman  
Guild of Master Craftsman Publications Ltd  
Craftsman House

> **[Craftsman Tools, Storage, Lawn & Garden Equipment](https://www.craftsman.com/)**
>
> Over 90 Years of Quality and Innovation. With Stanley Black & Decker, the Craftsman legacy continues.

  
Craftsman Evolv  
The query is :

```
   QueryBuilder queryBuilder = textPhrasePrefixQuery("name", query).boost(1.0f);
    SearchResponse response = client.prepareSearch("tags").setTypes("tag")
            .setQuery(queryBuilder)
            .setFrom(0).setSize(100).setExplain(true)//.addSort("name.sort", SortOrder.DESC)
            .execute()
            .actionGet();

```

The document with the name : [craftsman.com](http://craftsman.com) is the first when sorting DESC and last when sorting ASC , which is clearly something that i did not expect.  
Did someone expirience something similar like that ?

I also wanted to debug ES to see what the problem is , but struggle to find the entry poin tto put my breakpoint to. Do you know a good point where to start ?

Thanks  
Roman

--

--

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [August 18, 2012, 8:47pm UTC](https://discuss.elastic.co/t/strange-behvaiour-when-sorting-a-field-that-contains-a-special-char/8782/4 "2012-08-18T20:47:31Z")

</div>

Sorry. I answered too late.

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 18 août 2012 à 22:46, David Pilato [david@pilato.fr](mailto:david@pilato.fr) a écrit :

It could be relative to your mapping. If you use default mapping, your name is break into 2 tokens: craftsman and com.

HTH

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 18 août 2012 à 21:18, Roman Kournjaev [kournjaev@gmail.com](mailto:kournjaev@gmail.com) a écrit :

Hi

I am experiencing somewhat strange and unpredicted behaviour sorting documents upon name.

For instance , my documents have the following names :  
Craftsman  
Arroyo Craftsman  
Guild of Master Craftsman Publications Ltd  
Craftsman House

> **[Craftsman Tools, Storage, Lawn & Garden Equipment](https://www.craftsman.com/)**
>
> Over 90 Years of Quality and Innovation. With Stanley Black & Decker, the Craftsman legacy continues.

  
Craftsman Evolv  
The query is :

```
   QueryBuilder queryBuilder = textPhrasePrefixQuery("name", query).boost(1.0f);
    SearchResponse response = client.prepareSearch("tags").setTypes("tag")
            .setQuery(queryBuilder)
            .setFrom(0).setSize(100).setExplain(true)//.addSort("name.sort", SortOrder.DESC)
            .execute()
            .actionGet();

```

The document with the name : [craftsman.com](http://craftsman.com) is the first when sorting DESC and last when sorting ASC , which is clearly something that i did not expect.  
Did someone expirience something similar like that ?

I also wanted to debug ES to see what the problem is , but struggle to find the entry poin tto put my breakpoint to. Do you know a good point where to start ?

Thanks  
Roman

--

--

--

---

<div class="post-metadata">

**Author:** ![rickcrawford](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rickcrawford/32/2760_2.png) [@rickcrawford](https://discuss.elastic.co/u/rickcrawford)\
**Post date:** [August 19, 2012, 4:21am UTC](https://discuss.elastic.co/t/strange-behvaiour-when-sorting-a-field-that-contains-a-special-char/8782/5 "2012-08-19T04:21:40Z")

</div>

Sorting follows unicode byte ordering for strings so you want to make sure  
to use numerics for sorting when possible, or use a keyword filter +  
lowercase to normalize your strings as much as possible.

On Saturday, August 18, 2012 12:18:28 PM UTC-7, Roman Kournjaev wrote:

> Hi
> 
> I am experiencing somewhat strange and unpredicted behaviour sorting  
> documents upon name.
> 
> For instance , my documents have the following names :
> 
> - Craftsman
> - Arroyo Craftsman
> - Guild of Master Craftsman Publications Ltd
> - Craftsman House
> - _[craftsman.com](http://craftsman.com)_
> - Craftsman Evolv
> 
> The query is :
> 
> ```
> QueryBuilder queryBuilder = textPhrasePrefixQuery("name", query).
> 
> ```
> 
> boost(1.0f);  
> SearchResponse response = client.prepareSearch("tags").setTypes(  
> "tag")  
> .setQuery(queryBuilder)  
> .setFrom(0).setSize(100).setExplain(true)//.addSort("name.sort",  
> SortOrder.DESC)  
> .execute()  
> .actionGet();
> 
> The document with the name : \*[craftsman.com](http://craftsman.com) \*is the first when sorting  
> DESC and last when sorting ASC , which is clearly something that i did not  
> expect.  
> Did someone expirience something similar like that ?
> 
> I also wanted to debug ES to see what the problem is , but struggle to  
> find the entry poin tto put my breakpoint to. Do you know a good point  
> where to start ?
> 
> Thanks  
> Roman

--

---

<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:15am UTC](https://discuss.elastic.co/t/strange-behvaiour-when-sorting-a-field-that-contains-a-special-char/8782/6 "2017-07-06T03:15:53Z")

</div>


