# Elasticsearch 5.4.3. Sorting items inside array field

**URL:** <https://discuss.elastic.co/t/elasticsearch-5-4-3-sorting-items-inside-array-field/140491>\
**Category:** Elasticsearch\
**Created:** [July 18, 2018, 9:19am UTC](https://discuss.elastic.co/t/elasticsearch-5-4-3-sorting-items-inside-array-field/140491 "2018-07-18T09:19:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lcokru](https://avatars.discourse-cdn.com/v4/letter/l/bbce88/32.png) [@lcokru](https://discuss.elastic.co/u/lcokru)\
**Post date:** [July 18, 2018, 9:19am UTC](https://discuss.elastic.co/t/elasticsearch-5-4-3-sorting-items-inside-array-field/140491/1 "2018-07-18T09:19:11Z")

</div>

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

---

<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:** [August 15, 2018, 9:19am UTC](https://discuss.elastic.co/t/elasticsearch-5-4-3-sorting-items-inside-array-field/140491/2 "2018-08-15T09:19:13Z")

</div>

Closed on August 15, 2018, 9:19am UTC

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