Home > OS >  get results with the same field in elasticsearch
get results with the same field in elasticsearch

Time:12-26

I am trying to get documents that the value of the "name" field repeats in 2 documents. how can I accomplished that?

Data example:

{
"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"
},

Desired output:

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

CodePudding user response:

You can do it like this with a terms aggregation and a top_hits sub-aggregation:

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