Elasticsearch Query: Array field length mismatch

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:

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:

{
  "_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 ?

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

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.

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