1、实战问题
问题1:es 中 scroll 游标不能与 collapse 共用,有什么办法可以实现 collapse 的效果? 问题2:采集互联网数据有重复数据写入 Elasticsearch,如何去重后放到另外索引?
2、排查思路
2.1 Elasticsearch 去重的几种方式
方式一:terms 指定字段聚合 + top_hits 子聚合。 方式二:collapse 折叠去重。
PUT news/_bulk{"index":{"_id":1}}{"title":"南开录取通知书亮相,附赠嘉兴莲花种子、一封特殊的信","cont":"今天,南开大学晒出建党百年特别版录取通知书。","url":"https://baijiahao.baidu.com/s?id=1703334751082094750&wfr=spider&for=pc","publish_time":"2021-06-23 13:36"}{"index":{"_id":2}}{"title":"南开录取通知书亮相,附赠嘉兴莲花种子、一封特殊的信","cont":"今天,南开大学晒出建党百年特别版录取通知书。","url":"https://baijiahao.baidu.com/s?id=1703334751082094750&wfr=spider&for=pc","publish_time":"2021-06-23 13:36"}{"index":{"_id":3}}{"title":"南开录取通知书亮相,附赠嘉兴莲花种子、一封特殊的信","cont":"今天,南开大学晒出建党百年特别版录取通知书。","url":"https://baijiahao.baidu.com/s?id=1703334751082094750&wfr=spider&for=pc","publish_time":"2021-06-23 13:36"}{"index":{"_id":4}}{"title":"建党百年特别版!南开大学录取通知书送两粒嘉兴莲花种子","cont":"@南开大学 6月23日消息,建党百年特别版南开大学录取通知书揭秘!","url":"https://www.163.com/dy/article/GD69KNIR0514R9P4.html","publish_time":"2021-06-23 13:25:00"}# top_hits 子聚合去重GET news/_search{ "query": { "match_all": {} }, "aggs": { "type": { "terms": { "field": "title.keyword", "size": 10 }, "aggs": { "title_top": { "top_hits": { "_source": { "includes": [ "title" ] }, "sort": [ { "title.keyword": { "order": "desc" } } ], "size": 1 } } } } }, "size": 0}# collapse 去重GET news/_search{ "query": { "match_all": {} }, "collapse": { "field": "title.keyword" }} 复制
2.2 Elasticsearch scroll 不支持 collapse 确认

2.3 考虑新方案

数据源:爬虫采集互联网数据(由于是采集数据,难免会有转载等重复数据)。 源数据存储:Mysql。 如何界定重复?基于:发文标题、发文时间、发文正文内容、发文url 组成字段的MD5值作为去重标记。
3、logstash fingerprint filter 插件介绍

3.1 fingerprint filter 插件版本
Versioned plugin documentation is not available for plugins released prior to Logstash 6.0. 复制
3.2 fingerprint filter 插件用途

4、logstash fingerprint filter 去重实战
4.1 同步脚本
input { # Read all documents from Elasticsearch elasticsearch { hosts => "172.21.0.14:19022" index => "news" query => '{ "sort": [ "_doc" ] }' }}filter { fingerprint { key => "1234ABCD" method => "SHA256" source => ["title", "cont", "url", "publish_time"] target => "[@metadata][generated_id]" concatenate_sources => true }}output { stdout { codec => dots } elasticsearch { hosts => "172.21.0.14:19022" index => "news_after_fingerprint" document_id => "%{[@metadata][generated_id]}" }} 复制
4.1.1 脚本讲解
第一:input,代表输入(读取端),本实例自然是基于 Elasticsearch 读。 第二:filter,代表中间处理,那就是指纹处理部分。 第三:output,代表输出(写入端),本实例还是写入 Elasticsearch,只不过会写入新的索引 news_after_fingerprint。
4.1.2 filter 环节核心参数讲解
key => "1234ABCD",代表当前指纹的唯一值。 method => "SHA256",指纹生成方式。 source => ["title", "cont", "url", "publish_time"],生成指纹的基础字段名称。 target => "[@metadata][generated_id]":将存储生成的指纹的字段的名称,后面output 环节会使用。该字段的任何当前内容都将被覆盖。 concatenate_sources => true
4.2 同步实操

4.3 成功标记


5、小结
问题1 答案:不用 collapse,用 fingerprint filter 插件将数据转存为另外索引,然后 scroll 遍历输出就可以。 问题2 答案:用 fingerprint filter 插件将数据转存为另外索引即可。
能将去重后的数据独立存储为一个索引,且无需额外操作。 方便业务单独处理数据。
参考

文章转载自铭毅天下Elasticsearch,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
2025年4月中国数据库流行度排行榜:OB高分复登顶,崖山稳驭撼十强
墨天轮编辑部
1777次阅读
2025-04-09 15:33:27
2025年3月国产数据库大事记
墨天轮编辑部
847次阅读
2025-04-03 15:21:16
2025年3月国产数据库中标情况一览:TDSQL大单622万、GaussDB大单581万……
通讯员
587次阅读
2025-04-10 15:35:48
征文大赛 |「码」上数据库—— KWDB 2025 创作者计划启动
KaiwuDB
492次阅读
2025-04-01 20:42:12
数据库,没有关税却有壁垒
多明戈教你玩狼人杀
475次阅读
2025-04-11 09:38:42
国产数据库需要扩大场景覆盖面才能在竞争中更有优势
白鳝的洞穴
453次阅读
2025-04-14 09:40:20
最近我为什么不写评论国产数据库的文章了
白鳝的洞穴
394次阅读
2025-04-07 09:44:54
阿里云 Elasticsearch Serverless 检索增强型 8.17 版来袭!
阿里云大数据AI技术
354次阅读
2025-04-18 10:24:15
天津市政府数据库框采结果公布!
通讯员
351次阅读
2025-04-10 12:32:35
【活动】分享你的压箱底干货文档,三篇解锁进阶奖励!
墨天轮编辑部
344次阅读
2025-04-17 17:02:24