elasticsearch基于字段查询重复数据
terms聚合
要查completed_product_xxx索引里product_id重复的数据,用ES DSL可以直接用terms聚合:
1 | GET /completed_product_xxx/_search |
这里的
size:10000不是查询文档数量10000,而是控制terms聚合最终返回多少个product_id桶。
返回结果里的:
1 | "buckets": [ |
就表示product_id=2009398068101774300出现了2次。
把重复的完整文档查出来
可以用terms+top_hits:
1 | GET /completed_product_xxx/_search |
如果product_id是text类型,需要改成:
1 | "field": "product_id.keyword" |
如果只想查product_id=2009398068101774300是否重复
更简单:
1 | GET /completed_product_10026_xxx/_search |
size区别
| DSL位置 | size含义 |
|---|---|
_search下面的"size":0 |
返回0条原始文档 |
_search下面的"size":10000 |
最多返回10000条原始文档 |
terms下面的"size":10000 |
最多返回10000个不同的聚合桶 |
1 | "size": 0 |
+:
1 | "terms": { |
扫描/聚合索引中的数据,找出重复的
product_id,但最终最多返回10000个重复的product_id。