# Can't sort on string of many words

**URL:** <https://discuss.elastic.co/t/cant-sort-on-string-of-many-words/4888>\
**Category:** Elasticsearch\
**Created:** [July 18, 2011, 8:52pm UTC](https://discuss.elastic.co/t/cant-sort-on-string-of-many-words/4888 "2011-07-18T20:52:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cgascon](https://avatars.discourse-cdn.com/v4/letter/c/59ef9b/32.png) [@cgascon](https://discuss.elastic.co/u/cgascon)\
**Post date:** [July 18, 2011, 8:52pm UTC](https://discuss.elastic.co/t/cant-sort-on-string-of-many-words/4888/1 "2011-07-18T20:52:10Z")

</div>

Hi,

I'm trying to sort documents based on a field named "sortableSubject" (not analyzed, type string).

But even though it's not analyzed, it throws the following exception:

"ReduceSearchPhaseException: Failed to execute phase [fetch], [reduce] ; shardFailures {[1][1][4]: QueryPhaseExecutionException[[1][4]: query[+(body:body\* | subject:body\*)],from[0],size[5000],sort[\<custom:"sortableSubject": org.elasticsearch.index.field.data.strings.StringFieldDataType$1@14637eb\>!]: Query Failed [Failed to execute main query]]; nested: IOException[Can't sort on string types with more than one value per doc, or more than one token per field]; }{[1][1][0]: QueryPhaseExecutionException[[1][0]: query[+(body:body\* | subject:body\*)],from[0],size[5000],sort[\<custom:"sortableSubject": org.elasticsearch.index.field.data.strings.StringFieldDataType$1@1b72b99\>!]: Query Failed [Failed to execute main query]]; nested: IOException[Can't sort on string types with more than one value per doc, or more than one token per field]; }{[1][1][3]: QueryPhaseExecutionException[[1][3]: query[+(body:body\* | subject:body\*)],from[0],size[5000],sort[\<custom:"sortableSubject": org.elasticsearch.index.field.data.strings.StringFieldDataType$1@547ea3\>!]: Query Failed [Failed to execute main query]]; nested: IOException[Can't sort on string types with more than one value per doc, or more than one token per field]; }{[1][1][1]: QueryPhaseExecutionException[[1][1]: query[+(body:body\* | subject:body\*)],from[0],size[5000],sort[\<custom:"sortableSubject": org.elasticsearch.index.field.data.strings.StringFieldDataType$1@8f7cf4\>!]: Query Failed [Failed to execute main query]]; nested: IOException[Can't sort on string types with more than one value per doc, or more than one token per field]; }{[1][1][2]: QueryPhaseExecutionException[[1][2]: query[+(body:body\* | subject:body\*)],from[0],size[5000],sort[\<custom:"sortableSubject": org.elasticsearch.index.field.data.strings.StringFieldDataType$1@1015d59\>!]: Query Failed [Failed to execute main query]]; nested: IOException[Can't sort on string types with more than one value per doc, or more than one token per field]; }

Am I missing something ? Can't we sort documents on a string of many words ?

---

<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:** [July 19, 2011, 9:22pm UTC](https://discuss.elastic.co/t/cant-sort-on-string-of-many-words/4888/2 "2011-07-19T21:22:31Z")

</div>

Can you gist a recreation ([Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/help))? If its not  
analyzed, this should not happen.

On Mon, Jul 18, 2011 at 11:52 PM, cgascon [gascon.charles@gmail.com](mailto:gascon.charles@gmail.com) wrote:

> Hi,
> 
> I'm trying to sort documents based on a field named "sortableSubject" (not  
> analyzed, type string).
> 
> But even though it's not analyzed, it throws the following exception:
> 
> "ReduceSearchPhaseException: Failed to execute phase [fetch], [reduce] ;  
> shardFailures {[1][1][4]: QueryPhaseExecutionException[[1][4]:  
> query[+(body:body\* |
> 
> subject:body\*)],from[0],size[5000],sort[\<custom:"sortableSubject":  
> org.elasticsearch.index.field.data.strings.StringFieldDataType$1@14637eb  
> \>!]:  
> Query Failed [Failed to execute main query]]; nested: IOException[Can't  
> sort  
> on string types with more than one value per doc, or more than one token  
> per  
> field]; }{[1][1][0]: QueryPhaseExecutionException[[1][0]:  
> query[+(body:body\*  
> |
> 
> subject:body\*)],from[0],size[5000],sort[\<custom:"sortableSubject":  
> org.elasticsearch.index.field.data.strings.StringFieldDataType$1@1b72b99  
> \>!]:  
> Query Failed [Failed to execute main query]]; nested: IOException[Can't  
> sort  
> on string types with more than one value per doc, or more than one token  
> per  
> field]; }{[1][1][3]: QueryPhaseExecutionException[[1][3]:  
> query[+(body:body\*  
> |
> 
> subject:body\*)],from[0],size[5000],sort[\<custom:"sortableSubject":  
> org.elasticsearch.index.field.data.strings.StringFieldDataType$1@547ea3  
> \>!]:  
> Query Failed [Failed to execute main query]]; nested: IOException[Can't  
> sort  
> on string types with more than one value per doc, or more than one token  
> per  
> field]; }{[1][1][1]: QueryPhaseExecutionException[[1][1]:  
> query[+(body:body\*  
> |
> 
> subject:body\*)],from[0],size[5000],sort[\<custom:"sortableSubject":  
> org.elasticsearch.index.field.data.strings.StringFieldDataType$1@8f7cf4  
> \>!]:  
> Query Failed [Failed to execute main query]]; nested: IOException[Can't  
> sort  
> on string types with more than one value per doc, or more than one token  
> per  
> field]; }{[1][1][2]: QueryPhaseExecutionException[[1][2]:  
> query[+(body:body\*  
> |
> 
> subject:body\*)],from[0],size[5000],sort[\<custom:"sortableSubject":  
> org.elasticsearch.index.field.data.strings.StringFieldDataType$1@1015d59  
> \>!]:  
> Query Failed [Failed to execute main query]]; nested: IOException[Can't  
> sort  
> on string types with more than one value per doc, or more than one token  
> per  
> field]; }
> 
> Am I missing something ? Can't we sort documents on a string of many words  
> ?
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/Can-t-sort-on-string-of-many-words-tp3180547p3180547.html](http://elasticsearch-users.115913.n3.nabble.com/Can-t-sort-on-string-of-many-words-tp3180547p3180547.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![cgascon](https://avatars.discourse-cdn.com/v4/letter/c/59ef9b/32.png) [@cgascon](https://discuss.elastic.co/u/cgascon)\
**Post date:** [July 20, 2011, 3:02am UTC](https://discuss.elastic.co/t/cant-sort-on-string-of-many-words/4888/3 "2011-07-20T03:02:29Z")

</div>

This was made while unit testing (local node) and I just realized the mapping wasn't really put, so default mapping was used I guess (the field was then analyzed and cause problem when I was sorting on this field).

Thanks though !

---

<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, 7:00am UTC](https://discuss.elastic.co/t/cant-sort-on-string-of-many-words/4888/4 "2012-02-02T07:00:29Z")

</div>

hi,cgascon,i has the some problem,can you tall me how do you fixed it?thank you!

---

<div class="post-metadata">

**Author:** ![cgascon](https://avatars.discourse-cdn.com/v4/letter/c/59ef9b/32.png) [@cgascon](https://discuss.elastic.co/u/cgascon)\
**Post date:** [February 2, 2012, 2:20pm UTC](https://discuss.elastic.co/t/cant-sort-on-string-of-many-words/4888/5 "2012-02-02T14:20:03Z")

</div>

hi,cgascon,i has the some problem,can you tall me how do you fixed it?thank you!

Hi yin weifeng,

The problem was the mapping.

Here is the mapping I have now (I use two properties), please notice I've copied this code from java and removed some backslash etc so there may be some syntax errors.

[...]  
"subject":{"type":"string"},  
"sortableSubject":{"index":"not\_analyzed","type":"string"}  
[...]

The problem was that the mapping was not this one, but I thought it was. sortableSubject was then an analyzed string.

So I got rid on my old mapping made sure to create this mapping for the object these properties are in.

I Hope it helps.

---

<div class="post-metadata">

**Author:** ![louis\_gueye](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/louis_gueye/32/2772_2.png) [@louis\_gueye](https://discuss.elastic.co/u/louis_gueye)\
**Post date:** [February 6, 2012, 11:10am UTC](https://discuss.elastic.co/t/cant-sort-on-string-of-many-words/4888/6 "2012-02-06T11:10:40Z")

</div>

Hi yin,

A string, when analyzed, gets split into tokens.

So imagine those strings

"can you tall me how do you fixed it"  
"you can not afford that"

Results (very roughly) in ["can" : 1,"you" : 2,"tall" : 1,"me" : 1, "how" :  
1,"do" : 1, "fixed" : 1, "it" : 1] after analyzing.  
Results (very roughly) in ["can" : 1,"you" : 1,"afford" : 1,"that" : 1] after  
analyzing.

When you search by "you" you will get 2 hits, but the original field  
doesn't exist any longer, it's has been split into occurences by token. How  
do you sort then ?  
You can sort by score : the document that has the highest tokens count  
comes first (call it ranking, pertinence, whatever).  
That's the reason why one can not apply lexicographic sort on ANALYZED  
fields, NON ANALYZED ones can.

There are some workarounds : store 2 versions of your field, one that is  
analyzed, the other one that is not, and sort by the one that is not  
analyzed.  
This is exactly what \*multifield \*does.

Hope it helps.

--  
Cordialement/Regards,

Louis GUEYE  
linkedin [http://fr.linkedin.com/in/louisgueye](http://fr.linkedin.com/in/louisgueye) |  
blog[http://deepintojee.wordpress.com/](http://deepintojee.wordpress.com/)  
| twitter [http://twitter.com/#!/lgueye](http://twitter.com/#%21/lgueye)

2012/2/2 yin weifeng [ywf1990@gmail.com](mailto:ywf1990@gmail.com)

> hi,cgascon,i has the some problem,can you tall me how do you fixed it?thank  
> you!
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/Can-t-sort-on-string-of-many-words-tp3180547p3709137.html](http://elasticsearch-users.115913.n3.nabble.com/Can-t-sort-on-string-of-many-words-tp3180547p3709137.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<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:40am UTC](https://discuss.elastic.co/t/cant-sort-on-string-of-many-words/4888/7 "2017-07-06T03:40:29Z")

</div>


