Try our conversational search powered by Generative AI!

Filtering on Date part of DateTime

Vote:
 

I need to filter only events that starts today, ignoring the time of day. I have tried with the following extension method:

        public static FilterExpression AsDateMatch(this DateTime? value, DateTime matchValue)
        {
            return new FilterExpression(x => x.Exists() & x.Value.Date.Match(matchValue));
        }

...but this does not work. It seems that x.Value.Date.Match(matchValue) are not allowed.


I use the extension method like this:

SearchClient.Instance.Search()
.Filter(x => x.StartDate.AsDateMatch(DateTime.Now.Date))
.GetResult();
#145416
Mar 03, 2016 13:19
Vote:
 
SearchClient.Instance.Search<Events>()
            .Filter(x => x.StartDate.MatchDay(DateTime.Today.Year,
                                                DateTime.Today.Month,
                                                DateTime.Today.Day))
            .GetResult();
#145419
Mar 03, 2016 14:20
Vote:
 

Great, but x.StartDate is nullable (i.e. of type DateTime?) and MatchDay() is only available for the non-nullable DateTime.

If anyone could explain why the code in my first post does not work, it would make me really happy! :-)

#145420
Edited, Mar 03, 2016 14:29
Vote:
 
#145422
Mar 03, 2016 14:35
Vote:
 

No it does not, and I can't understand why.

#145425
Mar 03, 2016 15:07
Vote:
 

It seems that epifind cannot handle .Value

Can you try the following code:

public static FilterExpression<DateTime?> AsDateMatch(this DateTime? value, DateTime matchValue)
{
    var minValue = new DateTime(matchValue.Year, matchValue.Month, matchValue.Day, 0, 0, 0);
    var maxValue = new DateTime(matchValue.Year, matchValue.Month, matchValue.Day, 23, 59, 59);

    return new FilterExpression<DateTime?>(x => x.Exists() & x.InRange(minValue, maxValue));
}
#145429
Edited, Mar 03, 2016 15:31
Vote:
 

Ok, I think I know why the original code doesn't work.

First, I've indexed the following object:

public class Event
{
    public string Title { get; set; }
    public DateTime? StartDate { get; set; }
}

Then I opened epifind index, and noticed that StartDate field is indexed as StartDate$$date

Then I used Fiddler to see all requests that are sent to epifind, and noticed that x.Value.Date.Match(matchValue) generates the following query:

"term":{"StartDate.Value$$date":"2016-03-02T23:00:00Z"}

Since StartDate.Value$$date field doesn't exist, we're getting no hits.

Hope this helps!

#145432
Edited, Mar 03, 2016 15:48
Vote:
 

Thanks! :-)

#145596
Mar 08, 2016 7:45
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.