在ElasticSearch中得到相同字段的结果

zed5wv10  于 2022-12-26  发布在  ElasticSearch
关注(0)|答案(1)|浏览(171)

我正在尝试获取"名称"字段的值在2个文档中重复的文档。我该如何完成呢?
数据示例:

{
"name": "John",
"last_name": "Doe"
},
{
"name": "John",
"last_name": "Mark"
},
{
"name": "Jasmine",
"last_name": "llass"
},
{
"name": "Jonah",
"last_name": "Hill"
},
{
"name": "Leonardo",
"last_name": "DeC"
},

预期输出:

{
"name": "John",
"last_name": "Doe"
},
{
"name": "John",
"last_name": "Mark"
}
mdfafbf1

mdfafbf11#

您可以像这样使用terms聚合和top_hits子聚合:

{
  "size": 0,
  "aggs": {
    "names": {
      "terms": {
        "field": "name",
        "min_doc_count": 2
      },
      "aggs": {
        "docs": {
          "top_hits": {
            "size": 2
          }
        }
      }
    }
  }
}

相关问题