如何从索引中所有可用文档的弹性搜索数组中删除空字符串?

How to remove empty strings from elastic search array in all available documents in an index?

我的弹性搜索文档中有一个数组。不幸的是,它在某些文档中包含空字符串

它具有以下文档结构。

{
  "_index" : "products",
  "_type" : "_doc",
  "_id" : "100",
  "_version" : 5,
  "_seq_no" : 4,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "Washing Machine",
    "price" : 49,
    "inStock" : 5,
    "tags" : [
      "electronics",
      "home appliance",
      ""
    ]
  }
}

我正在寻找一个查询,以从索引中所有文档中存在的 tags 数组中删除所有空字符串。

注意:我使用的是弹性搜索 7.8.1

您可以使用 update_by_query

从文档中删除空字符串
POST /products/_update_by_query
{
  "query": {
    "bool": {
      "must": [{"match_all": {}}],
      "filter": [{"terms": {"tags": [""]}}]
    }
  },
  "script": {
    "source": "ctx._source.tags.removeIf(item -> item.isEmpty());",
    "lang": "painless"
  }
}