Transfer csv to elasticsearch from Python

wanted to transfer following csv to elsticsearch

|hcode|hname|
|1|aaaa|
|2|bbbbb|
|3|ccccc|
|4|dddd|
|5|eeee|
|6|ffff|

and need to insert hcode field as document_id. getting below error
elasticseach version is 7.1.1

python vervion is 3.7.6

  File "C:\Users\Namali\Anaconda3\lib\site-packages\elasticsearch\connection\base.py", line 181, in _raise_error
    status_code, error_message, additional_info

RequestError: RequestError(400, 'mapper_parsing_exception', 'failed to parse')"

Python code-----------------------------------------------------------------

import csv
import json

from elasticsearch import Elasticsearch

es = Elasticsearch([{'host': 'localhost', 'port': 9200}])

def csv_reader(file_obj, delimiter=','):
   reader_ = csv.reader(file_obj,delimiter=delimiter,quotechar='"')
   
   i = 1
   results = []
   for row in reader_:
    #try :
    #es.index(index='hb_hotel_raw', doc_type='hb_hotel_raw', id=row[0], 
                # body=json.dump([row for row in reader_], file_obj))
    es.index(index='test', doc_type='test', id=row[0],body=json.dumps(row))
    #except:
    #    print("error")
    i = i + 1
    results.append(row)
    print(row)

if __name__ == "__main__":
  with open("D:\\namali\\rez\\data_mapping\\test.csv") as f_obj:
    csv_reader(f_obj)

Please format your code, logs or configuration files using </> icon as explained in this guide and not the citation button. It will make your post more readable.

Or use markdown style like:

```
CODE
```

This is the icon to use if you are not using markdown format:

There's a live preview panel for exactly this reasons.

Lots of people read these forums, and many of them will simply skip over a post that is difficult to read, because it's just too large an investment of their time to try and follow a wall of badly formatted text.
If your goal is to get an answer to your questions, it's in your interest to make it as easy to read and understand as possible.
Please update your post.

Then, could you share what is actually sent to elasticsearch? What is the json content?

Ideally, if you could transform the python code to just elasticsearch http requests that can be executed in Kibana dev console, that'd make things easier to diagnose your problem. (I don't write python code).

Hi David.. thx and updated according to your advice

Great. I also asked for this:

Ideally, if you could transform the python code to just elasticsearch http requests that can be executed in Kibana dev console, that'd make things easier to diagnose your problem. (I don't write python code).

I speak a bit of python and the problem is probably here. For a row this would create a string like this:

[ "1", "aaaa"]

Which is an array of values not a dict. Normally you'd build and send a dict using something like this :

myDoc =  {
   "code" : row[0],
   "name": row[1]
}

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.