gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 08:57:39 24 4
gpt4 key购买 nike

我将 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" 的值。我怎样才能做到这一点?

最佳答案

你的输出(我们称之为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}}}

关于python - 如何在Python中从Elasticsearch访问索引值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52690078/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com