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

December 17, 2023
测试
测试
测试
测试
4 分钟阅读
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())
 

继续阅读

更多来自我们博客的帖子

如何安装 BuddyPress
由 测试 December 17, 2023
经过差不多一年的开发,BuddyPress 这个基于 WordPress Mu 的 SNS 插件正式版终于发布了。BuddyPress...
阅读更多
Filter如何工作
由 测试 December 17, 2023
在 web.xml...
阅读更多
如何理解CGAffineTransform
由 测试 December 17, 2023
CGAffineTransform A structure for holding an affine transformation matrix. ...
阅读更多