前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用python查询Elasticsearch并导出所有数据 转

使用python查询Elasticsearch并导出所有数据 转

作者头像
双面人
发布2019-08-31 13:51:01
3.6K0
发布2019-08-31 13:51:01
举报
文章被收录于专栏:热爱IT热爱IT
代码语言:javascript
复制
import csv
 from elasticsearch import Elasticsearch
# 查看参数配置:https://pypi.org/project/elasticsearch/
 es = Elasticsearch(hosts="http://192.168.21.33:9200/", http_auth=('abc','dataanalysis'))
 query_json = {
   "_source": "title",
   "query": {
     "bool": {
       "must": [
         {"match_phrase": {
           "content": "汽车"
         }},
         {"match_phrase": {
           "content": "房子"
         }}
       ]
     }
   }
 }
 query = es.search(index='1485073708892',body=query_json,scroll='5m',size=100)
results = query['hits']['hits'] # es查询出的结果第一页
 total = query['hits']['total']  # es查询出的结果总量
 scroll_id = query['_scroll_id'] # 游标用于输出es查询出的所有结果
for i in range(0, int(total/100)+1):
     # scroll参数必须指定否则会报错
     query_scroll = es.scroll(scroll_id=scroll_id,scroll='5m')['hits']['hits']
     results += query_scroll

 with open('./data/event_title.csv','w',newline='',encoding='utf-8') as flow:
     csv_writer = csv.writer(flow)
     for res in results:
         # print(res)
         csv_writer.writerow([res['_id']+','+res['_source']['title']])

 print('done!')
 # print(es.info())
 
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档