# Sorting on Sorting

**URL:** <https://discuss.elastic.co/t/sorting-on-sorting/164319>\
**Category:** Elasticsearch\
**Created:** [January 15, 2019, 1:59pm UTC](https://discuss.elastic.co/t/sorting-on-sorting/164319 "2019-01-15T13:59:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![akhilsai1499](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/akhilsai1499/32/71890_2.png) [@akhilsai1499](https://discuss.elastic.co/u/akhilsai1499)\
**Post date:** [January 15, 2019, 1:59pm UTC](https://discuss.elastic.co/t/sorting-on-sorting/164319/1 "2019-01-15T13:59:25Z")

</div>

Hi Elastic Search Team,

Thanks for the opportunity you gave us to ask the questions.

I have one query on the sorting.

Lets say we have sample data below:

Name Date Rank  
A 01-02-2001 2  
A 01-02-2002 3  
A 01-02-2003 10

B 05-06-2011 5  
B 05-06-2012 15  
B 05-06-2013 1

C 11-12-1996 4  
C 11-12-1997 34  
C 11-12-1998 9

So after first level of sorting based on the collapse of parameter "Date" on descending order:  
We get the following result:

Name Date Rank

B 05-06-2013 1  
A 01-02-2003 10  
C 11-12-1998 9

Now , i want to sort based on the "Rank" parameter in descending order  
So the desired expected output should be:

Name Date Rank

A 01-02-2003 10  
C 11-12-1998 9  
B 05-06-2013 1

Is the above output possible??  
Please let me know if it is..

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![randel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/randel/32/2541_2.png) [@randel](https://discuss.elastic.co/u/randel)\
**Post date:** [January 15, 2019, 2:38pm UTC](https://discuss.elastic.co/t/sorting-on-sorting/164319/2 "2019-01-15T14:38:09Z")

</div>

You might want to look at nested documents. All documents of Name A would go into one document and therefore you only get one hit per Name. After that you can sort the inner hits by Date and Rank.

Alternatively look at child/parent documents.

---

<div class="post-metadata">

**Author:** ![akhilsai1499](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/akhilsai1499/32/71890_2.png) [@akhilsai1499](https://discuss.elastic.co/u/akhilsai1499)\
**Post date:** [January 16, 2019, 7:12am UTC](https://discuss.elastic.co/t/sorting-on-sorting/164319/3 "2019-01-16T07:12:17Z")

</div>

Hi Randel  
Thanks for the response.

We get here 3 innerhits for 3 different documents.  
A,B,C  
We cannot sort those 3 different innerhits based on Date and Rank.  
Since they come under different documents.  
Under each document we get one innerhit.

Doc A  
{  
innerhit A  
}

Doc B  
{  
innerhit B  
}

Doc C  
{  
innerhit C  
}

which is not possible to sort on any parameter.  
I tried from my end.  
let me know if it is possible.  
Thanks in Advance

---

<div class="post-metadata">

**Author:** ![randel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/randel/32/2541_2.png) [@randel](https://discuss.elastic.co/u/randel)\
**Post date:** [January 16, 2019, 10:11am UTC](https://discuss.elastic.co/t/sorting-on-sorting/164319/4 "2019-01-16T10:11:39Z")

</div>

The documentation of [inner\_hits](https://www.elastic.co/guide/en/elasticsearch/reference/6.5/search-request-inner-hits.html) states that [sorting](https://www.elastic.co/guide/en/elasticsearch/reference/6.5/search-request-sort.html) should be possible - something like:

```
POST /your_index/_search
{
  "query": {
    "nested": {
      "path": "Name",
      "query": {
        "term": {
          "your_field": "your_term"
        }
      },
      "inner_hits": {
        "size": 1,
        "sort": [
          {
            "Date": {
              "order": "desc"
            }
          },
          {
            "Rank": {
              "order": "desc"
            }
          }
        ]
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![akhilsai1499](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/akhilsai1499/32/71890_2.png) [@akhilsai1499](https://discuss.elastic.co/u/akhilsai1499)\
**Post date:** [January 21, 2019, 9:39am UTC](https://discuss.elastic.co/t/sorting-on-sorting/164319/5 "2019-01-21T09:39:01Z")

</div>

HI

Thank u somuch for the reply

But in the above query u mentioned  
POST /your\_index/\_search  
{  
"query": {  
"nested": {  
"path": "Name",  
"query": {  
"term": {  
"your\_field": "your\_term"  
}  
},  
"inner\_hits": {  
"size": 1,  
"sort": [  
{  
"Date": {  
"order": "desc"  
}  
},  
{  
"Rank": {  
"order": "desc"  
}  
}  
]  
}  
}  
}

Rank attribute gets sorted when and only when sort attribute values are equal.  
else it wont come to Rank attribute.

---

<div class="post-metadata">

**Author:** ![randel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/randel/32/2541_2.png) [@randel](https://discuss.elastic.co/u/randel)\
**Post date:** [January 21, 2019, 1:32pm UTC](https://discuss.elastic.co/t/sorting-on-sorting/164319/6 "2019-01-21T13:32:01Z")

</div>

You might want to add another 'sort' before inner\_hits and sort by Date and sort inside of inner\_hits by Rank.

---

<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:** [February 18, 2019, 1:32pm UTC](https://discuss.elastic.co/t/sorting-on-sorting/164319/7 "2019-02-18T13:32:05Z")

</div>

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