Query for latest document of an index

Hi,

I want to fetch the content of the latest document of an index through python. I'm using elasticsearch python module. In the elasticsearch python module documentation, es.get() takes an integer 'id'. but '_id' field is of string type.

from elasticsearch import Elasticsearch

es = Elasticsearch("xxx:9200")
res = es.get(index='zuul_log-6.0.1-2018.10.03', doc_type='_doc', id=1)
print res

below error is shown:

Traceback (most recent call last):
  File "fetch_es.py", line 10, in <module>
    res = es.get(index='zuul_log-6.0.1-2018.10.03', doc_type='_doc', id=1)
  File "/root/virtual-py27/lib/python2.7/site-packages/elasticsearch/client/utils.py", line 76, in _wrapped
    return func(*args, params=params, **kwargs)
  File "/root/virtual-py27/lib/python2.7/site-packages/elasticsearch/client/__init__.py", line 430, in get
    doc_type, id), params=params)
  File "/root/virtual-py27/lib/python2.7/site-packages/elasticsearch/transport.py", line 318, in perform_request
    status, headers_response, data = connection.perform_request(method, url, params, body, headers=headers, ignore=ignore, timeout=timeout)
  File "/root/virtual-py27/lib/python2.7/site-packages/elasticsearch/connection/http_urllib3.py", line 186, in perform_request
    self._raise_error(response.status, raw_data)
  File "/root/virtual-py27/lib/python2.7/site-packages/elasticsearch/connection/base.py", line 125, in _raise_error
    raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
elasticsearch.exceptions.NotFoundError: NotFoundError(404, u'{"_index":"zuul_log-6.0.1-2018.10.03","_type":"_doc","_id":"1","found":false}')

when I change '_id' to actual value that i see in kibana, by which code changes to:

from elasticsearch import Elasticsearch

es = Elasticsearch("xxx:9200")
res = es.get(index='zuul_log-6.0.1-2018.10.03', doc_type='_doc', id=AWYaUlU5Dy2D363mludo)
print res

again i get same below error:

Traceback (most recent call last):
  File "fetch_es.py", line 10, in <module>
    res = es.get(index='zuul_log-6.0.1-2018.10.03', doc_type='_doc', id='AWYaUlU5Dy2D363mludo')
  File "/root/virtual-py27/lib/python2.7/site-packages/elasticsearch/client/utils.py", line 76, in _wrapped
    return func(*args, params=params, **kwargs)
  File "/root/virtual-py27/lib/python2.7/site-packages/elasticsearch/client/__init__.py", line 430, in get
    doc_type, id), params=params)
  File "/root/virtual-py27/lib/python2.7/site-packages/elasticsearch/transport.py", line 318, in perform_request
    status, headers_response, data = connection.perform_request(method, url, params, body, headers=headers, ignore=ignore, timeout=timeout)
  File "/root/virtual-py27/lib/python2.7/site-packages/elasticsearch/connection/http_urllib3.py", line 186, in perform_request
    self._raise_error(response.status, raw_data)
  File "/root/virtual-py27/lib/python2.7/site-packages/elasticsearch/connection/base.py", line 125, in _raise_error
    raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
elasticsearch.exceptions.NotFoundError: NotFoundError(404, u'{"_index":"zuul_log-6.0.1-2018.10.03","_type":"_doc","_id":"AWYaUlU5Dy2D363mludo","found":false}')

Can you please help me out in fetching latest record of an index from elasticsearch?

And also does index field support wildcard? because I create a index per day and name it by appending date to the common part. So what i need is to fetch latest record of the latest index of the wildcard provided.

Thanks for the help.

Please ignore above. It was fixed

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.