Using Elasticsearch, how to obtain data between certain hours using range

Asked

Viewed 383 times

1

Hello, I am using Elasticsearch, and my query is not searching for the information I want.

{
    "query": 
    {
        "bool": 
        {
            "must": 
            [
                {
                    "range": 
                    {
                       "date": 
                       {
                           "gte": "2016-04-29 00:00:01",
                           "lte": "now"
                       }
                    }
                }
            ]
        }
    }
}

I’ve tried some variations, like putting:

"lte": "2016-04-29 23:59:59"

But it doesn’t matter, the result is all possible dates, basically it’s like this query doesn’t even exist. Can someone help me?

  • You want to take data with date greater than 29/04/2016?

  • Yes, I want the dates between 2016-04-29 00:00:01 and 2016-04-29 23:59:59, ie I want the information registered today

1 answer

3

Maybe the problem of not returning the filtered documents is related to type formatting datetime indexed.

I tested your query and the result was as expected, returned the documents filtered by the date range, the only change was the inclusion of the clause format.

If the format the query parameters should have the same formatting of the properties present in the indexed documents.

{
    "query": 
    {
        "bool": 
        {
            "must": 
            [
                {
                    "range": 
                    {
                       "my_property_date": 
                       {
                            "format": "yyyy-MM-dd HH:mm:ss",
                            "gte": "2016-04-29 00:00:01",
                            "lte": "now"
                       }
                    }
                }
            ]
        }
    }
}

Click here to learn more about the format | Elasticsearch

Browser other questions tagged

You are not signed in. Login or sign up in order to post.