Elasticsearch查询:数组中的值与数组长度不匹配。

huangapple go评论71阅读模式
英文:

Elasticsearch Query: Mismatch between values in the array and the array length

问题

尝试以下查询以获取两个数组字段及其相应长度的值,但数组的值和长度不匹配。

查询:

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"
    }
  }
}

结果:

{
  "_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]
  }
}

根据返回的值,两个数组的长度都是2,但其中一个数组的长度显示为1。为什么会发生这种情况?

英文:

Tried 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 ?

答案1

得分: 2

因为 file_types 数组包含相同的值两次(即 jpeg),所以出现了这个情况。由于您的脚本使用文档值工作,数组中只包含一个 jpeg 实例,不需要两个。

如果您想要检索在 _source 中存在的数组的长度,那么您可以更改您的脚本以在 _source 字段上运行,像这样:

params['_source'].file_types.length
英文:

It's because the file_types array contains the same value twice (i.e. jpeg). Since your script works on doc values, the array only contains a single instance of jpeg, no need for two.

If you want to retrieve the length of the arrays as they are present in the _source, then you can change your script to work on the _source field instead, like this:

params['_source'].file_types.length

huangapple
  • 本文由 发表于 2023年5月29日 17:32:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76356185.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定