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

2019-08-31 13:51:01 浏览数 (1)

代码语言: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())
 

0 人点赞