如何在Python中从Elasticsearch访问索引值

时间:2018-10-07 15:33:28

标签: python json dictionary elasticsearch

我将3个json对象从数组中转储到localhost Elasticsearch索引"amazon"中。

当我访问localhost中的索引时,它显示了此输出

{"amazon":{"aliases":{},"mappings":{"product-title":{"properties":{"images":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"price":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"title":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}}}},"settings":{"index":{"creation_date":"1538923579981","number_of_shards":"5","number_of_replicas":"1","uuid":"SQ83_ecZSn6x9mDsGj9KLQ","version":{"created":"6040299"},"provided_name":"amazon"}}}}

我想从我的python代码中访问"title""price""images"的值。我该怎么办?

1 个答案:

答案 0 :(得分:1)

您的输出(我们称其为d)是一本字典。您可以提取嵌套字典结构的一个分支并查询其键:

properties = d['amazon']['mappings']['product-title']['properties']

title = properties['title']
price = properties['price']
images = properties['images']

print(title, price, images, sep='\n')

{'type': 'text', 'fields': {'keyword': {'type': 'keyword', 'ignore_above': 256}}}
{'type': 'text', 'fields': {'keyword': {'type': 'keyword', 'ignore_above': 256}}}
{'type': 'text', 'fields': {'keyword': {'type': 'keyword', 'ignore_above': 256}}}