简体   繁体   English

使用python的弹性搜索导入错误

[英]Elastic search import error using python

Here is the json variable 这是json变量

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 我正在使用以下命令导入json变量

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. doc可能是不可调用的字符串。 Usually jsonout doesn't sound like it should have functions. 通常jsonout听起来不像它应该具有功能。

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. 你有docs的第1行的变量,但你jsonout在第2行而且,如果你把docs变量,而不是jsonout到第二行,你应该得到这样的错误'dict' object is not callable ,因为你具有doc('id') (而不是doc['id'] ),而doc是字典。

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. 因此,我怀疑您的实际jsonout变量值也有问题-它可能是字符串列表而不是字典列表。

Found the solution finally. 终于找到了解决方案。

We can use json.loads to convert str object to json object. 我们可以使用json.loads将str对象转换为json对象。

json.loads(jsonout) json.loads(jsonout)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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