[英]How to store key-value pairs in an elasticsearch index using python
我想将dict的内容存储到elasticsearch索引中,如下所示。 这是正确的还是有更好的方法来做到这一点。
def process(self, inputDict):
for k, v in inputDict.items():
# for each key-value pair, store it as a field and string inside the specified index of elastic search.
key1=k
value1=v
doc={
"key1" : "value" ,
}
self.es.index(index='test-index2',doc_type='exdoc', id=1, body=doc)
pass;
首先:你尝试过代码吗? 我试了一下,它运行没有错误。 这意味着索引test-index2
必定有一些文档。 在空闲状态下运行我得到以下输出:
{'_index': 'test-index2', '_type': 'exdoc', '_id': '1', '_version': 1, '_shards': {'failed': 0, 'total': 2, 'successful': 1}, 'created': True}
{'_index': 'test-index2', '_type': 'exdoc', '_id': '1', '_version': 2, '_shards': {'failed': 0, 'total': 2, 'successful': 1}, 'created': False}
{'_index': 'test-index2', '_type': 'exdoc', '_id': '1', '_version': 3, '_shards': {'failed': 0, 'total': 2, 'successful': 1}, 'created': False}
看那里的_version
字段? 这看起来很可疑。 从意义上查询弹性搜索
GET test-index2/exdoc/_search
{
"query": {
"match_all": {}
}
}
会给你以下输出:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "test-index2",
"_type": "exdoc",
"_id": "1",
"_score": 1,
"_source": {
"key1": "value"
}
}
]
}
}
那里只有一个文件: {"key1": "value"}
。 所以你总是发送相同的文档 - 忽略inputDict
的键和值 - 对于相同的id( id=1
)到elasticsearch。 我觉得你想要这样的东西:
def process(self, inputDict):
i = 1
for k, v in inputDict.items():
# for each key-value pair, store it as a field and string inside the specified index of elastic search.
doc={
k: v
}
self.es.index(index='test-index2',doc_type='exdoc', id=i, body=doc)
i += 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.