Proteeti Prova
Proteeti Prova

Reputation: 1169

How to access index values from Elasticsearch in python

I dumped 3 json objects from an array to a localhost Elasticsearch index "amazon".

When I accessed the index in localhost, it shows me this output

{"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"}}}}

I want to access the values of "title", "price" and "images" from my python code. How can I do that?

Upvotes: 1

Views: 72

Answers (1)

jpp
jpp

Reputation: 164773

Your output (let's call it d) is a dictionary. You can extract a branch of the nested dictionary structure and query its keys:

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}}}

Upvotes: 1

Related Questions