# Sorting of fields with special charecters

**URL:** <https://discuss.elastic.co/t/sorting-of-fields-with-special-charecters/115481>\
**Category:** Elasticsearch\
**Created:** [January 15, 2018, 6:45am UTC](https://discuss.elastic.co/t/sorting-of-fields-with-special-charecters/115481 "2018-01-15T06:45:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![SawyerFord](https://avatars.discourse-cdn.com/v4/letter/s/91b2a8/32.png) [@SawyerFord](https://discuss.elastic.co/u/SawyerFord)\
**Post date:** [January 15, 2018, 6:45am UTC](https://discuss.elastic.co/t/sorting-of-fields-with-special-charecters/115481/1 "2018-01-15T06:45:39Z")

</div>

Currently we have used the elastic sort sorting capability in our Java application for sorting or a field called description, but the sort does not return a correct order.

Our field consists of special charecters, (#,$,%,&, and so on)...

What changes should we make, we have added the below code for sorting

```auto
 nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort(ElasticsearchFieldConstant.ELASTIC_BROADCAST_DESCRIPTION)
                            .order("Description");
                    return nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort("_score").order(SortOrder.ASC))
                            .build();

```

---

<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:** [January 15, 2018, 7:28am UTC](https://discuss.elastic.co/t/sorting-of-fields-with-special-charecters/115481/2 "2018-01-15T07:28:44Z")

</div>

Please format your code using `</>` icon as explained in [this guide](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will make your post more readable.

Or use markdown style like:

````
```
CODE
```

````

> but the sort does not return a correct order.

What does it return? Why do you think it's not the correct order? What would you expect?

Could you provide a full recreation script as described in

> [@About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21):
>
> The heart of the free and open Elastic Stack Elasticsearch is a distributed, RESTful search and analytics engine capable of addressing a growing number of use cases. As the heart of the Elastic Stack, it centrally stores your data for lightning fast search, fine‑tuned relevancy, and powerful analytics that scale with ease. warning PLEASE READ THIS SECTION IF IT'S YOUR FIRST POST Some useful links: [elasticsearch reference guide](http://www.elastic.co/guide/en/elasticsearch/reference/current/index.html)[elasticsearch user guide](http://www.elastic.co/guide/en/elasticsearch/guide/current/index.html)[elasticsearch plugins](https://www.elastic.co/guide/en/elasticsearch/plugins/current/index.html)[elasticsearch cl…](https://www.elastic.co/guide/en/elasticsearch/client/index.html)

It will help to better understand what you are doing.  
Please, try to keep the example as simple as possible.

Anyway, it's often a question of analyzers. You can analyze the same text for different purposes with subfields (aka multifields) and use one method for searching and the other for sorting.

---

<div class="post-metadata">

**Author:** ![SawyerFord](https://avatars.discourse-cdn.com/v4/letter/s/91b2a8/32.png) [@SawyerFord](https://discuss.elastic.co/u/SawyerFord)\
**Post date:** [January 15, 2018, 8:38am UTC](https://discuss.elastic.co/t/sorting-of-fields-with-special-charecters/115481/3 "2018-01-15T08:38:45Z")

</div>

As expected in the sense, the sorting should be with order for the following example

If the list of description are

```auto
{#Hirekrekre, $fmsdklfmsklfs, Hi I am fine, Hi $190 for you, subscribe me on #hash tag}

```

Then I like to know what will be the sorted order returned by the current default elastic search sorting , if the list of description are above.

If it does implement asciifolding by default, then is the sorting order I am recieving for strings with special charecters is fine?

---

<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:** [January 15, 2018, 9:09am UTC](https://discuss.elastic.co/t/sorting-of-fields-with-special-charecters/115481/4 "2018-01-15T09:09:08Z")

</div>

> Then I like to know what will be the sorted order returned by the current default Elasticsearch sorting , if the list of description are above.

Just try it and you will get the answer.

Again, if you need more help please provide a full recreation script as I mentioned before. That will help.

---

<div class="post-metadata">

**Author:** ![SawyerFord](https://avatars.discourse-cdn.com/v4/letter/s/91b2a8/32.png) [@SawyerFord](https://discuss.elastic.co/u/SawyerFord)\
**Post date:** [January 15, 2018, 9:28am UTC](https://discuss.elastic.co/t/sorting-of-fields-with-special-charecters/115481/5 "2018-01-15T09:28:26Z")

</div>

Actually the current problem is on the Production environment that we are facing, We are not getting the sorting results of the description as per the sorting order(ASC/DESC).

Basically the description contains alphabets, special charecters, white spaces, numbers and everything

And we have been trying sorting by description with the above code, which is returning large list of strings which are in a order.

**Now I would like to know whether elastic search provides the sorting order(handles the special charecters sorting, numbers inside it everything inside it) by default or should we customize the script**

As we are on production and the functionality has gone long ago, I am not able to get the script.

---

<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:** [January 15, 2018, 10:01am UTC](https://discuss.elastic.co/t/sorting-of-fields-with-special-charecters/115481/6 "2018-01-15T10:01:31Z")

</div>

But you can hopefully start a local test instance and try that with your mapping and settings, can't you?

---

<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:** [February 12, 2018, 10:01am UTC](https://discuss.elastic.co/t/sorting-of-fields-with-special-charecters/115481/7 "2018-02-12T10:01:40Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
