如何在ElasticSearch中找到所有重复的文档

iyfjxgzm  于 2023-04-29  发布在  ElasticSearch
关注(0)|答案(4)|浏览(154)

我们需要遍历AWS ElasticSearch集群版本6中的所有文档。0,并收集所有重复用户ID的计数。我曾尝试使用数据可视化来汇总用户ID的计数并导出它们,但这些数字与通过传统SQL搜索的另一个数据源不匹配。我们希望看到的是这样的:用户ID COUNT userid1 4 userid22 3 ...我不是一个先进的Lucene查询人,还没有找到这个问题的答案。如果有人能提供一些见解如何做到这一点,我将不胜感激。

prdp8dxp

prdp8dxp1#

下面的查询将对每个id进行计数,并过滤计数〈2的id,这样你就可以得到如下的结果:
ID:2,计数:2
id:4,count:15

GET /index
{
    "query":{
        "match_all":{}
    },
    "aggs":{
        "user_id":{
            "terms":{
                "field":"user_id",
                "size":100000,
                "min_doc_count":2
            }
        }
    }
}

更多信息:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html

odopli94

odopli942#

如果要获取所有重复的userid,请使用count
首先,你要知道aggs的最大大小。通过aggs基数查找所有最大匹配记录。

GET index/type/_search
{
   "size": 0,
   "aggs": {
      "maximum_match_counts": {
         "cardinality": {
            "field": "userid",
            "precision_threshold": 100
         }
      }
   }
}

获取maximum_match_counts聚合值
现在您可以获取所有重复的userid

GET index/type/_search
{
   "size": 0,
   "aggs": {
      "userIds": {
         "terms": {
            "field": "userid",
            "size": maximum_match_counts,
            "min_doc_count": 2
         }
      }
   }
}
byqmnocz

byqmnocz3#

当您使用术语聚合(Bharat建议)并将聚合大小设置为超过10 K时,您将得到一个警告,说明此方法将为功能版本抛出错误。
你应该使用composite aggregation通过pagination/afterkey方法扫描所有文档,而不是使用术语聚合。
复合聚集可用于有效地对来自多级聚集的所有桶进行分页。这种聚合提供了一种方法来流式传输特定聚合的所有桶,类似于滚动对文档所做的。

idv4meu8

idv4meu84#

GET /index

{
    "query":{
        "match_all":{}
    },
    "aggs":{
        "user_id":{
            "terms":{
                "field":"user_id",
                "size":65536,
                "min_doc_count":2
            }
        }
    }
}

this one is working perfectly, i have found issues in lot of end point ES url's using this query, this will help you in post man, postman will display an error if you run the query with size 100000,so better you can use size=65536, and you have to replace the  "field":"user_id" name as well,it should be your field name

相关问题