Elasticsearch 滚动获取数据 Scroll参数

声明:本博客根据ES官方文档6.3版本内容翻译总结整理而成,转载请注明出处:https://blog.csdn.net/qingmou_csdn

 

前期准备:创建索引、写入数据

DELETE gaoyh

PUT gaoyh
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  },
  "mappings": {
    "gtest":{
      "properties": {
        "user":{
          "type": "text",
          "fields": {
            "raw":{
              "type": "keyword"
            }
          }
        },
        "interest":{
          "type": "keyword",
          "doc_values":false,
          "norms": false
        },
        "create_time":{
          "type":"date"
        }
      }
    }
  }
}

POST gaoyh/gtest/_bulk
{"index":{"_id":"1"}}
{"user":"tom","interest":"music","create_time":"2018-07-17"}
{"index":{"_id":"2"}}
{"user":"kit","interest":"sport","create_time":"2018-07-17"}
{"index":{"_id":"3"}}
{"user":"nio","interest":"climb","create_time":"2018-07-17"}

为了使用滚动,初始搜索请求应在查询字符串中指定参数scroll,该参数告诉Elasticsearch应该将“搜索上下文” 保持活动多长时间(请参阅保持搜索上下文保持活动状态),例如?scroll=1m(保持活动1分钟) 。

 

GET gaoyh/_search?scroll=1m
{
  "query": {
    "match_all": {}
  },
  "size": 1
}

上述请求结果包括_scroll_id,将其传递给scroll API以检索下一批结果。

 

响应结果如下:

{
  "_scroll_id": "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAOBG8WY2RUOHJfWVhUbHVjenR1ajktcnAzZw==",
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "gaoyh",
        "_type": "gtest",
        "_id": "1",
        "_score": 1,
        "_source": {
          "user": "tom",
          "interest": "music",
          "create_time": "2018-07-17"
        }
      }
    ]
  }
}

POST _search/scroll
{
  "scroll":"1m",
 "scroll_id":"DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAOBG8WY2RUOHJfWVhUbHVjenR1ajktcnAzZw=="
}

可以使用GET 或 POST ;

URL不应包含 index 或 type 名称 --- 这些是在原始search 请求中指定的;

"scroll":"1m" 该参数告诉Elasticsearch保持搜索上下文活动对另一个开放1m;

 

{
  "_scroll_id": "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAOBG8WY2RUOHJfWVhUbHVjenR1ajktcnAzZw==",
  "took": 1,
  "timed_out": false,
  "terminated_early": true,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "gaoyh",
        "_type": "gtest",
        "_id": "2",
        "_score": 1,
        "_source": {
          "user": "kit",
          "interest": "sport",
          "create_time": "2018-07-17"
        }
      }
    ]
  }
}

{
  "_scroll_id": "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAOBwwWY2RUOHJfWVhUbHVjenR1ajktcnAzZw==",
  "took": 1,
  "timed_out": false,
  "terminated_early": true,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "gaoyh",
        "_type": "gtest",
        "_id": "3",
        "_score": 1,
        "_source": {
          "user": "nio",
          "interest": "climb",
          "create_time": "2018-07-17"
        }
      }
    ]
  }
}

size参数允许您配置每批结果返回的最大命中数。每次调用scrollAPI都会返回下一批结果,直到没有剩余的结果返回,即 hits数组为空。如下所示:

{
  "_scroll_id": "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAOCwcWY2RUOHJfWVhUbHVjenR1ajktcnAzZw==",
  "took": 1,
  "timed_out": false,
  "terminated_early": true,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": []
  }
}

可以使用以下_all参数清除所有搜索上下文:

DELETE /_search/scroll/_all

{
  "succeeded": true,
  "num_freed": 3
}

 

 

你可能感兴趣的:(ElasticSearch)