# Elasticsearch Query: Array field length mismatch

**URL:** <https://discuss.elastic.co/t/elasticsearch-query-array-field-length-mismatch/334551>\
**Category:** Elasticsearch\
**Created:** [May 29, 2023, 5:53am UTC](https://discuss.elastic.co/t/elasticsearch-query-array-field-length-mismatch/334551 "2023-05-29T05:53:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kusumakarb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kusumakarb/32/121534_2.png) [@kusumakarb](https://discuss.elastic.co/u/kusumakarb)\
**Post date:** [May 29, 2023, 5:53am UTC](https://discuss.elastic.co/t/elasticsearch-query-array-field-length-mismatch/334551/1 "2023-05-29T05:53:02Z")

</div>

Trying out the following query to get the values of 2 array fields and their corresponding lengths, the array values and the lengths don't match  
**Query:**

```auto
GET my_index/_search
{
  "_source": ["file_types", "link_to_file"],
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "_id": "45t8724b811b590d54cc1925166a9cb5"
          }
        }
      ]
    }
  },
  "script_fields": {
    "file_types_len": {
      "script": "doc['file_types'].length"
    },
    "link_to_file_len": {
      "script": "doc['link_to_file'].length"
    }
  }
}

```

**Result:**

```auto
{
  "_index": "my_index-150522",
  "_type": "_doc",
  "_id": "45t8724b811b590d54cc1925166a9cb5",
  "_score": 0.0,
  "_source": {
    "link_to_file": [
      "https://bucket.s3.amazonaws.com/abc123",
      "https://bucket.s3.amazonaws.com/wer134"
    ],
    "file_types": ["jpeg", "jpeg"]
  },
  "fields": {
    "file_types_len": [1],
    "link_to_file_len": [2]
  }
}

```

Both the arrays are of length 2 according the values returned, but the length for one of the arrays is says 1. Why is this happening ?

---

<div class="post-metadata">

**Author:** ![kusumakarb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kusumakarb/32/121534_2.png) [@kusumakarb](https://discuss.elastic.co/u/kusumakarb)\
**Post date:** [May 29, 2023, 2:04pm UTC](https://discuss.elastic.co/t/elasticsearch-query-array-field-length-mismatch/334551/2 "2023-05-29T14:04:34Z")

</div>

Should not be using doc values, as suggested in one of the stackoverflow answers (it's not letting me post the link here)

```auto
GET my_index/_search
{
  "_source": ["file_types", "link_to_file"],
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "_id": "45t8724b811b590d54cc1925166a9cb5"
          }
        }
      ]
    }
  },
  "script_fields": {
    "file_types_len": {
      "script": "params._source.file_types.length"
    },
    "link_to_file_len": {
      "script": "params._source.link_to_file.length"
    }
  }
}

```

Is the way to query.

---

<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:** [June 26, 2023, 2:04pm UTC](https://discuss.elastic.co/t/elasticsearch-query-array-field-length-mismatch/334551/3 "2023-06-26T14:04:51Z")

</div>

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