小编典典

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

elasticsearch

我有一个看起来像这样的架构:

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

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

查询依据:

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

应该返回b1b2


阅读 382

收藏
2020-06-22

共1个答案

小编典典

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

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