Bastien Donjon

· Uncategorized

Remove duplicate documents from a search in Elasticsearch

If you have many documents with the same value of the same field, this source code can help you. Using terms aggregator and top hits aggregator is required.

My index :

My result (deduplication result by domain field) :

Elasticsearch query :

/POST http://localhost:9200/test/dedup/_search?search_type=count&pretty=true
"aggs":{
    "dedup" : {
      "terms":{
        "field": "domain"
       },
       "aggs":{
         "dedup_docs":{
           "top_hits":{
             "size":1
           }
         }
      }
    }
  }
}

I originally asked a question here.