2

My code is as follows:

import json
from elasticsearch import Elasticsearch

es = Elasticsearch()

resp = es.search(index="mynewcontacts", body={"query": {"match_all": {}}})
    response = json.dumps(resp)
    data = json.loads(response)
    #print data["hits"]["hits"][0]["_source"]["email"]
    for row in data:
    print row["hits"]["hits"][0]["_source"]["email"]
    return "OK"

which produces this truncated (for convenience) JSON:

{"timed_out": false, "took": 1, "_shards": {"successful": 5, "total": 5, "failed": 0}, "hits": {"max_score": 1.0, "total": 7, "hits": [{"_index": "mynewcontacts", "_type": "contact", "_score": 1.0, 
"_source": {"email": "[email protected]", "position": "Sr.Researcher", "last": "Zhuo", "first": "Sharon", "company": "Tabridge Executive Search"}, "_id": "AVYmLMlKJVSAh7zyC0xf"},
{"_index": "mynewcontacts", "_type": "contact", "_score": 1.0, "_source": {"email": "[email protected]", "position": "Vice President", "last": "Springthorpe", "first": "Andrew", "company": "SBC Group"}, "_id": "AVYmLMlRJVSAh7zyC0xg"}, {"_index": "mynewcontacts", "_type": "contact", "_score": 1.0, "_source": {"email": "[email protected]", "position": "Financial Advisor", "last": "Bell", "first": "Margaret Jacqueline", "company": "Streamline"}, "_id": "AVYmLMlXJVSAh7zyC0xh"}, {"_index": "mynewcontacts", "_type": "contact", "_score": 1.0, "_source": {"email": "[email protected]", "position": "Technical Solutions Manager MMS North Asia", "last": "Okai", "first": "Kensuke", "company": "Criteo"}, "_id": "AVYmLMlfJVSAh7zyC0xi"}, {"_index": "mynewcontacts", "_type": "contact", "_score": 1.0, "_source": {"email": "[email protected]", "position": "Sr. Strategic Account Executive", "last": "Kato", "first": "Mizuto", "company": "Twitter"}, "_id": "AVYmLMlkJVSAh7zyC0xj"}, {"_index": "mynewcontacts", "_type": "contact", "_score": 1.0, "_source": {"email": "[email protected]", "position": "Design Manager", "last": "Okada", "first": "Kengo", "company": "ON Semiconductor"}, "_id": "AVYmLMlpJVSAh7zyC0xk"}, {"_index": "mynewcontacts", "_type": "contact", "_score": 1.0, "_source": {"email": "[email protected]", "position": "Legal Counsel", "last": "Lei", "first": "Yangzi (Karen)", "company": "Samsung China Semiconductor"}, "_id": "AVYmLMkUJVSAh7zyC0xe"}]}}

When I try:

print data["hits"]["hits"][0]["_source"]["email"]

it prints the first email fine but when I attempt the loop with

for row in data:
    print row["hits"]["hits"][0]["_source"]["email"]

I receive an error:

TypeError: string indices must be integers

Please can somebody suggest how I can iterate through the items correctly? Many thanks!

3 Answers 3

5

What you're doing is looping through keys of the dictionary. To print each email in the response you'd do this:

for row in data["hits"]["hits"]:
    print row["_source"]["email"]

Also converting to json isn't necessary. This should accomplish what you're looking to do:

from elasticsearch import Elasticsearch

es = Elasticsearch()

resp = es.search(index="mynewcontacts", body={"query": {"match_all": {}}})
for row in resp["hits"]["hits"]:
    print row["_source"]["email"]
return "OK"
Sign up to request clarification or add additional context in comments.

Comments

1

I could be wrong, but looks like you might not be starting the for loop based on the correct json item. Try:

for row in data['hits']['hits']:
    # Rest of loop here.

1 Comment

In your original code you had retrieved a dictionary key too deep int the json data.
1

Your retrieved response data is a Python dictionary - if you make a for loop over it, it will yield the dictionary keys - in this case, teh strigns timed_out, took, shards, etc...

Aparently yu want to iterate over teh list provided in the position data["_shards"]["hits"]["hits"] in your response data. That is a list.

So, just do

for row in data["_shards"]["hits"]["hits"]:
    print(row["_source"]["email"])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.