# Order by length of array properties

**URL:** <https://discuss.elastic.co/t/order-by-length-of-array-properties/285393>\
**Category:** Elasticsearch\
**Created:** [September 28, 2021, 4:52pm UTC](https://discuss.elastic.co/t/order-by-length-of-array-properties/285393 "2021-09-28T16:52:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![juancruzdev](https://avatars.discourse-cdn.com/v4/letter/j/a88e57/32.png) [@juancruzdev](https://discuss.elastic.co/u/juancruzdev)\
**Post date:** [September 28, 2021, 4:52pm UTC](https://discuss.elastic.co/t/order-by-length-of-array-properties/285393/1 "2021-09-28T16:52:43Z")

</div>

i have an index with objects and an array called "reviews" that inside has properties called positive\_comment, negative\_comment, reviewer\_name and more.

There are some reviews that have the field positive\_comment empty (not null, just empty).

I need to order by the length of the field positive\_comment first, so reviews with empty positive\_comment comes last. The results are the same when ordering in SQL with LEN() property.

This is my query. I also tried: .value.size() in the script, or type "string" but no results.

```auto
{
   "_source":[
      "reviews.positive_comment"
   ],
   "query":{
      "bool":{
         "filter":[
            {
               "term":{
                  "id":214
               }
            }
         ]
      }
   },
   "sort":{
      "_script":{
         "script":"doc['reviews.positive_comment'].value.length()",
         "type":"number",
         "order":"asc"
      }
   }
}

```

This is my result:

```auto
{
   "_source":{
      "reviews":[
         {
            "positive_comment":"Great"
         },
         {
            "positive_comment":"Really good product"
         },
         {
            "positive_comment":""
         },
         {
            "positive_comment":""
         },
         {
            "positive_comment":""
         },
         {
            "positive_comment":""
         },
         {
            "positive_comment":""
         },
         {
            "positive_comment":""
         },
         {
            "positive_comment":""
         },
         {
            "positive_comment":""
         },
         {
            "positive_comment":""
         },
         {
            "positive_comment":""
         },
         {
            "positive_comment":""
         },
         {
            "positive_comment":""
         },
         {
            "positive_comment":""
         },
         {
            "positive_comment":"Awesome"
         }
      ]
   },
   "sort":[
      "0"
   ]
}

```

Additional info:

First object of array:

```auto
"reviews": [{ "item_id": 214, "year": 1990, "date_created": "2021-07-21T15:05:41.103", "negative_comment": "Nothing", "valuation": 3, "reviewer_name": "", "positive_comment": "Awesome", "reviewer_id": 7465, "comment": "This is a comment.", "id": 1817 }]

```

Mapping of this particular property of the array:

```auto
"reviews": { "properties": { "positive_comment": { "type": "keyword" }}}

```

---

<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:** [October 26, 2021, 4:53pm UTC](https://discuss.elastic.co/t/order-by-length-of-array-properties/285393/2 "2021-10-26T16:53:18Z")

</div>

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