def _get_latest_doc_from_index(es_index, sort_field):
    recent_query = {
        "filter": {
            "match_all": {}
        },
        "sort": {sort_field: "desc"},
        "size": 1
    }
    es = get_es_new()

    try:
        res = es.search(es_index, body=recent_query)
        if 'hits' in res:
            if 'hits' in res['hits']:
                result = res['hits']['hits'][0]
                return result['_source']['_id']

    except Exception, ex:
        logging.error("Error querying get_latest_doc_from_index[%s]: %s" % (es_index, ex))
        return None
Query elasticsearch index sort descending by the sort field
dataset/ETHPy150Open dimagi/commcare-hq/corehq/apps/hqadmin/escheck.py/_get_latest_doc_from_index