首页 > 解决方案 > 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.

标签: elasticsearch

解决方案


您可以使用 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% 的数据


推荐阅读