Join us this Friday for AI in Action at the Virtual Happy Hour! This free virtual event is open to all—enroll now on Academy and don’t miss out.

 

MatchContained with DateTime For Day, Month, Year match

Vote:
 

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.

#225596
Jul 21, 2020 10:15
Vote:
 

I adapted my approach for this. Firstly I realised I only cared about Month and Year but I still had the same problem. So for anyone else the steps I did were

  1. Changed my indexed date fields to be a strongly typed class field so I could use the MatchContained (As it doesn't seem to work with the collection item for the match).
  2. In my type I index both a DataTime and a representitive string for the date. E.G $"{dateTime.Year}-{dateTime.Month}
  3. Used the MatchContained with my new indexed field as below
        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 datesInRange = minDate.GetDateMonthRangeFloored(maxDate);

                foreach (var dateTime in datesInRange)
                {
                    var stringMatch = $"{dateTime.Year}-{dateTime.Month}";
                    dateFilter = dateFilter.Or(x => x.MonthRange.MatchContained(f => f.Key, stringMatch));
                }

                return query.Filter(dateFilter);
            }

            return query;
        }
#225611
Jul 21, 2020 17:07
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.