0

I'm looking for a way to partially match a date when querying objects.

I store my dates in the Y-m-d H:i:s format and now, for example, I want to find all blog posts that were created on May 2015 (by using a field named "created_at" for example). How can I do that?

Although this seems to be like something popular to do, I could not find anything with Google.

user2394156
  • 1,792
  • 4
  • 16
  • 33
  • 1
    [This answer](http://stackoverflow.com/questions/31941295/searching-date-field-value-in-elasticsearch-without-time/31942008#31942008) should help. – Val Aug 13 '15 at 12:13
  • I was thinking of something similar, but is there anything more elegant? – user2394156 Aug 13 '15 at 12:13

1 Answers1

2

You'll need to use a range filter, this should work:

{
    "query": {
        "filtered": {
            "filter": {
                "range": {
                    "created_at": {
                        "gte": "2015-05-01 00:00:00"
                        "lte": "2015-05-01 23:59:59"
                    }
                }
            }
        }
    }
}
Or Weinberger
  • 7,332
  • 23
  • 71
  • 116
  • Ok so it's not working for the format I specified. For yyyy-MM it seems to be not ignoring the missing part of the date – user2394156 Aug 13 '15 at 13:24
  • Are you sure that `created_at` is defined as 'date'? Can you please share the contents of `http://localhost:9200//_mapping` ? – Or Weinberger Aug 13 '15 at 13:37