如何在弹性搜索查询中动态分页



我正在尝试通过聚合从弹性搜索中删除记录,我使用的查询是这个

{
"aggs": {
"countfield": {
"terms": {
"field": "IaClaimChargeID",
"size": 100,
"order": {
"_count": "desc"
},
"min_doc_count": 1
},
"aggs": {
"text": {
"top_hits": {
"size": 1,
"_source": {
"includes": [
"ChargeAmount"
]
}
}
}
}
}
}
}

现在我正在做的是删除doc_count为>1 的所有文档。问题是大约有100万条记录,我无法在单个查询中获取所有记录。有没有解决方案,我可以给出像 1000 -5000 这样的分页大小,或者是否有更好的解决方案来实现这一点。

我正在做的是获取doc_count,然后运行一个删除查询,其中 doc_count>1 ,用于输出中的值。

这是输出

"buckets" : [
{
"key" : "$455512-Apr-09", // deleting by this key
"hits" : []
doc_count" : 1,
"text" : {
"hits" : {
"total" : 1,
"max_score" : 1.0,
}]

Elasticsearch 有一个端点,可以按特定条件删除记录,因此您可以使用_delete_by_query端点。

POST /put_your_index_name_goes_here/_delete_by_query
{
"query": {
"match": {
"doc_count": 1
}
}
}

参见参考文献 https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html

您可以通过提供fromsize参数来像这样分页

{
"from" : 0, "size" : 10, 
"aggs": {
"countfield": {
"terms": {
"field": "IaClaimChargeID",
"size": 100,
"order": {
"_count": "desc"
},
"min_doc_count": 1
},
"aggs": {
"text": {
"top_hits": {
"size": 1,
"_source": {
"includes": [
"ChargeAmount"
]
}
}
}
}
}
}
}

你可以看这里

此外,带有 from 的size与您在聚合中给出的大小不同,聚合中的大小表示存储桶大小。而最上面的大小是指文档的数量(其默认值为 10(

最新更新