比较elasticsearch中两个索引之间的ID

Beo*_*ulf 3 elasticsearch

我在 elasticsearch 集群中有两个索引,其中包含两种略有不同格式的相同数据。但是,记录的数量是不同的。每个文档的 ID 应该相同。有没有办法提取一个索引中存在哪些 ID 而另一个索引中不存在的 ID 列表?

And*_*fan 5

如果您的两个索引在存储这些文档的位置具有相同的类型,您可以使用以下内容:

GET index1,index2/_search
{
  "size": 0,
  "aggs": {
    "group_by_uid": {
      "terms": {
        "field": "_uid"
      },
      "aggs": {
        "count_indices": {
          "cardinality": {
            "field": "_index"
          }
        },
        "values_bucket_filter_by_index_count": {
          "bucket_selector": {
            "buckets_path": {
              "count": "count_indices"
            },
            "script": "params.count < 2"
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

上面的查询适用于 5.x。如果您的 ID 是文档中的一个字段,则最好进行测试。

  • 对于 ES7,我只需要将“_uid”更改为“_id” (2认同)