Reputation: 177
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'}}
Upvotes: 2
Views: 13606
Reputation: 4196
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)
Upvotes: 4