# How to sory array type field using the length

**URL:** <https://discuss.elastic.co/t/how-to-sory-array-type-field-using-the-length/344210>\
**Category:** Kibana\
**Created:** [October 2, 2023, 7:52am UTC](https://discuss.elastic.co/t/how-to-sory-array-type-field-using-the-length/344210 "2023-10-02T07:52:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anand\_Konagala](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anand_konagala/32/120215_2.png) [@Anand\_Konagala](https://discuss.elastic.co/u/Anand_Konagala)\
**Post date:** [October 2, 2023, 7:52am UTC](https://discuss.elastic.co/t/how-to-sory-array-type-field-using-the-length/344210/1 "2023-10-02T07:52:24Z")

</div>

Hi,

I want to create an index having with one array field.. Then, how can I give mapping for that array field which is available to sort using the length of that array. One more thing that it supports that the array having more that n length. Can anybody share the example mapping and queries for this conditions.

Thank you,

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [October 2, 2023, 8:59am UTC](https://discuss.elastic.co/t/how-to-sory-array-type-field-using-the-length/344210/2 "2023-10-02T08:59:51Z")

</div>

Hi @Anand_Konagala,

To confirm, are you looking to sort the documents in the query using the length of array fields? You should be able to do that using [sort context](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-sort-context.html) with a Painless script using the [Array length operator](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-operators-array.html#array-length-operator).

Hope that helps!

---

<div class="post-metadata">

**Author:** ![Anand\_Konagala](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anand_konagala/32/120215_2.png) [@Anand\_Konagala](https://discuss.elastic.co/u/Anand_Konagala)\
**Post date:** [October 2, 2023, 9:17am UTC](https://discuss.elastic.co/t/how-to-sory-array-type-field-using-the-length/344210/3 "2023-10-02T09:17:46Z")

</div>

Hi @carly.richmond ,

thanks for the response, And I have one more doubt that what kind of field type is suitable for this query.

Thank you,

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [October 2, 2023, 10:45am UTC](https://discuss.elastic.co/t/how-to-sory-array-type-field-using-the-length/344210/4 "2023-10-02T10:45:50Z")

</div>

Hi @Anand_Konagala,

By field type do you mean strings versus integers versus objects? There isn't technically a dedicated array type as fields accept 0 or more values. The only condition would be all elements need to be the same type.

I would recommend reading the arrays section of the documentation located [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/array.html) for a more complete answer.

Hope that helps!

---

<div class="post-metadata">

**Author:** ![Anand\_Konagala](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anand_konagala/32/120215_2.png) [@Anand\_Konagala](https://discuss.elastic.co/u/Anand_Konagala)\
**Post date:** [October 2, 2023, 11:57am UTC](https://discuss.elastic.co/t/how-to-sory-array-type-field-using-the-length/344210/5 "2023-10-02T11:57:25Z")

</div>

Hi @carly.richmond,

I gave a mapping for one of the field as type "nested"

```auto
PUT mr-profiles
{
"mapping": {
    "properties": {
        "locations": {
          "type": "nested"
        }
      }
  }
}

```

And I ran one query on that,

```auto
GET mr-profiles/_search
{
  "sort": {
  "_script": {
    "type": "number",
    "script": {
      "lang": "painless",
      "source": "doc['locations'].values.size() * params.factor",
      "params": {
        "factor": 1.1
      }
    },
    "order": "asc"
  }
}

}

```

But, It throughs an Error

```auto
"type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "mr-profiles",
        "node": "9PF_FW5aQcmLIxDothVTOg",
        "reason": {
          "type": "script_exception",
          "reason": "runtime error",
          "script_stack": [
            "org.elasticsearch.server@8.8.0/org.elasticsearch.search.lookup.LeafDocLookup.getFactoryForDoc(LeafDocLookup.java:148)",
            "org.elasticsearch.server@8.8.0/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:191)",
            "org.elasticsearch.server@8.8.0/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:32)",
            "doc['locations'].values.size() * params.factor",
            " ^---- HERE"
          ],
          "script": "doc['locations'].values.size() * params.factor",
          "lang": "painless",
          "position": {
            "offset": 4,
            "start": 0,
            "end": 61
          },
          "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "No field found for [locations] in mapping"
          }
        }
      }
    ]

```

I gave a mapping, But Why it shows mapping not found error..  
Could you tell me that Why I am getting This error

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [October 2, 2023, 12:55pm UTC](https://discuss.elastic.co/t/how-to-sory-array-type-field-using-the-length/344210/6 "2023-10-02T12:55:06Z")

</div>

> [@Anand\_Konagala](#):
>
> `doc['locations'].values.size()`

I'm not sure this is the issue, as I would recommending double checking the mapping. But should it not be `doc['locations'].length` [as per this example](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-walkthrough.html#_accessing_doc_values_from_painless)?

---

<div class="post-metadata">

**Author:** ![Anand\_Konagala](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anand_konagala/32/120215_2.png) [@Anand\_Konagala](https://discuss.elastic.co/u/Anand_Konagala)\
**Post date:** [October 3, 2023, 6:12am UTC](https://discuss.elastic.co/t/how-to-sory-array-type-field-using-the-length/344210/7 "2023-10-03T06:12:27Z")

</div>

Hii @carly.richmond

I tried a lot of queries.. but, It not works.. It through an error.. Can you try this  
Here is my mapping,

```auto
PUT array_sort
{
  "mappings": {
    "properties": {
      "Skills": {
        "type": "nested",
        "properties": {
          "text": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

```

This is the data

```auto
POST array_sort/_doc/1
{
  "Skills": [
    {"text": "java"},
    {"text": "elasticsearch"}
  ]
}

POST array_sort/_doc/2
{
  "Skills": [
    {"text": "python"},
    {"text": "ruby"},
    {"text": "javascript"}
  ]
}

POST array_sort/_doc/3
{
  "Skills": [
    {"text": "sql"},
    {"text": "html"},
    {"text": "css"}
  ]
}

```

Here is the query for finding that array size more then two,

```auto
GET array_sort/_search
{
  "query": {
    "nested": {
      "path": "Skills",
      "query": {
        "script": {
          "script": {
            "source": "doc['Skills'].length > 3",
            "lang": "painless"
          }
        }
      }
    }
  }
}

```

I tried a lot of queries But, I am getting an error.. Here is an error

```auto
{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "org.elasticsearch.server@8.7.1/org.elasticsearch.search.lookup.LeafDocLookup.getFactoryForDoc(LeafDocLookup.java:127)",
          "org.elasticsearch.server@8.7.1/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:170)",
          "org.elasticsearch.server@8.7.1/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:32)",
          """if (doc['Skills'].length() > 2) {
                """,
          " ^---- HERE"
        ],
        "script": " ...",
        "lang": "painless",
        "position": {
          "offset": 23,
          "start": 15,
          "end": 65
        }
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "array_sort",
        "node": "x4tf2IsOSUu4VVsm2zVwGA",
        "reason": {
          "type": "script_exception",
          "reason": "runtime error",
          "script_stack": [
            "org.elasticsearch.server@8.7.1/org.elasticsearch.search.lookup.LeafDocLookup.getFactoryForDoc(LeafDocLookup.java:127)",
            "org.elasticsearch.server@8.7.1/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:170)",
            "org.elasticsearch.server@8.7.1/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:32)",
            """if (doc['Skills'].length() > 2) {
                """,
            " ^---- HERE"
          ],
          "script": " ...",
          "lang": "painless",
          "position": {
            "offset": 23,
            "start": 15,
            "end": 65
          },
          "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "No field found for [Skills] in mapping"
          }
        }
      }
    ]
  },
  "status": 400
}

```

Can you try this and inform.. Why I am encountering this Error,

Thank you,

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [October 3, 2023, 8:59am UTC](https://discuss.elastic.co/t/how-to-sory-array-type-field-using-the-length/344210/8 "2023-10-03T08:59:59Z")

</div>

Hi @Anand_Konagala,

Looking at your mapping I think you need to check the `skills.text` field:

```auto
GET array_sort/_search
{
  "query": {
    "bool": {
      "filter": {
        "script": {
          "script": """
            return doc['skills.text'].length > 2
          """
        }
      }
    }
  }
}

```

Your example query also had 2 levels of the `script` tagv when there should be only one.

---

<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 31, 2023, 9:00am UTC](https://discuss.elastic.co/t/how-to-sory-array-type-field-using-the-length/344210/9 "2023-10-31T09:00:11Z")

</div>

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