Export Elasticsearch results into a CSV file

穿精又带淫゛_ 提交于 2019-12-11 07:24:58

问题


I am trying to export the results that is found using the below query into a CSV on my desktop.

This is my first time using Elasticsearch and cURL so i am confused on how to do this.

from elasticsearch import Elasticsearch

es = Elasticsearch(["9200"])

# Replace the following Query with your own Elastic Search Query
res = es.search(index="search", body=
                {
                    "_source": ["DTDT", "TRDT", "SPLE", "RPLE"],
                    "query": {
                        "bool": {
                            "should": [
                                {"wildcard": {"CN": "TEST1"}}

                            ]
                        }
                    }
}, size=10)

for doc in res['hits']['hits']:
    print(doc)

right now when i run this query it returns the name, lastname, address and gender for dave and i want to put the results into a csv on my desktop when i run the query.

i have been reading this link on how to do it but im not sure how to make my query do this - (https://docs.python.org/3/library/csv.html)

could someone help and show me how to convert my query in exporting a csv PLEASE!

thanks

the output i get is -

{'_index': 'search', '_type': 'trades', '_id': '179299804977823744', '_score': 1.0, '_source': {'DTDT': '20170928', 'SPLE': '1001', 'RPLE': '1001', 'TRDT': '2017-09-28 17:01:19'}}

回答1:


You can use csv module to write data.

From the output you have given, I am assuming that you want to write data from _source to csv file.

Code :

from elasticsearch import Elasticsearch
import csv

es = Elasticsearch(["9200"])

# Replace the following Query with your own Elastic Search Query
res = es.search(index="search", body=
                {
                    "_source": ["DTDT", "TRDT", "SPLE", "RPLE"],
                    "query": {
                        "bool": {
                            "should": [
                                {"wildcard": {"CN": "TEST1"}}

                            ]
                        }
                    }
}, size=10)



with open('mycsvfile.csv', 'w') as f:  # Just use 'w' mode in 3.x
    header_present  = False
    for doc in res['hits']['hits']:
        my_dict = doc['_source'] 
        if not header_present:
            w = csv.DictWriter(f, my_dict.keys())
            w.writeheader()
            header_present = True


        w.writerow(my_dict)


来源:https://stackoverflow.com/questions/46682018/export-elasticsearch-results-into-a-csv-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!