Elasticsearch 5.4.3. Sorting items inside array field

Hi. I have the mapping that is similar to bellow:

PUT car-company-index
{
"mappings": {
"carUser": {
"properties": {
"name": {
"type": "keyword"
},
"cars": {
"type": "object",
"properties": {
"car": {
"type": "nested",
"include_in_root": false,
"properties": {
"model": {
"type": "keyword"
},
"color": {
"type": "keyword"
},
"price": {
"type": "long"
},
"comment": {
"type": "nested",
"include_in_root": false,
"properties": {
"user": {
"type": "keyword"
},
"mark": {
"type": "byte"
},
"text": {
"type": "text"
}
}
}
}
}
}
}
}
}
}
}

Two records were inserted in the index:

PUT car-company-index/carUser/1
{
"name": "Alice White",
"cars": {
"car": [
{
"model": "Mersedess Benz",
"color": "white",
"price": 1000000,
"comment": [
{
"user": "Derek",
"mark": 5,
"text": "Awesome!"
},
{
"user": "Alice",
"mark": 2,
"text": "Sucks"
},
{
"user": "Scott",
"mark": 7,
"text": "Nice and chilly"
}
]
},
{
"model": "Wolkswagen Golf",
"color": "red",
"price": 500000,
"comment": [
{
"user": "Tony",
"mark": 3,
"text": "Awesome!"
},
{
"user": "Peter",
"mark": 10,
"text": "Sucks"
}
]
},
{
"model": "Mazda RX-7",
"color": "black",
"price": 1200000,
"comment": [
{
"user": "Kate",
"mark": 5,
"text": "Awesome!"
},
{
"user": "Cisco",
"mark": 2,
"text": "Not very cool"
},
{
"user": "Barry",
"mark": 7,
"text": "Nice and chilly"
}
]
}
]
}
}

PUT car-company-index/carUser/2
{
"name": "Jhon Doe",
"cars": {
"car": [
{
"model": "Mersedess Vito",
"color": "white",
"price": 1000000,
"comment": [
{
"user": "Alex",
"mark": 5,
"text": "Awesome!"
},
{
"user": "Thawn",
"mark": 2,
"text": "Not very cool"
}
]
},
{
"model": "Lexus",
"color": "red",
"price": 1500000,
"comment": [
{
"user": "Criss",
"mark": 5,
"text": "Awesome!"
},
{
"user": "Jessy",
"mark": 2,
"text": "Not very cool"
},
{
"user": "Antony",
"mark": 7,
"text": "Nice and chilly"
}
]
},
{
"model": "Chevrolet Camaro",
"color": "black",
"price": 1200000
}
]
}
}

Now, I make search request and want comments field to be sorted by user property. So I want a result that is similar to:

"hits": [
  {
    "_index": "car-company-index",
    "_type": "carUser",
    "_id": "2",
    "_score": 2.287682,
    "_source": {
      "name": "Jhon Doe",
      "cars": {
        "car": [
          {
            "model": "Mersedess Vito",
            "color": "white",
            "price": 1000000,
            "comment": [
              {
                "user": "Alex", <-- if sorted in ascending order
                "mark": 5,
                "text": "Awesome!"
              },
              {
                "user": "Thawn", <-- if sorted in ascending order
                "mark": 2,
                "text": "Not very cool"
              }
            ]
          },
          {
            "model": "Lexus",
            "color": "red",
            "price": 1500000,
            "comment": [
              {
                "user": "Antony",  <-- should be the first item if sorted in ascending order
                "mark": 7,
                "text": "Nice and chilly"
              }
              {
                "user": "Criss",
                "mark": 5,
                "text": "Awesome!"
              },
              {
                "user": "Jessy",
                "mark": 2,
                "text": "Not very cool"
              },
            ]
          },
          {
            "model": "Chevrolet Camaro",
            "color": "black",
            "price": 1200000
          }
        ]
      }
    }
  }
]

Any ideas regarding this question?
Thank you in advance

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