# How to sort with standard alphabe

**URL:** https://discuss.elastic.co/t/how-to-sort-with-standard-alphabe/6994
**Category:** Elasticsearch
**Created:** [March 14, 2012, 10:54am UTC](https://discuss.elastic.co/t/how-to-sort-with-standard-alphabe/6994 "2012-03-14T10:54:23Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![inlier\_2](https://avatars.discourse-cdn.com/v4/letter/i/cdc98d/32.png) [@inlier\_2](https://discuss.elastic.co/u/inlier_2)
#### Post date: [March 14, 2012, 10:54am UTC](https://discuss.elastic.co/t/how-to-sort-with-standard-alphabe/6994/1 "2012-03-14T10:54:23Z")

</div>

I need sort order title like

1. "AAA"
2. "aaa"
3. "BBB"
4. "bbb"
5. "CCC"
6. "ccc"  
but in Elastic search when I use this query  
{  
"fields": [  
"title"  
],  
"query": {  
"query\_string": {  
"query": "\*",  
"fields": [  
"title"  
]  
}  
},  
"sort": [  
{  
"title.untouched": "asc" // title is multi\_field  
}  
]  
}  
The result allways
7. "AAA"
8. "BBB"
9. "CCC"
10. "aaa"
11. "bbb"
12. "ccc"  
It's not correct. Help me please.  
I use the analysis  
"settings":{  
"index" : {  
"analysis" : {  
"analyzer" : {  
"index\_analyzer" : {  
"tokenizer" : "keyword",  
"filter" : ["icu\_collation"]  
}  
}  
}  
}  
}

But the order doesn't run like me want.

---

<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: [March 14, 2012, 5:02pm UTC](https://discuss.elastic.co/t/how-to-sort-with-standard-alphabe/6994/2 "2012-03-14T17:02:05Z")

</div>

You can try using script-based sorting, where the value to sort on is  
the lower-cased field.

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

On Wed, Mar 14, 2012 at 3:54 AM, inlier [hiquocthang@gmail.com](mailto:hiquocthang@gmail.com) wrote:

> I need sort order title like
> 
> 1. "AAA"
> 2. "aaa"
> 3. "BBB"
> 4. "bbb"
> 5. "CCC"
> 6. "ccc"  
> but in Elastic search when I use this query  
> {  
> "fields": [  
> "title"  
> ],  
> "query": {  
> "query\_string": {  
> "query": "\*",  
> "fields": [  
> "title"  
> ]  
> }  
> },  
> "sort": [  
> {  
> "title.untouched": "asc" // title is multi\_field  
> }  
> ]  
> }  
> The result allways
> 7. "AAA"
> 8. "BBB"
> 9. "CCC"
> 10. "aaa"
> 11. "bbb"
> 12. "ccc"  
> It's not correct. Help me please.  
> I use the analysis  
> "settings":{  
> "index" : {  
> "analysis" : {  
> "analyzer" : {  
> "index\_analyzer" : {  
> "tokenizer" : "keyword",  
> "filter" : ["icu\_collation"]  
> }  
> }  
> }  
> }  
> }
> 
> But the order doesn't run like me want.

---

<div class="post-metadata">

### Author: ![inlier\_2](https://avatars.discourse-cdn.com/v4/letter/i/cdc98d/32.png) [@inlier\_2](https://discuss.elastic.co/u/inlier_2)
#### Post date: [March 15, 2012, 2:26am UTC](https://discuss.elastic.co/t/how-to-sort-with-standard-alphabe/6994/3 "2012-03-15T02:26:56Z")

</div>

Please explain more. I TRY with this query but it can't run, I think  
because doc['title'].value is not number.  
{  
"fields": [  
"title"  
],  
"query": {  
"query\_string": {  
"query": "\*",  
"default\_operator": "AND",  
"fields": [  
"title"  
]  
}  
},  
"sort": {  
"\_script": {  
"script": "doc['title'].value \* factor",  
"type": "number",  
"params": {  
"factor": 1.1  
}  
}  
}  
}

On Mar 15, 12:02 am, Ivan Brusic [i...@brusic.com](mailto:i...@brusic.com) wrote:

> You can try using script-based sorting, where the value to sort on is  
> the lower-cased field.
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/sort.html)
> 
> On Wed, Mar 14, 2012 at 3:54 AM, inlier [hiquocth...@gmail.com](mailto:hiquocth...@gmail.com) wrote:
> 
> > I need sort order title like
> > 
> > 1. "AAA"
> > 2. "aaa"
> > 3. "BBB"
> > 4. "bbb"
> > 5. "CCC"
> > 6. "ccc"  
> > but in Elastic search when I use this query  
> > {  
> > "fields": [  
> > "title"  
> > ],  
> > "query": {  
> > "query\_string": {  
> > "query": "\*",  
> > "fields": [  
> > "title"  
> > ]  
> > }  
> > },  
> > "sort": [  
> > {  
> > "title.untouched": "asc" // title is multi\_field  
> > }  
> > ]  
> > }  
> > The result allways
> > 7. "AAA"
> > 8. "BBB"
> > 9. "CCC"
> > 10. "aaa"
> > 11. "bbb"
> > 12. "ccc"  
> > It's not correct. Help me please.  
> > I use the analysis  
> > "settings":{  
> > "index" : {  
> > "analysis" : {  
> > "analyzer" : {  
> > "index\_analyzer" : {  
> > "tokenizer" : "keyword",  
> > "filter" : ["icu\_collation"]  
> > }  
> > }  
> > }  
> > }  
> > }
> 
> > But the order doesn't run like me want.

---

<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: [March 16, 2012, 5:29pm UTC](https://discuss.elastic.co/t/how-to-sort-with-standard-alphabe/6994/4 "2012-03-16T17:29:10Z")

</div>

Perhaps I was premature in suggesting scripts given that I have never  
used them for sorting. I assumed they worked like facet scripts, where  
they return the value to be used, but it appears that sorting scripts  
return a numerical value to be sorted on.

I wonder would it be possible to have script like:  
"sort": {  
"\_script": {  
"script": "doc['title'].value.toLowerCase",  
"type": "string",  
}  
}

Not sure if "string" is a supported type.

On Wed, Mar 14, 2012 at 7:26 PM, inlier [hiquocthang@gmail.com](mailto:hiquocthang@gmail.com) wrote:

> Please explain more. I TRY with this query but it can't run, I think  
> because doc['title'].value is not number.  
> {  
> "fields": [  
> "title"  
> ],  
> "query": {  
> "query\_string": {  
> "query": "\*",  
> "default\_operator": "AND",  
> "fields": [  
> "title"  
> ]  
> }  
> },  
> "sort": {  
> "\_script": {  
> "script": "doc['title'].value \* factor",  
> "type": "number",  
> "params": {  
> "factor": 1.1  
> }  
> }  
> }  
> }
> 
> On Mar 15, 12:02 am, Ivan Brusic [i...@brusic.com](mailto:i...@brusic.com) wrote:
> 
> > You can try using script-based sorting, where the value to sort on is  
> > the lower-cased field.
> > 
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/sort.html)
> > 
> > On Wed, Mar 14, 2012 at 3:54 AM, inlier [hiquocth...@gmail.com](mailto:hiquocth...@gmail.com) wrote:
> > 
> > > I need sort order title like
> > > 
> > > 1. "AAA"
> > > 2. "aaa"
> > > 3. "BBB"
> > > 4. "bbb"
> > > 5. "CCC"
> > > 6. "ccc"  
> > > but in Elastic search when I use this query  
> > > {  
> > > "fields": [  
> > > "title"  
> > > ],  
> > > "query": {  
> > > "query\_string": {  
> > > "query": "\*",  
> > > "fields": [  
> > > "title"  
> > > ]  
> > > }  
> > > },  
> > > "sort": [  
> > > {  
> > > "title.untouched": "asc" // title is multi\_field  
> > > }  
> > > ]  
> > > }  
> > > The result allways
> > > 7. "AAA"
> > > 8. "BBB"
> > > 9. "CCC"
> > > 10. "aaa"
> > > 11. "bbb"
> > > 12. "ccc"  
> > > It's not correct. Help me please.  
> > > I use the analysis  
> > > "settings":{  
> > > "index" : {  
> > > "analysis" : {  
> > > "analyzer" : {  
> > > "index\_analyzer" : {  
> > > "tokenizer" : "keyword",  
> > > "filter" : ["icu\_collation"]  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }
> > 
> > > But the order doesn't run like me want.

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [March 17, 2012, 10:40am UTC](https://discuss.elastic.co/t/how-to-sort-with-standard-alphabe/6994/5 "2012-03-17T10:40:42Z")

</div>

Another option is to have the title indexed as multi field, once with a  
keyword tokenizer and lowercase, and once just with keyword tokenizer (so  
it retains the casing). Sorting can be done on the lowercase one, and  
fetching the relevant actual non lowercased field can be done by simply  
asking for it from results.

On Fri, Mar 16, 2012 at 7:29 PM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:

> Perhaps I was premature in suggesting scripts given that I have never  
> used them for sorting. I assumed they worked like facet scripts, where  
> they return the value to be used, but it appears that sorting scripts  
> return a numerical value to be sorted on.
> 
> I wonder would it be possible to have script like:  
> "sort": {  
> "\_script": {  
> "script": "doc['title'].value.toLowerCase",  
> "type": "string",  
> }  
> }
> 
> Not sure if "string" is a supported type.
> 
> On Wed, Mar 14, 2012 at 7:26 PM, inlier [hiquocthang@gmail.com](mailto:hiquocthang@gmail.com) wrote:
> 
> > Please explain more. I TRY with this query but it can't run, I think  
> > because doc['title'].value is not number.  
> > {  
> > "fields": [  
> > "title"  
> > ],  
> > "query": {  
> > "query\_string": {  
> > "query": "\*",  
> > "default\_operator": "AND",  
> > "fields": [  
> > "title"  
> > ]  
> > }  
> > },  
> > "sort": {  
> > "\_script": {  
> > "script": "doc['title'].value \* factor",  
> > "type": "number",  
> > "params": {  
> > "factor": 1.1  
> > }  
> > }  
> > }  
> > }
> > 
> > On Mar 15, 12:02 am, Ivan Brusic [i...@brusic.com](mailto:i...@brusic.com) wrote:
> > 
> > > You can try using script-based sorting, where the value to sort on is  
> > > the lower-cased field.
> > > 
> > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/sort.html)
> > > 
> > > On Wed, Mar 14, 2012 at 3:54 AM, inlier [hiquocth...@gmail.com](mailto:hiquocth...@gmail.com) wrote:
> > > 
> > > > I need sort order title like
> > > > 
> > > > 1. "AAA"
> > > > 2. "aaa"
> > > > 3. "BBB"
> > > > 4. "bbb"
> > > > 5. "CCC"
> > > > 6. "ccc"  
> > > > but in Elastic search when I use this query  
> > > > {  
> > > > "fields": [  
> > > > "title"  
> > > > ],  
> > > > "query": {  
> > > > "query\_string": {  
> > > > "query": "\*",  
> > > > "fields": [  
> > > > "title"  
> > > > ]  
> > > > }  
> > > > },  
> > > > "sort": [  
> > > > {  
> > > > "title.untouched": "asc" // title is multi\_field  
> > > > }  
> > > > ]  
> > > > }  
> > > > The result allways
> > > > 7. "AAA"
> > > > 8. "BBB"
> > > > 9. "CCC"
> > > > 10. "aaa"
> > > > 11. "bbb"
> > > > 12. "ccc"  
> > > > It's not correct. Help me please.  
> > > > I use the analysis  
> > > > "settings":{  
> > > > "index" : {  
> > > > "analysis" : {  
> > > > "analyzer" : {  
> > > > "index\_analyzer" : {  
> > > > "tokenizer" : "keyword",  
> > > > "filter" : ["icu\_collation"]  
> > > > }  
> > > > }  
> > > > }  
> > > > }  
> > > > }
> > > 
> > > > But the order doesn't run like me want.

---

<div class="post-metadata">

### Author: ![inlier\_2](https://avatars.discourse-cdn.com/v4/letter/i/cdc98d/32.png) [@inlier\_2](https://discuss.elastic.co/u/inlier_2)
#### Post date: [March 19, 2012, 7:17am UTC](https://discuss.elastic.co/t/how-to-sort-with-standard-alphabe/6994/6 "2012-03-19T07:17:53Z")

</div>

"analyzer" : {  
"index\_analyzer" : {  
"tokenizer" : "keyword",  
"filter" : ["lowercase"]  
},  
"sort\_analyzer" : {  
"tokenizer" : "keyword"

```
                }
            }

```

I set multi field "title" for index\_analyser and "title.untouched" for  
"sort\_analyzer" but this setting don't run like I want. It sorts by  
aaa  
AAA  
bbb  
BBB  
( i don't want this).

---

<div class="post-metadata">

### Author: ![inlier\_2](https://avatars.discourse-cdn.com/v4/letter/i/cdc98d/32.png) [@inlier\_2](https://discuss.elastic.co/u/inlier_2)
#### Post date: [March 19, 2012, 7:21am UTC](https://discuss.elastic.co/t/how-to-sort-with-standard-alphabe/6994/7 "2012-03-19T07:21:01Z")

</div>

To Ivan Brusic, thanks for helping me. I like your way, I can get more  
when see your code, but it not run good for me.

---

<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:35am UTC](https://discuss.elastic.co/t/how-to-sort-with-standard-alphabe/6994/8 "2017-07-06T03:35:38Z")

</div>


