# Case Insensitive Sort

**URL:** https://discuss.elastic.co/t/case-insensitive-sort/8647
**Category:** Elasticsearch
**Created:** [August 6, 2012, 2:52pm UTC](https://discuss.elastic.co/t/case-insensitive-sort/8647 "2012-08-06T14:52:04Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Michael\_Caplan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_caplan/32/2782_2.png) [@Michael\_Caplan](https://discuss.elastic.co/u/Michael_Caplan)
#### Post date: [August 6, 2012, 2:52pm UTC](https://discuss.elastic.co/t/case-insensitive-sort/8647/1 "2012-08-06T14:52:04Z")

</div>

Hello,

I've been working through implementing a case insensitive sort on a string  
field. My initial problem was the inability to sort on an analysed field  
as discussed here:

[http://blog.wiercinski.net/2011/uncategorized/elasticsearch-sorting-on-string-types-with-more-than-one-value-per-doc-or-more-than-one-token-per-field/](http://blog.wiercinski.net/2011/uncategorized/elasticsearch-sorting-on-string-types-with-more-than-one-value-per-doc-or-more-than-one-token-per-field/)

With the sort field in question now defined as a multi\_field, I'm in  
business:

"last\_name": {  
"type": "multi\_field",  
"fields": {  
"last\_name": {  
"type": "string",  
"boost": 2,  
"analyzer": "name"  
},  
"untouched": {  
"type": "string",  
"index": "not\_analyzed",  
"include\_in\_all": false  
}  
}  
}

However, when sorting on last\_name.untouched, the sort applied is case  
sensitive. I seem to be missing out on a solution that seem to be hinted  
at here:

[http://elasticsearch-users.115913.n3.nabble.com/multi-field-and-sort-td3548822.html](http://elasticsearch-users.115913.n3.nabble.com/multi-field-and-sort-td3548822.html)  
[http://elasticsearch-users.115913.n3.nabble.com/Case-insensitive-sort-td843856.html](http://elasticsearch-users.115913.n3.nabble.com/Case-insensitive-sort-td843856.html)

Is the approach taken with the multi\_field wrong, or is there another layer  
to solving this problem that I am missing?

Thanks!

Mike

---

<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: [August 6, 2012, 3:03pm UTC](https://discuss.elastic.co/t/case-insensitive-sort/8647/2 "2012-08-06T15:03:58Z")

</div>

Hi Mike,

The second solution seems to be the obvious solution to the issue.  
Using a keyword tokenizer will only create one token for the entire  
field, adhering to the "one-token-per-field" requirement. Other  
tokenizers will create one or more (synonyms) tokens per word (except  
stopwords). Applying a lower-case filter will provide case  
insensitivity, keeping in mind that the same analyzer must be used for  
both indexing and querying.

Ivan

On Mon, Aug 6, 2012 at 7:52 AM, Michael Caplan [michael@eggplant.ws](mailto:michael@eggplant.ws) wrote:

> Hello,
> 
> I've been working through implementing a case insensitive sort on a string  
> field. My initial problem was the inability to sort on an analysed field as  
> discussed here:
> 
> [http://blog.wiercinski.net/2011/uncategorized/elasticsearch-sorting-on-string-types-with-more-than-one-value-per-doc-or-more-than-one-token-per-field/](http://blog.wiercinski.net/2011/uncategorized/elasticsearch-sorting-on-string-types-with-more-than-one-value-per-doc-or-more-than-one-token-per-field/)
> 
> With the sort field in question now defined as a multi\_field, I'm in  
> business:
> 
> "last\_name": {  
> "type": "multi\_field",  
> "fields": {  
> "last\_name": {  
> "type": "string",  
> "boost": 2,  
> "analyzer": "name"  
> },  
> "untouched": {  
> "type": "string",  
> "index": "not\_analyzed",  
> "include\_in\_all": false  
> }  
> }  
> }
> 
> However, when sorting on last\_name.untouched, the sort applied is case  
> sensitive. I seem to be missing out on a solution that seem to be hinted at  
> here:
> 
> [http://elasticsearch-users.115913.n3.nabble.com/multi-field-and-sort-td3548822.html](http://elasticsearch-users.115913.n3.nabble.com/multi-field-and-sort-td3548822.html)  
> [http://elasticsearch-users.115913.n3.nabble.com/Case-insensitive-sort-td843856.html](http://elasticsearch-users.115913.n3.nabble.com/Case-insensitive-sort-td843856.html)
> 
> Is the approach taken with the multi\_field wrong, or is there another layer  
> to solving this problem that I am missing?
> 
> Thanks!
> 
> Mike

---

<div class="post-metadata">

### Author: ![Michael\_Caplan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_caplan/32/2782_2.png) [@Michael\_Caplan](https://discuss.elastic.co/u/Michael_Caplan)
#### Post date: [August 6, 2012, 10:24pm UTC](https://discuss.elastic.co/t/case-insensitive-sort/8647/3 "2012-08-06T22:24:43Z")

</div>

Hi Ivan,

Just to make sure I have this right, to fulfill sorting requirements any  
tokenizer used on string fields need to produce one token and the keyword  
tokenizer meets this requirement. Besides the keyword tokenizer, am I  
correct with my understanding that there are no other tokenizers that meet  
this requirement?

Thanks!

Mike

On Monday, August 6, 2012 12:03:58 PM UTC-3, Ivan Brusic wrote:

> Hi Mike,
> 
> The second solution seems to be the obvious solution to the issue.  
> Using a keyword tokenizer will only create one token for the entire  
> field, adhering to the "one-token-per-field" requirement. Other  
> tokenizers will create one or more (synonyms) tokens per word (except  
> stopwords). Applying a lower-case filter will provide case  
> insensitivity, keeping in mind that the same analyzer must be used for  
> both indexing and querying.
> 
> Ivan
> 
> On Mon, Aug 6, 2012 at 7:52 AM, Michael Caplan \<\> wrote:
> 
> > Hello,
> > 
> > I've been working through implementing a case insensitive sort on a  
> > string  
> > field. My initial problem was the inability to sort on an analysed  
> > field as  
> > discussed here:
> 
> [http://blog.wiercinski.net/2011/uncategorized/elasticsearch-sorting-on-string-types-with-more-than-one-value-per-doc-or-more-than-one-token-per-field/](http://blog.wiercinski.net/2011/uncategorized/elasticsearch-sorting-on-string-types-with-more-than-one-value-per-doc-or-more-than-one-token-per-field/)
> 
> > With the sort field in question now defined as a multi\_field, I'm in  
> > business:
> > 
> > "last\_name": {  
> > "type": "multi\_field",  
> > "fields": {  
> > "last\_name": {  
> > "type": "string",  
> > "boost": 2,  
> > "analyzer": "name"  
> > },  
> > "untouched": {  
> > "type": "string",  
> > "index": "not\_analyzed",  
> > "include\_in\_all": false  
> > }  
> > }  
> > }
> > 
> > However, when sorting on last\_name.untouched, the sort applied is case  
> > sensitive. I seem to be missing out on a solution that seem to be  
> > hinted at  
> > here:
> 
> [http://elasticsearch-users.115913.n3.nabble.com/multi-field-and-sort-td3548822.html](http://elasticsearch-users.115913.n3.nabble.com/multi-field-and-sort-td3548822.html)
> 
> > 
> 
> [http://elasticsearch-users.115913.n3.nabble.com/Case-insensitive-sort-td843856.html](http://elasticsearch-users.115913.n3.nabble.com/Case-insensitive-sort-td843856.html)
> 
> > Is the approach taken with the multi\_field wrong, or is there another  
> > layer  
> > to solving this problem that I am missing?
> > 
> > Thanks!
> > 
> > Mike

---

<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: [August 6, 2012, 11:53pm UTC](https://discuss.elastic.co/t/case-insensitive-sort/8647/4 "2012-08-06T23:53:23Z")

</div>

Correct. Only the keyword tokenizer (to my knowledge) can guarantee  
one token per term. Of course, other analyzers could create only one  
token such as the whitespace token and terms with no whitespace (state  
abbreviations, zip codes, etc..).

--  
Ivan

On Mon, Aug 6, 2012 at 3:24 PM, Michael Caplan [michael@eggplant.ws](mailto:michael@eggplant.ws) wrote:

> Hi Ivan,
> 
> Just to make sure I have this right, to fulfill sorting requirements any  
> tokenizer used on string fields need to produce one token and the keyword  
> tokenizer meets this requirement. Besides the keyword tokenizer, am I  
> correct with my understanding that there are no other tokenizers that meet  
> this requirement?
> 
> Thanks!
> 
> Mike
> 
> On Monday, August 6, 2012 12:03:58 PM UTC-3, Ivan Brusic wrote:
> 
> > Hi Mike,
> > 
> > The second solution seems to be the obvious solution to the issue.  
> > Using a keyword tokenizer will only create one token for the entire  
> > field, adhering to the "one-token-per-field" requirement. Other  
> > tokenizers will create one or more (synonyms) tokens per word (except  
> > stopwords). Applying a lower-case filter will provide case  
> > insensitivity, keeping in mind that the same analyzer must be used for  
> > both indexing and querying.
> > 
> > Ivan
> > 
> > On Mon, Aug 6, 2012 at 7:52 AM, Michael Caplan \<\> wrote:
> > 
> > > Hello,
> > > 
> > > I've been working through implementing a case insensitive sort on a  
> > > string  
> > > field. My initial problem was the inability to sort on an analysed  
> > > field as  
> > > discussed here:
> > > 
> > > [http://blog.wiercinski.net/2011/uncategorized/elasticsearch-sorting-on-string-types-with-more-than-one-value-per-doc-or-more-than-one-token-per-field/](http://blog.wiercinski.net/2011/uncategorized/elasticsearch-sorting-on-string-types-with-more-than-one-value-per-doc-or-more-than-one-token-per-field/)
> > > 
> > > With the sort field in question now defined as a multi\_field, I'm in  
> > > business:
> > > 
> > > "last\_name": {  
> > > "type": "multi\_field",  
> > > "fields": {  
> > > "last\_name": {  
> > > "type": "string",  
> > > "boost": 2,  
> > > "analyzer": "name"  
> > > },  
> > > "untouched": {  
> > > "type": "string",  
> > > "index": "not\_analyzed",  
> > > "include\_in\_all": false  
> > > }  
> > > }  
> > > }
> > > 
> > > However, when sorting on last\_name.untouched, the sort applied is case  
> > > sensitive. I seem to be missing out on a solution that seem to be  
> > > hinted at  
> > > here:
> > > 
> > > [http://elasticsearch-users.115913.n3.nabble.com/multi-field-and-sort-td3548822.html](http://elasticsearch-users.115913.n3.nabble.com/multi-field-and-sort-td3548822.html)
> > > 
> > > [http://elasticsearch-users.115913.n3.nabble.com/Case-insensitive-sort-td843856.html](http://elasticsearch-users.115913.n3.nabble.com/Case-insensitive-sort-td843856.html)
> > > 
> > > Is the approach taken with the multi\_field wrong, or is there another  
> > > layer  
> > > to solving this problem that I am missing?
> > > 
> > > Thanks!
> > > 
> > > Mike

---

<div class="post-metadata">

### Author: ![vkareh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vkareh/32/2776_2.png) [@vkareh](https://discuss.elastic.co/u/vkareh)
#### Post date: [August 10, 2012, 5:44pm UTC](https://discuss.elastic.co/t/case-insensitive-sort/8647/5 "2012-08-10T17:44:31Z")

</div>

I had a hard time using with this as well, as I needed to display  
properly-capitalized words, but sort them in a case-insensitive fashion.  
One solution that worked for me was to have the fields indexes as  
"not\_analyzed", then use this in my query:

{  
"query": {...},  
"sort": [  
{"\_script": {  
"script": "doc['field\_name\_to\_sort\_by'].value.toLowerCase()",  
"type": "string",  
"order": "asc"  
}}  
]  
}

On Monday, August 6, 2012 10:52:04 AM UTC-4, Michael Caplan wrote:

> Hello,
> 
> I've been working through implementing a case insensitive sort on a string  
> field. My initial problem was the inability to sort on an analysed field  
> as discussed here:
> 
> [http://blog.wiercinski.net/2011/uncategorized/elasticsearch-sorting-on-string-types-with-more-than-one-value-per-doc-or-more-than-one-token-per-field/](http://blog.wiercinski.net/2011/uncategorized/elasticsearch-sorting-on-string-types-with-more-than-one-value-per-doc-or-more-than-one-token-per-field/)
> 
> With the sort field in question now defined as a multi\_field, I'm in  
> business:
> 
> "last\_name": {  
> "type": "multi\_field",  
> "fields": {  
> "last\_name": {  
> "type": "string",  
> "boost": 2,  
> "analyzer": "name"  
> },  
> "untouched": {  
> "type": "string",  
> "index": "not\_analyzed",  
> "include\_in\_all": false  
> }  
> }  
> }
> 
> However, when sorting on last\_name.untouched, the sort applied is case  
> sensitive. I seem to be missing out on a solution that seem to be hinted  
> at here:
> 
> [http://elasticsearch-users.115913.n3.nabble.com/multi-field-and-sort-td3548822.html](http://elasticsearch-users.115913.n3.nabble.com/multi-field-and-sort-td3548822.html)
> 
> [http://elasticsearch-users.115913.n3.nabble.com/Case-insensitive-sort-td843856.html](http://elasticsearch-users.115913.n3.nabble.com/Case-insensitive-sort-td843856.html)
> 
> Is the approach taken with the multi\_field wrong, or is there another  
> layer to solving this problem that I am missing?
> 
> Thanks!
> 
> Mike

--

---

<div class="post-metadata">

### Author: ![Ryan\_Kruse](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ryan_kruse/32/2483_2.png) [@Ryan\_Kruse](https://discuss.elastic.co/u/Ryan_Kruse)
#### Post date: [February 20, 2013, 10:03pm UTC](https://discuss.elastic.co/t/case-insensitive-sort/8647/6 "2013-02-20T22:03:32Z")

</div>

vkareh's solution worked for me.

On Friday, August 10, 2012 12:44:31 PM UTC-5, vkareh wrote:

> I had a hard time using with this as well, as I needed to display  
> properly-capitalized words, but sort them in a case-insensitive fashion.  
> One solution that worked for me was to have the fields indexes as  
> "not\_analyzed", then use this in my query:
> 
> {  
> "query": {...},  
> "sort": [  
> {"\_script": {  
> "script": "doc['field\_name\_to\_sort\_by'].value.toLowerCase()",  
> "type": "string",  
> "order": "asc"  
> }}  
> ]  
> }
> 
> On Monday, August 6, 2012 10:52:04 AM UTC-4, Michael Caplan wrote:
> 
> > Hello,
> > 
> > I've been working through implementing a case insensitive sort on a  
> > string field. My initial problem was the inability to sort on  
> > an analysed field as discussed here:
> > 
> > [http://blog.wiercinski.net/2011/uncategorized/elasticsearch-sorting-on-string-types-with-more-than-one-value-per-doc-or-more-than-one-token-per-field/](http://blog.wiercinski.net/2011/uncategorized/elasticsearch-sorting-on-string-types-with-more-than-one-value-per-doc-or-more-than-one-token-per-field/)
> > 
> > With the sort field in question now defined as a multi\_field, I'm in  
> > business:
> > 
> > "last\_name": {  
> > "type": "multi\_field",  
> > "fields": {  
> > "last\_name": {  
> > "type": "string",  
> > "boost": 2,  
> > "analyzer": "name"  
> > },  
> > "untouched": {  
> > "type": "string",  
> > "index": "not\_analyzed",  
> > "include\_in\_all": false  
> > }  
> > }  
> > }
> > 
> > However, when sorting on last\_name.untouched, the sort applied is case  
> > sensitive. I seem to be missing out on a solution that seem to be hinted  
> > at here:
> > 
> > [http://elasticsearch-users.115913.n3.nabble.com/multi-field-and-sort-td3548822.html](http://elasticsearch-users.115913.n3.nabble.com/multi-field-and-sort-td3548822.html)
> > 
> > [http://elasticsearch-users.115913.n3.nabble.com/Case-insensitive-sort-td843856.html](http://elasticsearch-users.115913.n3.nabble.com/Case-insensitive-sort-td843856.html)
> > 
> > Is the approach taken with the multi\_field wrong, or is there another  
> > layer to solving this problem that I am missing?
> > 
> > Thanks!
> > 
> > Mike

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Sridharkalaibala](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sridharkalaibala/32/796_2.png) [@Sridharkalaibala](https://discuss.elastic.co/u/Sridharkalaibala)
#### Post date: [April 1, 2015, 4:05pm UTC](https://discuss.elastic.co/t/case-insensitive-sort/8647/7 "2015-04-01T16:05:25Z")

</div>

Thanks you very much... Its working...

---

<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, 12:22am UTC](https://discuss.elastic.co/t/case-insensitive-sort/8647/8 "2017-07-06T00:22:21Z")

</div>


