elasticsearch - How to get total size of request query (not size in single response) in Elastic Search?
问题描述
For my use case, I would like to search and get all result with a query. I use scroll to get all result.
https://ip:p/key/_search?scroll=2m
(with the very complex query in json here .... i suppose)
Then I would consume scroll to get result until it error.
https://ip:p/_search/scroll
{
"scroll" : "2m",
"scroll_id" : "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAA5NAWLU51eGVMN29RajJRMmlubHV1eFdLdw=="
}
I would like to know how many total result, or how many consume scroll I would need to call until I get all result, to estimate the progress bar or remaining time.
解决方案
您可以使用 track_total_hits=true。它将返回文档总数。
GET index18/_search?scroll=1m&track_total_hits=true
{
"size": 2,
"query": {
"match_all": {}
}
}
结果:
"hits" : {
"total" : {
"value" : 4, --> note
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "index18",
"_type" : "_doc",
"_id" : "iSCe6nEB8J88APx3YBGn",
"_score" : 1.0,
"_source" : {
"field1" : "test",
"field2" : "test"
}
},
{
"_index" : "index18",
"_type" : "_doc",
"_id" : "iiCe6nEB8J88APx3ghF-",
"_score" : 1.0,
"_source" : {
"field1" : "test",
"field2" : "abc"
}
}
]
}
每个请求都要求 2 条记录,记录总数为 4 。所以在第一个请求中,将获取 50% 的数据
推荐阅读
- bash - 匹配来自不同列的两个文本文件中的字段,然后在找到匹配项的地方附加一个文件内容,包括重复项
- r - 在 R 中找不到函数“dbconnect”
- python-3.x - 无法获取属性位置 pyglet
- python - 在 python 中使用注释时出现语法错误
- c++ - LNK2019 未解析的外部符号 OOP 和多态性?
- sql - 如何根据条件按结果集从组中获取数据?
- python - 将 Python 2.7x 转换为 3.x 兼容性问题:格式和 subprocess.Popen、numpy.array 和浮点数的问题
- oracle - 一列中的多个值显示另一列中的单个值
- java - HttpURLConnection 发送带有自定义标头的 POST 的 401 响应代码
- javascript - 使用map和dataservice同步获取数据