我有以下格式的索引:
"the debt": {"node": ["04j0t75", "0crxlv5"], "levenshtein": [100, 100]},
"nobuo": {"node": ["0ftqr"], "levenshtein": [56]},
"uematsu": {"node": ["0ftqr"], "levenshtein": [70]}
我想使用Python客户端将此索引发送到elasticsearch。我使用下面的代码片段from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk
es = Elasticsearch(hosts=[{"local": '9200'}])
test_index = json.loads(json.dumps(inverse_index))
actions = [
{
'_index': 'test_index',
'_type': 'application/json',
'_id': i,
'_source': json.loads('"{{{0}: {1}}}"'.format(k, v)),
}
for i, (k, v) in enumerate(test_index.items(), 1)
]
bulk(es, actions)
我得到以下错误:Unexpected character ('u' (code 117)): was expecting double-quote to start field name\n at [Source
我认为它希望我用双引号将键引起来,但我无法以某种方式做到这一点。你可以帮帮我吗?
提前致谢!
最佳答案
不知道为什么要格式化键值字符串,然后在actions
循环中json加载它...
代替
'_source': json.loads('"{{{0}: {1}}}"'.format(k, v))
做'_source': {k: v}
所以:actions = [
{
'_index': 'test_index',
'_type': 'application/json',
'_id': i,
'_source': {k: v},
}
for i, (k, v) in enumerate(test_index.items(), 1)
]
关于python-3.x - 如何批量发送字典到elasticsearch?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62572305/