简体   繁体   中英

Elastic search import error using python

Here is the json variable

jsonout = [{"city": "Springfield", "id": 1, "name": "Moes Tavern"}, {"city": "Springfield", "id": 2, "name": "Springfield Power Plant"}, {"city": "Fountain Lakes", "id": 3, "name": "Kath and Kim Pty Ltd"}]

The following command i am using to import json variable

es.bulk((es.index_op(doc, id=doc('id')) for doc in jsonout), index='dbmysql', doc_type='person')

The following is the error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-14-10faf5c5bb89> in <module>()
      1 docs = [{'id': 2, 'name': 'Jessica Coder', 'age': 32, 'title': 'Programmer'}, {'id': 3, 'name': 'Freddy Tester', 'age': 29, 'title': 'Office Assistant'}]
----> 2 es.bulk((es.index_op(doc, id=doc('id')) for doc in jsonout), index='dbmysql', doc_type='person')

d:\nvk\USER\Anaconda2\lib\site-packages\pyelasticsearch\client.pyc in decorate(*args, **kwargs)
     91                 elif k in convertible_args:
     92                     query_params[k] = kwargs.pop(k)
---> 93             return func(*args, query_params=query_params, **kwargs)
     94         return decorate
     95     return decorator

d:\nvk\USER\Anaconda2\lib\site-packages\pyelasticsearch\client.pyc in bulk(self, actions, index, doc_type, query_params)
    445         response = self.send_request('POST',
    446                                      [index, doc_type, '_bulk'],
--> 447                                      body='\n'.join(actions) + '\n',
    448                                      query_params=query_params)
    449 

<ipython-input-14-10faf5c5bb89> in <genexpr>((doc,))
      1 docs = [{'id': 2, 'name': 'Jessica Coder', 'age': 32, 'title': 'Programmer'}, {'id': 3, 'name': 'Freddy Tester', 'age': 29, 'title': 'Office Assistant'}]
----> 2 es.bulk((es.index_op(doc, id=doc('id')) for doc in jsonout), index='dbmysql', doc_type='person')

TypeError: 'str' object is not callable

doc is likely the uncallable string. Usually jsonout doesn't sound like it should have functions.

You are missing something in your question - in the error example you have this code:

1 docs = [{'id': 2, 'name': 'Jessica Coder', ...}, {...}]
2 es.bulk((es.index_op(doc, id=doc('id')) for doc in jsonout), index='dbmysql', doc_type='person')

You have the docs variable in the line 1, but you have jsonout in the line 2. And, if you put the docs variable instead of jsonout into the second line, you should get an error like 'dict' object is not callable because you have doc('id') (instead of doc['id'] ) and doc is a dictionary.

So I suspect that also something is wrong with your actual jsonout variable value - it is probably a list of strings instead of list of dictionaries.

Found the solution finally.

We can use json.loads to convert str object to json object.

json.loads(jsonout)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM