date filter using elasticsearch, using nest c#

I am having mapping for a date field like shown below.

.Date(s => s.Name("createdOn").Format("yyyy-MM-dd'T'HH:mm:ss.SSS||yyyy-MM-dd'T'HH:mm:ss||MM/dd/yyyy||yyyy/MM/dd"))

I want to get documents created after a certain date. I am passing date as string in the format MM/dd/yyyy to the filter range like shown below..

DateTime date;
DateTime.TryParse(filter.SearchText, out date);
string dateString = date.ToString("MM/dd/yyyy");
query = filterDesc.And(filterDesc.Range(n =>n.Greater(dateString).OnField(searchFields[filter.Field])));

But elastic search is considering time also in the search. i.e i want to get documents created after 2/10/2015, I am getting documents which are created on 2/10/2015 also. Please let me know the possible solution.

Thanks and in advance.

Upvotes: 2

Views: 4078

Answers (1)

bittusarkar
bittusarkar

Reputation: 6357

Basically you want to search for documents created on or after "2/11/2015 00:00". The code below should work I guess; I did not try it myself though.

DateTime date;
DateTime.TryParse(filter.SearchText, out date);
date = date.Date.AddDays(1); // date = floor(date) + 1 day
string dateString = date.ToString("MM/dd/yyyy");
query = filterDesc.And(filterDesc.Range(n =>n.GreaterOrEquals(dateString).OnField(searchFields[filter.Field])));

Mind you, if date in a document actually is exactly "2/10/2015 00:00", the document will not be returned as part of results of the above query. This is because we convert the date to "2/11/2015 00:00". But if you do not want this behaviour, you can tweak my solution as per your requirements.

Upvotes: 3

Related Questions