Sorting on Sorting

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.

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.

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

The documentation of inner_hits states that sorting 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"
            }
          }
        ]
      }
    }
  }
}

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.

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

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