# Sort Chinese error

**URL:** <https://discuss.elastic.co/t/sort-chinese-error/6572>\
**Category:** Elasticsearch\
**Created:** [February 2, 2012, 5:42am UTC](https://discuss.elastic.co/t/sort-chinese-error/6572 "2012-02-02T05:42:05Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![yin\_weifeng](https://avatars.discourse-cdn.com/v4/letter/y/f4b2a3/32.png) [@yin\_weifeng](https://discuss.elastic.co/u/yin_weifeng)\
**Post date:** [February 2, 2012, 5:42am UTC](https://discuss.elastic.co/t/sort-chinese-error/6572/1 "2012-02-02T05:42:05Z")

</div>

I use such code:  
"SearchResponse searchResponse = client.prepareSearch("My\_db")  
.setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH)  
.setQuery(queryBuilder)  
.addSort(“xxx”,SortOrder.DESC)..."  
hope to sort by field "xxx" ,but when the value of field "xxx" is  
Chinese , or '#','%'...，Es throw an error message:Query Failed [Failed  
to execute main query],what should I do.

---

<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:** [February 3, 2012, 5:11am UTC](https://discuss.elastic.co/t/sort-chinese-error/6572/2 "2012-02-03T05:11:07Z")

</div>

What is the exact error? One possible issue is if field "xxx" is  
analyzed. You can only sort on non-analyzed fields.

On Wed, Feb 1, 2012 at 9:42 PM, 伟峰 殷 [ywf1990@gmail.com](mailto:ywf1990@gmail.com) wrote:

> I use such code:  
> "SearchResponse searchResponse = client.prepareSearch("My\_db")  
> .setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH)  
> .setQuery(queryBuilder)  
> .addSort(“xxx”,SortOrder.DESC)..."  
> hope to sort by field "xxx" ,but when the value of field "xxx" is  
> Chinese , or '#','%'...，Es throw an error message:Query Failed [Failed  
> to execute main query],what should I do.

---

<div class="post-metadata">

**Author:** ![yin\_weifeng](https://avatars.discourse-cdn.com/v4/letter/y/f4b2a3/32.png) [@yin\_weifeng](https://discuss.elastic.co/u/yin_weifeng)\
**Post date:** [February 3, 2012, 8:45am UTC](https://discuss.elastic.co/t/sort-chinese-error/6572/3 "2012-02-03T08:45:07Z")

</div>

Thanks Ivan!

I want to do term search and also sorting on the same field, should I make two different index fields for the same contents, or some other way?

---

<div class="post-metadata">

**Author:** ![yin\_weifeng](https://avatars.discourse-cdn.com/v4/letter/y/f4b2a3/32.png) [@yin\_weifeng](https://discuss.elastic.co/u/yin_weifeng)\
**Post date:** [February 3, 2012, 8:46am UTC](https://discuss.elastic.co/t/sort-chinese-error/6572/4 "2012-02-03T08:46:26Z")

</div>

- deleted -

---

<div class="post-metadata">

**Author:** ![Jan\_Fiedler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jan_fiedler/32/2518_2.png) [@Jan\_Fiedler](https://discuss.elastic.co/u/Jan_Fiedler)\
**Post date:** [February 3, 2012, 10:31am UTC](https://discuss.elastic.co/t/sort-chinese-error/6572/5 "2012-02-03T10:31:58Z")

</div>

For correct, locale specific sorting you should create a separate field for  
sorting purposes. This is best done via the multi-field mapping (  
[http://www.elasticsearch.org/guide/reference/mapping/multi-field-type.html](http://www.elasticsearch.org/guide/reference/mapping/multi-field-type.html)).

The sort field should use a special (sort) analyzer that performs collation  
for Chinese. In simple terms, a collator takes your term and calculates a  
sorting key (that does not resemble the term). Take a look at the ICU  
plugin and its collators (  
[http://www.elasticsearch.org/guide/reference/index-modules/analysis/icu-plugin.html](http://www.elasticsearch.org/guide/reference/index-modules/analysis/icu-plugin.html)  
)

---

<div class="post-metadata">

**Author:** ![yin\_weifeng](https://avatars.discourse-cdn.com/v4/letter/y/f4b2a3/32.png) [@yin\_weifeng](https://discuss.elastic.co/u/yin_weifeng)\
**Post date:** [February 3, 2012, 12:21pm UTC](https://discuss.elastic.co/t/sort-chinese-error/6572/6 "2012-02-03T12:21:32Z")

</div>

hi, Jan

Thank you for your help!  
I use this mapping,now it can sorting and search at on the field.  
...  
"fields" : {  
"fieldName" : {"type" : "string", "index" : "analyzed"},  
"sortFieldName" : {"type" : "string", "index" : "not\_analyzed"}  
}  
...

But for the Chinese, sorting as "not\_analyzed" seams no significance  
How to define the special sort analyzer , for example in phonetic.

2012/2/3 Jan Fiedler [fiedler.jan@gmail.com](mailto:fiedler.jan@gmail.com)

> For correct, locale specific sorting you should create a separate field  
> for sorting purposes. This is best done via the multi-field mapping (  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/multi-field-type.html)  
> ).
> 
> The sort field should use a special (sort) analyzer that performs  
> collation for Chinese. In simple terms, a collator takes your term and  
> calculates a sorting key (that does not resemble the term). Take a look at  
> the ICU plugin and its collators (  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/index-modules/analysis/icu-plugin.html)  
> )

---

<div class="post-metadata">

**Author:** ![yin\_weifeng](https://avatars.discourse-cdn.com/v4/letter/y/f4b2a3/32.png) [@yin\_weifeng](https://discuss.elastic.co/u/yin_weifeng)\
**Post date:** [February 3, 2012, 12:23pm UTC](https://discuss.elastic.co/t/sort-chinese-error/6572/7 "2012-02-03T12:23:57Z")

</div>

hi, Jan

Thank you for your help!  
I use this mapping,now it can sorting and search at on the field.  
...  
"fields" : {  
"fieldName" : {"type" : "string", "index" : "analyzed"},  
"sortFieldName" : {"type" : "string", "index" : "not\_analyzed"}  
}  
...

But for the Chinese, sorting as "not\_analyzed" seams no significance  
How to define the special sort analyzer , for example in phonetic.

2012/2/3 Jan Fiedler [fiedler.jan@gmail.com](mailto:fiedler.jan@gmail.com)

> For correct, locale specific sorting you should create a separate field  
> for sorting purposes. This is best done via the multi-field mapping (  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/multi-field-type.html)  
> ).
> 
> The sort field should use a special (sort) analyzer that performs  
> collation for Chinese. In simple terms, a collator takes your term and  
> calculates a sorting key (that does not resemble the term). Take a look at  
> the ICU plugin and its collators (  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/index-modules/analysis/icu-plugin.html)  
> )

---

<div class="post-metadata">

**Author:** ![Jan\_Fiedler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jan_fiedler/32/2518_2.png) [@Jan\_Fiedler](https://discuss.elastic.co/u/Jan_Fiedler)\
**Post date:** [February 3, 2012, 2:47pm UTC](https://discuss.elastic.co/t/sort-chinese-error/6572/8 "2012-02-03T14:47:49Z")

</div>

You are close but not there yet. To get Chinese sorting right you need the  
following 3 additional steps:

_1. Configure a sort analyzer for your sort field_

...  
"fields" : {  
"fieldName" : {"type" : "string", "index" : "analyzed"},  
"sortFieldName" : {"type" : "string", "index" : "analyzed", "analyzer"  
: "my\_chinese\_sort"}  
}  
...

1. Configure the sort analyzer (e.g. in elasticsearch.yml)

index:  
analysis:  
analyzer:  
my\_chinese\_sort :  
type : custom  
tokenizer : keyword  
filter : [icu\_collation\_chinese]

filter:  
icu\_collation\_chinese:  
type: icu\_collation  
language : ch

I am not sure about the actual language identifier to be used for Chinese.  
I trust that ICU supports Chinese (I did not try it).

_3. Install the ICU plugin_

Run the following from your ES home:

bin/plugin -install elasticsearch/elasticsearch-analysis-icu/1.1.0

---

<div class="post-metadata">

**Author:** ![yin\_weifeng](https://avatars.discourse-cdn.com/v4/letter/y/f4b2a3/32.png) [@yin\_weifeng](https://discuss.elastic.co/u/yin_weifeng)\
**Post date:** [February 6, 2012, 2:06am UTC](https://discuss.elastic.co/t/sort-chinese-error/6572/9 "2012-02-06T02:06:03Z")

</div>

Thank you!

You are right, the configuration can really make the field order by Chinese  
pinyin, I think that ICU do something with it.

But the configuration in elasticsearch.yml has no effect,I use

curl -XPOST localhost:9200/backlog\_db -d '{  
"settings":{  
"index" : {  
"analysis" : {  
"analyzer" : {  
"my\_chinese\_sort" : {  
"type" : "custom",  
"tokenizer" : "keyword",  
"filter" : ["icu\_collation\_chinese"]  
}  
},  
"filter" : {  
"icu\_collation\_chinese:" : {  
"type" : "icu\_collation",  
"language" : "ch"  
}  
}  
}  
}  
}  
}'

to configure the sort analyzer.

---

<div class="post-metadata">

**Author:** ![Jan\_Fiedler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jan_fiedler/32/2518_2.png) [@Jan\_Fiedler](https://discuss.elastic.co/u/Jan_Fiedler)\
**Post date:** [February 6, 2012, 11:17am UTC](https://discuss.elastic.co/t/sort-chinese-error/6572/10 "2012-02-06T11:17:07Z")

</div>

I recommend using the analyze API (via curl interactively) to test whether  
your analyzer settings made it correctly into your index. Find information  
on the analyzer API usage here:  
[http://www.elasticsearch.org/guide/reference/api/admin-indices-analyze.html](http://www.elasticsearch.org/guide/reference/api/admin-indices-analyze.html)

---

<div class="post-metadata">

**Author:** ![yin\_weifeng](https://avatars.discourse-cdn.com/v4/letter/y/f4b2a3/32.png) [@yin\_weifeng](https://discuss.elastic.co/u/yin_weifeng)\
**Post date:** [February 15, 2012, 8:55am UTC](https://discuss.elastic.co/t/sort-chinese-error/6572/11 "2012-02-15T08:55:32Z")

</div>

Now it works well on win7 system, but when I use it in Linux environment  
(Ubuntu 11.10 and CentOS5.6), the sorting result are different even the  
configuration is same.

What could be the reason?

thanks!

2012/2/6 Jan Fiedler [fiedler.jan@gmail.com](mailto:fiedler.jan@gmail.com)

> I recommend using the analyze API (via curl interactively) to test whether  
> your analyzer settings made it correctly into your index. Find information  
> on the analyzer API usage here:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/admin-indices-analyze.html)

---

<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:39am UTC](https://discuss.elastic.co/t/sort-chinese-error/6572/12 "2017-07-06T03:39:17Z")

</div>


