# Sort doesn't work on a "not\_analyzed" field?

**URL:** <https://discuss.elastic.co/t/sort-doesnt-work-on-a-not-analyzed-field/8588>\
**Category:** Elasticsearch\
**Created:** [July 31, 2012, 8:37pm UTC](https://discuss.elastic.co/t/sort-doesnt-work-on-a-not-analyzed-field/8588 "2012-07-31T20:37:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josh\_Hyde](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/josh_hyde/32/2865_2.png) [@Josh\_Hyde](https://discuss.elastic.co/u/Josh_Hyde)\
**Post date:** [July 31, 2012, 8:37pm UTC](https://discuss.elastic.co/t/sort-doesnt-work-on-a-not-analyzed-field/8588/1 "2012-07-31T20:37:35Z")

</div>

I have a mapping [http://pastebin.com/SQxpD2cn](http://pastebin.com/SQxpD2cn):

{  
"default":{  
"properties":{  
"EVENT\_KEY":{  
"type":"multi\_field",  
"fields":{  
"EVENT\_KEY":{  
"type":"string",  
"index":"analyzed"  
},  
"not\_analyzed":{  
"type":"string",  
"index":"not\_analyzed"  
}  
}  
},  
"SYSTEM":{  
"type":"multi\_field",  
"fields":{  
"SYSTEM":{  
"type":"string",  
"index":"analyzed"  
},  
"not\_analyzed":{  
"type":"string",  
"index":"not\_analyzed"  
}  
}  
},  
"EVENT\_TIME":{  
"type":"multi\_field",  
"fields":{  
"EVENT\_TIME":{  
"type":"string",  
"index":"analyzed"  
},  
"not\_analyzed":{  
"type":"string",  
"index":"not\_analyzed"  
}  
}  
},  
"OUTCOME":{  
"type":"multi\_field",  
"fields":{  
"OUTCOME":{  
"type":"string",  
"index":"analyzed"  
},  
"not\_analyzed":{  
"type":"string",  
"index":"not\_analyzed"  
}  
}  
},  
"USER":{  
"type":"multi\_field",  
"fields":{  
"USER":{  
"type":"string",  
"index":"analyzed"  
},  
"not\_analyzed":{  
"type":"string",  
"index":"not\_analyzed"  
}  
}  
}  
}  
}  
}

When I try to search against the index (e.g.,  
[http://localhost:9200/indexName/\_search](http://localhost:9200/indexName/_search)) with this[http://pastebin.com/LvfNHUNh](http://pastebin.com/LvfNHUNh)  
:

{  
"sort" : [  
"USER.not\_analyzed" : { "order" : "desc" }  
]  
}

...the results don't actually come back sorted by USER.not\_analyzed. I know  
elasticsearch knows the field is in there, because I search on gibberish -  
e.g., "USER.not\_there" - I get an error response. Can someone tell me what  
I'm doing wrong?

---

<div class="post-metadata">

**Author:** ![Josh\_Hyde](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/josh_hyde/32/2865_2.png) [@Josh\_Hyde](https://discuss.elastic.co/u/Josh_Hyde)\
**Post date:** [July 31, 2012, 8:49pm UTC](https://discuss.elastic.co/t/sort-doesnt-work-on-a-not-analyzed-field/8588/2 "2012-07-31T20:49:23Z")

</div>

It's worth noting: the search does _not_ fail. It returns data from the  
index, but does so without the sorting applied.

---

<div class="post-metadata">

**Author:** ![Josh\_Hyde](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/josh_hyde/32/2865_2.png) [@Josh\_Hyde](https://discuss.elastic.co/u/Josh_Hyde)\
**Post date:** [August 1, 2012, 3:27pm UTC](https://discuss.elastic.co/t/sort-doesnt-work-on-a-not-analyzed-field/8588/3 "2012-08-01T15:27:09Z")

</div>

Just as an update: the issue turned out to be that my mapping was not named  
the same as the document in which the events were being stored. While, in  
hindsight, this link appears obvious, it doesn't get explicitly called out  
in the elasticsearch documentation.

---

<div class="post-metadata">

**Author:** ![ABC](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@ABC](https://discuss.elastic.co/u/ABC)\
**Post date:** [August 2, 2012, 9:50am UTC](https://discuss.elastic.co/t/sort-doesnt-work-on-a-not-analyzed-field/8588/4 "2012-08-02T09:50:40Z")

</div>

Josh, could you please elaborate a little more on the exact change you had  
to make to you mapping to make it work. perhaps post the mapping that works  
for you so we can see the cahnge. apologies for the basic nature of the  
question. am new to this and facing a similar issue.

On Wednesday, 1 August 2012 16:27:09 UTC+1, Josh Hyde wrote:

> Just as an update: the issue turned out to be that my mapping was not  
> named the same as the document in which the events were being stored.  
> While, in hindsight, this link appears obvious, it doesn't get explicitly  
> called out in the elasticsearch documentation.

---

<div class="post-metadata">

**Author:** ![Josh\_Hyde](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/josh_hyde/32/2865_2.png) [@Josh\_Hyde](https://discuss.elastic.co/u/Josh_Hyde)\
**Post date:** [August 2, 2012, 2:42pm UTC](https://discuss.elastic.co/t/sort-doesnt-work-on-a-not-analyzed-field/8588/5 "2012-08-02T14:42:08Z")

</div>

Bear with me, as I'll be reconstituting JSON from memory, and I'm not  
really the most proficient at either of those things.

Our documents were being created with a name of "events". Because I was  
submitting a mapping named "default", our mappings for the index ended up  
looking something like:

{  
"default":{  
"properties":{  
"EVENT\_KEY":{  
"type":"multi\_field",  
"fields":{  
"EVENT\_KEY":{  
"type":"string",  
"index":"analyzed"  
},  
"not\_analyzed":{  
"type":"string",  
"index":"not\_analyzed"  
}  
}  
},  
"SYSTEM":{  
"type":"multi\_field",  
"fields":{  
"SYSTEM":{  
"type":"string",  
"index":"analyzed"  
},  
"not\_analyzed":{  
"type":"string",  
"index":"not\_analyzed"  
}  
}  
},  
"EVENT\_TIME":{  
"type":"multi\_field",  
"fields":{  
"EVENT\_TIME":{  
"type":"string",  
"index":"analyzed"  
},  
"not\_analyzed":{  
"type":"string",  
"index":"not\_analyzed"  
}  
}  
},  
"OUTCOME":{  
"type":"multi\_field",  
"fields":{  
"OUTCOME":{  
"type":"string",  
"index":"analyzed"  
},  
"not\_analyzed":{  
"type":"string",  
"index":"not\_analyzed"  
}  
}  
},  
"USER":{  
"type":"multi\_field",  
"fields":{  
"USER":{  
"type":"string",  
"index":"analyzed"  
},  
"not\_analyzed":{  
"type":"string",  
"index":"not\_analyzed"  
}  
}  
}  
}  
},  
"events":{  
"properties":{  
"EVENT\_KEY":{  
"type":"string"  
},  
"SYSTEM":{  
"type":"string"  
},  
"EVENT\_TIME":{  
"type":"string"  
},  
"OUTCOME":{  
"type":"string"  
},  
"USER":{  
"type":"string"  
}  
}  
}  
}

Because the actual data was stored in the "events" document, trying to sort  
on "default.EVENT\_KEY.not\_analyzed", while it did not error, couldn't be  
applied to a different document, which explains why the sorting just didn't  
work.

---

<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:18am UTC](https://discuss.elastic.co/t/sort-doesnt-work-on-a-not-analyzed-field/8588/6 "2017-07-06T03:18:00Z")

</div>


