美文网首页
es滚动查询

es滚动查询

作者: caster | 来源:发表于2020-11-23 09:57 被阅读0次

1.restfu查询方式:

首先设置滚动查询并设置超时时间

curl -H "Content-Type: application/json" -u root:111111 -XPOST '172.16.17.212:9201/t3/_search?pretty&scroll=1m' -d '{"sort":"t1"}'

返回的结果会带有这次滚动查询的scroll_id用于接下来的查询:

{
  "_scroll_id" : "DnF1ZXJ5VGhlbkZldGNoBgAAAAAAAAFJFlB5N0txeVRlVG5xTU9xUWFhTkIzTEEAAAAAAAABTBZQeTdLcXlUZVRucU1PcVFhYU5CM0xBAAAAAAAAAUoWUHk3S3F5VGVUbnFNT3FRYWFOQjNMQQAAAAAAAAFNFlB5N0txeVRlVG5xTU9xUWFhTkIzTEEAAAAAAAABSxZQeTdLcXlUZVRucU1PcVFhYU5CM0xBAAAAAAAAAU4WUHk3S3F5VGVUbnFNT3FRYWFOQjNMQQ==",
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 6,
    "successful" : 6,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 200,
    "max_score" : null,
    "hits" : [
      {
        "_index" : "t3_20190423",
        "_type" : "20190423",
        "_id" : "PpcISWoBwQxG9TTQoTR-",
        "_score" : null,
        "_source" : {
          "t1" : "1",
          "t2" : "1",
          "t3" : "caster",
          "t4" : "20190423181850"
        },
        "sort" : [
          1
        ]
      },
      {
        "_index" : "t3_20190423",
        "_type" : "20190423",
        "_id" : "R5cISWoBwQxG9TTQoTR_",
        "_score" : null,
        "_source" : {
          "t1" : "10",
          "t2" : "1",
          "t3" : "caster",
          "t4" : "20190423181850"
        },
        "sort" : [
          10
        ]
      }
    ]
  }
}

接下来的查询使用返回的id进行查询即可:

curl -H "Content-Type: application/json" -u root:111111 -XPOST '172.16.17.212:9201/_search/scroll?pretty' -d '{ "scroll" : "1m","scroll_id":"DnF1ZXJ5VGhlbkZldGNoBgAAAAAAAAFJFlB5N0txeVRlVG5xTU9xUWFhTkIzTEEAAAAAAAABTBZQeTdLcXlUZVRucU1PcVFhYU5CM0xBAAAAAAAAAUoWUHk3S3F5VGVUbnFNT3FRYWFOQjNMQQAAAAAAAAFNFlB5N0txeVRlVG5xTU9xUWFhTkIzTEEAAAAAAAABSxZQeTdLcXlUZVRucU1PcVFhYU5CM0xBAAAAAAAAAU4WUHk3S3F5VGVUbnFNT3FRYWFOQjNMQQ=="}'

2.SQL查询方式:

xpack.sql使用方式如下:通过fetch_size进行分页设置

curl -H "Content-Type: application/json" -u root:111111 -XPOST '172.16.17.213:9200/_xpack/sql?format=json' -d '{"query":"select * from  t1", "fetch_size": 5}'

返回结果如下:除了带有查询结果,还带有cursor用于后续查询

{
  "columns": [
    {
      "name": "t1",
      "type": "keyword"
    },
    {
      "name": "t2",
      "type": "keyword"
    },
    {
      "name": "t3",
      "type": "keyword"
    },
    {
      "name": "t4",
      "type": "long"
    },
    {
      "name": "t5",
      "type": "text"
    },
    {
      "name": "t6",
      "type": "geo_point"
    }
  ],
  "rows": [
    [
      "id_423159",
      "id_153005",
      "call-id_769148",
      3619942,
      "aerTv afWDg pdEcl Crgqt",
      "-60.50252303481102, -37.10943100042641"
    ],
    [
      "id_521510",
      "id_600941",
      "call-id_017549",
      4364815,
      "EHbcJ vmjbh XfDjD dhuPh",
      "-80.12339604087174, -55.92544407583773"
    ],
    [
      "id_300243",
      "id_616046",
      "call-id_092436",
      4852402,
      "bQOkk NOUsw lhiIX vQuAf",
      "16.380679984577, -24.128756001591682"
    ],
    [
      "id_786766",
      "id_382938",
      "call-id_171220",
      9182918,
      "sunlU eZMfK xnzkO QrsxL",
      "-41.530255023390055, -60.80054202117026"
    ],
    [
      "id_174869",
      "id_782048",
      "call-id_997480",
      9695688,
      "CVvyF SOrwQ UtWzG FWWsF",
      "30.78904597554356, -63.00599902868271"
    ]
  ],
  "cursor": "w6XxAgFz5AFEbkYxWlhKNVZHaGxia1psZEdOb0JRQUFBQUFBQUNDOEZsbHVVRmN5WmpOa1ZFdGhOMjVHY0dSa05HbzFkMEVBQUFBQUFBQWd2UlpaYmxCWE1tWXpaRlJMWVRkdVJuQmtaRFJxTlhkQkFBQUFBQUFBSU1BV1dXNVFWekptTTJSVVMyRTNia1p3WkdRMGFqVjNRUUFBQUFBQUFDQy1GbGx1VUZjeVpqTmtWRXRoTjI1R2NHUmtOR28xZDBFQUFBQUFBQUFndnhaWmJsQlhNbVl6WkZSTFlUZHVSbkJrWkRScU5YZEL/////DwYBZgJ0MQEHa2V5d29yZAEAAWYCdDIBB2tleXdvcmQBAAFmAnQzAQdrZXl3b3JkAQABZgJ0NAEEbG9uZwEAAWYCdDUBBHRleHQAAAFmAnQ2AQlnZW9fcG9pbnQBAA==",
  "total": 1100
}

后续查询使用cursor查询即可:

curl -H "Content-Type: application/json" -u root:111111 -XPOST '172.16.17.213:9200/_xpack/sql?format=json' -d '{ "cursor": ""}'

相关文章

网友评论

      本文标题:es滚动查询

      本文链接:https://www.haomeiwen.com/subject/ykemiktx.html