Quantcast
Channel: Using Optimizely Platforms
Viewing all articles
Browse latest Browse all 8020

MatchContained with DateTime For Day, Month, Year match

$
0
0

Hi guys using using find 13.2.7 I have trying to match if any passed in dates in a range exist within an indexed collection of date ranges for the index item

E.g I have index on my item with a field

"EventDayRange$$date": ["2020-07-26T23:00:00Z","2020-07-27T23:00:00Z","2020-07-28T23:00:00Z","2020-07-29T23:00:00Z"]

And I'm trying to write an extension method that allows be to pass a list of dates and check if any of those exist in the list.

I created

        public static ITypeSearch<T> FilterMonths<T>(this ITypeSearch<T> query, List<DateTime> filterDates)
            where T : IContent, IEventContent
        {
            if (filterDates != null && filterDates.Any())
            {
                var dateFilter = SearchClient.Instance.BuildFilter<T>();
                var minDate = filterDates.Min();
                var maxDate = filterDates.Max();
                var daysInMonth = DateTime.DaysInMonth(maxDate.Year, maxDate.Month);
                var realMaxDate = new DateTime(maxDate.Year, maxDate.Month, daysInMonth, 23, 59, 59);
                var datesInRange = minDate.GetDateRangeFloored(realMaxDate);
                foreach (var dateTime in datesInRange)
                {
                    dateFilter = dateFilter.Or(x => x.EventDayRange.MatchItem(d => d.MatchDay(dateTime.Year, dateTime.Month, dateTime.Day)));
                }
                return query.Filter(dateFilter);
            }
        /// <summary>
        /// Gets the date range.
        /// </summary>
        /// <param name="startDate">The start date.</param>
        /// <param name="endDate">The end date.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentException">endDate must be greater than or equal to startDate</exception>
        public static IEnumerable<DateTime> GetDateRangeFloored(this DateTime startDate, DateTime endDate)
        {
            if (endDate < startDate)
                throw new ArgumentException("endDate must be greater than or equal to startDate");
            while (startDate <= endDate)
            {
                yield return new DateTime(startDate.Year, startDate.Month, startDate.Day, 0, 0,0);
                startDate = startDate.AddDays(1);
            }
        }

But when I run thge query I get an error.

I've been using the match contained for another query which works as it's just a string but there's no matchcontained implementation for a DateTime Day,Month,Year match.

Does anyone good with Find know how to resolve this as I'm a bit rusty with my Find queries.


Viewing all articles
Browse latest Browse all 8020

Trending Articles