1

I must be missing something. When I do a elastic.search(index='parole', q='sometext'), I got an object returned. I am trying to get the element inside this object

data=elastic.search(index='parole', q='sometext')
mydata=data['hits']['hits']

That is working perfectly.

But if I am doing:

mydata['_source']
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
        TypeError: list indices must be integers or slices, not str

What am I doing wrong?

>>> mydata=data['hits']['hits']
>>> mydata
    [{'_id': '6', '_source': {'parole_de_la_chanson': " Blabla", 'titre_chanson':  'Allo Maman Bobo', 'numero_id_json': 'key6', 'nom_du_chanteur': ' 'Alain Souchon '}, '_index': 'parole', '_type': 'string', '_score': 0.2802974}]
2
  • 1
    print mydata between those two lines and tell us the result. Commented Feb 7, 2016 at 14:12
  • Thanks. Ok. I ve updated it. Commented Feb 7, 2016 at 14:24

1 Answer 1

2

The problem is that data is a list. I would say, try mydata = data[0]['hits']['hits'], but it doesn't look like 'hits' is in that list at all, even in the dictionary.

Sign up to request clarification or add additional context in comments.

3 Comments

I ve made a mistake when I update mydata. What I want is acces '_source' and then 'parole_de_la_chanson'
In that case, try mydata = data[0]['_source']['parole_de_la_chanson'].
In fact it is: 'mydata[0]['_source']['parole_de_la_chanson']' thanks for your help.

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.