解析Python中的Elasticsearch json输出



我正在分析Elasticsearch索引中的数据,并收到了如下json格式的数据:

{
"_shards": {
"failed": 0,
"skipped": 0,
"successful": 5,
"total": 5
},
"hits": {
"hits": [
{
"_id": "wAv4u2cB9qH5eo0Slo9O",
"_index": "homesecmum",
"_score": 1.0870113,
"_source": {
"image": "0000000028037c08_1544283640.314629.jpg"
},
"_type": "dataRecord"
},
{
"_id": "wwv4u2cB9qH5eo0SmY8e",
"_index": "homesecmum",
"_score": 1.0870113,
"_source": {
"image": "0000000028037c08_1544283642.963721.jpg"
},
"_type": "dataRecord"
},
{
"_id": "wgv4u2cB9qH5eo0SmI8Z",
"_index": "homesecmum",
"_score": 1.074108,
"_source": {
"image": "0000000028037c08_1544283640.629583.jpg"
},
"_type": "dataRecord"
}
],
"max_score": 1.0870113,
"total": 5
},
"timed_out": false,
"took": 11
}

我试图只从json数据中提取图像参数,并将其存储为数组。我尝试了以下方法:

for result in res['hits']['hits']:
post = result['_source']['image']
print(post)

这个:

respars = json.loads(res['hits']['hits'][0]['_source'])['image']
print(json.dumps(respars, indent=4, sort_keys = True))

这两个都抛出了一个错误:

TypeError: byte indices must be integers or slices, not str

我确信这里早些时候也提出了类似的问题,但我无法克服这个错误。我该怎么修?

您可以使用PyPi的Elasticsearch DSL包,而不用手动处理响应。

要将_source条目中的所有图像作为列表,您可以使用列表理解:

image_list = [source['_source']['image'] for source in res['hits']['hits']]

输出:

['0000000028037c08_1544283640.314629.jpg',
'0000000028037c08_1544283642.963721.jpg',
'0000000028037c08_1544283640.629583.jpg']

最新更新