python 3实现分页显示从es中获取的数据方法

2018-12-2615:55:36后端程序开发Comments3,359 views字数 759阅读模式
python 实现分页显示从es中获取的数据方法。注意:使用该方法,获取的数据总数目不能超过1万,否则出错!
#在python3上运行
from elasticsearch import Elasticsearch
from urllib3.connectionpool import xrange

def get_page_data(result):
  for hit in result['hits']['hits']:
    print(hit)

if __name__=='__main__':
  es_host = "0.0.0.0"
  port = 9200
  timeout = 15000
  index = "gather-v10"
  es = Elasticsearch(hosts=es_host,port=port,timeout=timeout)
  # gather-v10 总条数
  count = es.count(index=index)['count']
  # 每页显示条数
  page_line = 2
  #显示多少页
  if (count%page_line==0):
    page = (int)(count/page_line)
  else:
    page = (int)(count/page_line+1)
  # 要生成很大的数字序列的时候,
  # 用xrange会比range性能优很多,
  # 因为不需要一上来就开辟一块很大的内存空间。
  # x = range(0,10);type(x) 是一个列表
  # x1 = xrange(0,10);type(x1) 是一个生成器 xrange(0,10)
  for x in xrange(0,page):
    rs = es.search(index=index,body={
      "query":{
        "match_all":{}
      },
      "from":x*page_line,
      "size":page_line
    })
    get_page_data(rs)
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9084.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/9084.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定