使用python查詢Elasticsearch並導出全部數據

import csv
from elasticsearch import Elasticsearchjson

# 查看參數配置: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)elasticsearch

results = query['hits']['hits'] # es查詢出的結果第一頁
total = query['hits']['total']  # es查詢出的結果總量
scroll_id = query['_scroll_id'] # 遊標用於輸出es查詢出的全部結果utf-8

for i in range(0, int(total/100)+1):
    # scroll參數必須指定不然會報錯
    query_scroll = es.scroll(scroll_id=scroll_id,scroll='5m')['hits']['hits']
    results += query_scrollit


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']])event


print('done!')
# print(es.info())
 ast

相關文章
相關標籤/搜索