0

I would like to export my elastic search data that is being returned from my query to a CSV file. I have been looking everywhere to do this but all my attemps on this is failing on how to do it.

my query is below;

import elasticsearch
import unicodedata
import csv

es = elasticsearch.Elasticsearch(["9200"])
# this returns up to 100 rows, adjust to your needs

res = es.search(index="search", body=
{
    "_source": ["CN", "NCR", "TRDT"],

    "query": {
                        "bool": {
                            "should": [
                                {"wildcard": {
                                        "CN": "TEST*"
                                    }
                                }]
                        }
                    }},size=10)

sample = res['hits']['hits']

# then open a csv file, and loop through the results, writing to the csv
with open('outputfile.tsv', 'wb') as csvfile:

    filewriter = csv.writer(csvfile, delimiter='\t',  # we use TAB delimited, to handle cases where freeform text may have a comma
                        quotechar='|', quoting=csv.QUOTE_MINIMAL)
    # create column header row
    filewriter.writerow(["CN", "NCR", "TRDT"])    #change the column labels here
    # fill columns 1, 2, 3 with your data
    col1 = hit["some"]["deeply"]["nested"]["field"].decode('utf-8')  #replace these nested key names with your own
    col1 = col1.replace('\n', ' ')
    # col2 = , col3 = , etc...
    for hit in sample:
        filewriter.writerow(col1)

could someone fix this code up for it to work please, been spending hours on this to make it work.

the error i am getting when running this is below:

Traceback (most recent call last):
  File "C:/Users/.PyCharmCE2017.2/config/scratches/trtr.py", line 30, in <module>
    filewriter.writerow(["CN", "NCR", "TRDT"])  # change the column labels here
TypeError: a bytes-like object is required, not 'str'

thank you in advance.

1 Answer 1

0

Replace wb with w in with open('outputfile.tsv', 'wb') since you are using Python 3.x

Sign up to request clarification or add additional context in comments.

1 Comment

thank you, that fixed that issue but now im getting another, which is: Traceback (most recent call last): File "C:/Users/.PyCharmCE2017.2/config/scratches/trtr.py", line 32, in <module> col1 = sample["some"]["deeply"]["nested"]["field"].decode('utf-8') # replace these nested key names with your own TypeError: list indices must be integers or slices, not str

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.