# Trouble sorting based on array results

**URL:** <https://discuss.elastic.co/t/trouble-sorting-based-on-array-results/109441>\
**Category:** Elasticsearch\
**Created:** [November 28, 2017, 4:41pm UTC](https://discuss.elastic.co/t/trouble-sorting-based-on-array-results/109441 "2017-11-28T16:41:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DookTibs](https://avatars.discourse-cdn.com/v4/letter/d/ebca7d/32.png) [@DookTibs](https://discuss.elastic.co/u/DookTibs)\
**Post date:** [November 28, 2017, 4:41pm UTC](https://discuss.elastic.co/t/trouble-sorting-based-on-array-results/109441/1 "2017-11-28T16:41:15Z")

</div>

I'm having trouble getting a particular Elasticsearch sort to work. I've got a type in my index that has an array of metadata objects. I want to sort based on a particular type of metadata, irregardless of the query that I used to get the results in the first place.

In my case my initial query and my sort subquery are different, and I'm wondering if that makes a difference.

As an example here's excerpts of a bash script I've used to build a test case that shows the problem:

```
curl -s -u $username:$password -X PUT $hostname/sortdemo
curl -s -u $username:$password -X PUT $hostname/sortdemo/thing/_mapping -d 
'{
   "properties": {
      "title": {
        "type": "text"
      },
      "category": {
        "type": "text"
      },
      "tags": {
         "type": "nested",
         "properties": {
            "key": {
               "type": "keyword"
            },
            "val": {
               "type": "keyword"
            }
         }
      }
   }
}'

```

I then add some "thing"s to the index:

```
curl -s -u $username:$password -X PUT $hostname/sortdemo/thing/1 -d '{
    "title": "First",
    "category": "active",
    "tags":[{"key":"SortTag","val":"apple"}, {"key":"OtherTag","val":"dog"}]
}'

curl -s -u $username:$password -X PUT $hostname/sortdemo/thing/2 -d '{
    "title": "Ignore",
    "category": "disabled",
    "tags":[{"key":"SortTag","val":"quince"}, {"key":"OtherTag","val":"octopus"}]
}'

curl -s -u $username:$password -X PUT $hostname/sortdemo/thing/3 -d '{
    "title": "Second",
    "category": "active",
    "tags":[{"key":"SortTag","val":"banana"}, {"key":"OtherTag","val":"angelfish"}]
}'

curl -s -u $username:$password -X PUT $hostname/sortdemo/thing/4 -d '{
    "title": "Third",
    "category": "active",
    "tags":[{"key":"SortTag","val":"cherry"}, {"key":"OtherTag","val":"bear"}]
}'

```

and finally I search:

```
curl -s -u $username:$password -X POST $hostname/sortdemo/_search -d '{
    "query": {
        "bool": {
            "must": [
                { "match": { "category": "active" }}
            ]
        }
    },
    "sort": [
      {
         "tags.val": {
            "order": "asc",
            "nested_filter": {
               "term": {
                  "tags.key": {
                     "value": "SortTag"
                  }
               }
            }
         }
      }
   ]
}'

```

This properly returns the "First", "Second", and "Third" items, and does not return the "Ignore" one b/c it has the wrong category. But, my sort order is not working right. I'm getting "Second", "First", "Third", and I'd expect "First", "Second", "Third". Switching the order from "asc" to "desc" or even changing "SortTag" to "garbageval" in the nested filter makes no change to the search order, so it's as if it's not being used at all.

---

<div class="post-metadata">

**Author:** ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)\
**Post date:** [December 1, 2017, 9:40am UTC](https://discuss.elastic.co/t/trouble-sorting-based-on-array-results/109441/2 "2017-12-01T09:40:25Z")

</div>

The `nested_path` is mandatory when sorting on a nested field:  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html#nested-sorting](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html#nested-sorting)  
You should add `"nested_path": "tags"` in your sort field.

---

<div class="post-metadata">

**Author:** ![DookTibs](https://avatars.discourse-cdn.com/v4/letter/d/ebca7d/32.png) [@DookTibs](https://discuss.elastic.co/u/DookTibs)\
**Post date:** [December 1, 2017, 9:16pm UTC](https://discuss.elastic.co/t/trouble-sorting-based-on-array-results/109441/3 "2017-12-01T21:16:02Z")

</div>

Thanks for the help, that solved it 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:** [December 29, 2017, 9:16pm UTC](https://discuss.elastic.co/t/trouble-sorting-based-on-array-results/109441/4 "2017-12-29T21:16:25Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
