Elasticsearch 从动态生成的索引中的数组对象中删除一个字段

Elasticsearch remove a field from an object of an array in a dynamically generated index

我正在尝试从 Elasticsearch 中的数组对象中删除字段。索引已动态生成。

这是映射:

{
  "mapping": {
    "_doc": {
      "properties": {
        "age": {
          "type": "long"
        },
        "name": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "result": {
          "properties": {
            "resultid": {
              "type": "long"
            },
            "resultname": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            }
          },
        "timestamp": {
          "type": "date"
        }
      }
    }
  }
}
}

这是一个文档:

{
    "result": [
        {
            "resultid": 69,
            "resultname": "SFO"
        },
        {
            "resultid": 151,
            "resultname": "NYC"
        }
    ],
    "age": 54,
    "name": "Jorge",
    "timestamp": "2020-04-02T16:07:47.292000"
}

我的目标是删除索引的所有文档中resultid 的所有字段。更新后文档应如下所示:

{
    "result": [
        {
            "resultname": "SFO"
        },
        {
            "resultname": "NYC"
        }
    ],
    "age": 54,
    "name": "Jorge",
    "timestamp": "2020-04-02T16:07:47.292000"
}

我尝试使用以下有关 Whosebug 的文章,但没有成功: Remove elements/objects From Array in ElasticSearch Followed by Matching Query remove objects from array that satisfying the condition in elastic search with javascript api Delete nested array in elasticsearch Removing objects from nested fields in ElasticSearch

希望有人能帮我找到解决办法。

您应该使用 _reindex API 在新的索引中重新编制索引并调用脚本来删除您的字段:

POST _reindex
{
  "source": {
    "index": "my-index"
  },
  "dest": {
    "index": "my-index-reindex"
  },
  "script": {
    "source": """
     for (int i=0;i<ctx._source.result.length;i++) {
        ctx._source.result[i].remove("resultid")
     }
     """

  }
}

可以删除第一个索引后:

DELETE my-index

并重新编制索引:

POST _reindex
{
  "source": {
    "index": "my-index-reindex"
  },
  "dest": {
    "index": "my-index"
  }
}

我将 Luc E 的答案与我自己的一些知识相结合,以便在不重新索引的情况下找到解决方案。

POST INDEXNAME/TYPE/_update_by_query?wait_for_completion=false&conflicts=proceed
{
"script": {
    "source": "for (int i=0;i<ctx._source.result.length;i++) { ctx._source.result[i].remove(\"resultid\")}"
    },
"query": {
    "bool": {
      "must": [
        {
          "exists": {
            "field": "result.id"
          }
        }
      ]
    }
  }
}

再次感谢吕克!

如果您的数组中有多个要删除的元素副本。用这个: ctx._source.some_array.removeIf(tag -> tag == params['c'])