从Elasticsearch中的搜索中删除重复的文档

时间:2014-08-22 13:27:05

标签: elasticsearch deduplication

我的索引包含大量纸张,同一个字段的值相同。我在这个领域有一个重复数据删除。

聚合器将作为计数器来找我。我想要一份文件清单。

我的索引:

  • 文档1 {域名:' domain1.fr',名称:' name1',日期:' 01-01-2014'}
  • Doc 2 {domain:' domain1.fr',name:' name1',date:' 01-02-2014'}
  • 文档3 {域名:' domain2.fr',名称:' name2',日期:' 01-03-2014'}
  • Doc 4 {domain:' domain2.fr',name:' name2',date:' 01-04-2014'}
  • Doc 5 {domain:' domain3.fr',name:' name3',date:' 01-05-2014'}
  • Doc 6 {domain:' domain3.fr',name:' name3',date:' 01-06-2014'}

我想要这个结果(域名重复数据删除结果):

  • Doc 6 {domain:' domain3.fr',name:' name3',date:' 01-06-2014'}
  • Doc 4 {domain:' domain2.fr',name:' name2',date:' 01-04-2014'}
  • Doc 2 {domain:' domain1.fr',name:' name1',date:' 01-02-2014'}

1 个答案:

答案 0 :(得分:29)

您可以使用field collapsing,将结果分组到name字段,并将top_hits聚合器的大小设置为1。

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

返回:

{
  "took" : 192,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "hits" : {
    "total" : 6,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "dedup" : {
      "buckets" : [ {
        "key" : "name1",
        "doc_count" : 2,
        "dedup_docs" : {
          "hits" : {
          "total" : 2,
          "max_score" : 1.0,
          "hits" : [ {
            "_index" : "test",
            "_type" : "dedup",
            "_id" : "1",
            "_score" : 1.0,
            "_source":{domain: "domain1.fr", name: "name1", date: "01-01-2014"}
          } ]
        }
      }
    }, {
      "key" : "name2",
      "doc_count" : 2,
      "dedup_docs" : {
        "hits" : {
          "total" : 2,
          "max_score" : 1.0,
          "hits" : [ {
            "_index" : "test",
            "_type" : "dedup",
            "_id" : "3",
            "_score" : 1.0,
            "_source":{domain: "domain1.fr", name: "name2", date: "01-03-2014"}
          } ]
        }
      }
    }, {
      "key" : "name3",
      "doc_count" : 2,
      "dedup_docs" : {
        "hits" : {
          "total" : 2,
          "max_score" : 1.0,
          "hits" : [ {
            "_index" : "test",
            "_type" : "dedup",
            "_id" : "5",
            "_score" : 1.0,
            "_source":{domain: "domain1.fr", name: "name3", date: "01-05-2014"}
           } ]
         }
       }
     } ]
   }
 }
}