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

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

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

The code I'm using is:

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: documents were found, without a key (field _id), with which I saved the document.

The question is how to return _id keys?

Thanks!

+4
source share
1 answer

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

 r._meta.id 

_meta also contains 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 for models and result sets for more information.

+6
source

Source: https://habr.com/ru/post/1433243/


All Articles