在 Elasticsearch 中搜索具有相同值的文档

Search for documents with the same value in Elasticsearch

我有一个看起来像这样的模式:

{
  "mappings": {
    "entity": {
      "properties": {
      "a": {
        "type": "text"
      },
      "b": {
        "type": "text"
      }
    }
  }

我想找到 b 的所有值,其中 a 的值由 2 个或多个实体共享:

查询对象:

[{"a": "a1", "b": "b1"}, 
 {"a": "a1", "b": "b2"}, 
 {"a": "a2", "b": "b3"}]

应该returnb1b2.

您可以对 a 字段进行 terms 聚合,min_doc_count 为 2,然后添加 top_hits 子聚合以找到匹配的 b 字段:

{
  "size": 0,
  "aggs": {
    "dups": {
      "terms": {
        "field": "a",
        "min_doc_count": 2
      },
      "aggs": {
        "b_hits": {
          "top_hits": {
            "_source": "b"
          }
        }
      }
    }
  }
}