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

2024-05-17 05:42:17 发布

您现在位置:Python中文网/ 问答频道 /正文

这里是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"}]

下面是我用来导入json变量的命令

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

以下是错误

---------------------------------------------------------------------------
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

Tags: nameinidcityageindexdoces
3条回答

doc很可能是不可调用的字符串。通常jsonout听起来不应该有函数。你知道吗

您的问题中缺少某些内容-在错误示例中,您有以下代码:

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')

在第1行中有docs变量,但在第2行中有jsonout。 而且,如果将docs变量而不是jsonout放入第二行,则会出现类似'dict' object is not callable的错误,因为您有doc('id')(而不是doc['id']),并且doc是一个字典。你知道吗

所以我怀疑实际的jsonout变量值也有问题——它可能是字符串列表而不是字典列表。你知道吗

终于找到了解决办法。你知道吗

我们可以用json.loads文件将str对象转换为json对象。你知道吗

你知道吗json.loads文件(jsonout公司)

相关问题 更多 >