Filipe Ferminiano
Filipe Ferminiano

Reputation: 8791

Elasticsearch querying doesn't bring any hit

i'm trying to search elasticsearch with python the last events from the last 5 minutes. But I'm getting this answer with no hits. This is ES answer:

{"hits": {"hits": [], "total": 0, "max_score": null}, "_shards": {"successful": 45, "failed": 0, "total": 45}, "took": 13, "timed_out": false}

And I know there are hits because I see them in Kibana.

This is my code:

from datetime import datetime
from elasticsearch import Elasticsearch, RequestsHttpConnection
import certifi
import datetime

def get_events():
    es = Elasticsearch([ELASTIC_SEARCH_ENDPOINT],use_ssl=True)
    from_date = datetime.datetime.now() - datetime.timedelta(minutes=15)
    to_date = datetime.datetime.now()
    query = {"query":
                {
                    "range":
                    {
                        "timestamp":
                        {
                            "gte": from_date,
                            "lte": to_date
                        }
                    }
                }    
            }
    res = es.search(index="logstash-*", body=query)
    print '########### recent events ############'
    print res
    return res

How can I fix this?

Upvotes: 0

Views: 47

Answers (1)

Girdhar Sojitra
Girdhar Sojitra

Reputation: 678

Timestamp which is being stored in Elasticsearch is in the same timezone(UTC or your machine time) as from_date and to_date (which is being used in query). This can be the reason why you are not seeing any result.

Upvotes: 1

Related Questions