Search code examples
pythonelasticsearchpyes

How to make pyes search method to return the keys of the found documents?


I have a question about pyes (Python API of the ElasticSearch).

Is it possible to make the search method to return the keys of the found documents?

The code I use:

import pyes
conn = pyes.ES('localhost:9200')
q = pyes.StringQuery("november rain", default_operator="AND")
result = conn.search(query=q, indices=[index])
for r in result:
    print r

Result: found documents, without the key (_id field) that I saved the document with.

The question is how to return the _id keys?

Thanks!


Solution

  • All results in a ResultSet are just ElasticSearchModel objects. So you can access the _id attribute via the _meta DotDict like this

    r._meta.id
    

    _meta also contains some other useful information:

    (Pdb) pp res._meta
    {'connection': <pyes.es.ES object at 0x8268ecc>,
     u'id': u'2',
     u'index': u'twitter',
     'parent': None,
     u'score': 0.095891505000000002,
     u'type': u'tweet'}
    

    See the documentation on models and result sets for more details.