Sulot
Sulot

Reputation: 514

Elasticsearch .search object return error in Python3

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

Upvotes: 1

Views: 188

Answers (1)

zondo
zondo

Reputation: 20346

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.

Upvotes: 2

Related Questions