【问题标题】:Remove duplicate documents from a search in Elasticsearch从 Elasticsearch 中的搜索中删除重复文档
【发布时间】:2014-10-16 09:02:36
【问题描述】:

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

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

我的索引:

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

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

  • 文档 6 {域:'domain3.fr',名称:'name3',日期:'01-06-2014'}
  • 文档 4 {域:'domain2.fr',名称:'name2',日期:'01-04-2014'}
  • 文档 2 {域:'domain1.fr',名称:'name1',日期:'01-02-2014'}

【问题讨论】:

  • 您要查找重复的文档并将其删除吗?或者从搜索结果中过滤它们?
  • 我想从搜索结果中过滤掉它们

标签: elasticsearch deduplication


【解决方案1】:

您可以使用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"}
           } ]
         }
       }
     } ]
   }
 }
}

【讨论】:

  • 感谢您的回答 :-)
  • 但是,如果我的字段值类似于 'eyrolles.com/Loisirs/Livre/couture-printemps-ete-9782756522081',我的存储桶术语是 'printemps'、'couture'、'9782756522081'... 术语聚合器按单词拆分 url...我不想拆分价值。
  • 这是一个不同的问题,您需要索引字段 not_analyzed 并在聚合中引用该字段。看看多字段类型:elasticsearch.org/guide/en/elasticsearch/reference/0.90/…
  • 有没有办法决定 ES 会选择哪一个?假设我有要在 field1 上折叠的文档,但是这些文档具有不同的 field2 值,并且我希望能够任意选择哪个?如果有帮助,在我的具体情况下,我想选择插入的最后一个。
  • 您在哪里删除了文档?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多