2

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.

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

In NEST 1.4.1 at least, the date related range methods have an overload for format, like n.GreaterOrEquals(date, "MM/dd/yyyy"). Just to help keep things a little cleaner.
@Snixtor I've done exactly that if you notice. The minor complexity in date computation is for meeting the requirements of the question asked.
@bsarkar I am looking for documents created after '2/11/2015'. I tried the overload method with format. But still no luck. As a temporary fix by setting the date as '2/11/2015 23:59:59' I am getting the required documents.
@RavindraKumarChallagandla I think there is some confusion here. My code works fine if you want to search for documents created after 2/10/2015 as that is what you asked for in your question. If it works for you, then please mark it as answer.
@baskar I don't want to include timestamp while searching for documents. Is there any way I can achieve this ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.