public IDictionary <string, ICollection <KeyValuePair <string, int> > > TranslateFacetResult(ProcessFacetsArgs args)
        {
            ICollection <KeyValuePair <string, int> > values;
            var newValues      = new List <KeyValuePair <string, int> >();
            var facets         = args.Facets;
            var indexFieldName = args.FieldNameTranslator.GetIndexFieldName(this.FieldName);

            if (facets.TryGetValue(indexFieldName, out values))
            {
                facets.Remove(indexFieldName);
                foreach (var value in values)
                {
                    var translatedValue = this.TranslateToContentType(value.Key);
                    if (translatedValue == null)
                    {
                        continue;
                    }
                    newValues.Add(new KeyValuePair <string, int>(translatedValue, value.Value));
                }
                if (newValues.Any())
                {
                    facets[Constants.IndexFields.ContentType] = newValues;
                }
            }
            return(facets);
        }
Example #2
0
        public IDictionary <string, ICollection <KeyValuePair <string, int> > > TranslateFacetResult(ProcessFacetsArgs args)
        {
            var facets = args.Facets;

            var daterangeFacet =
                args.OriginalFacetQueries.Where(
                    q => q.FieldNames.Count() == 1 && q.FieldNames.First() == "daterangehourresolution").ToList();

            if (daterangeFacet.Count > 0)
            {
                var dateRangeFacet = new List <KeyValuePair <string, int> >();

                ICollection <KeyValuePair <string, int> > hourFacet;
                ICollection <KeyValuePair <string, int> > dateFacet;
                ICollection <KeyValuePair <string, int> > weekFacet;
                ICollection <KeyValuePair <string, int> > monthFacet;
                ICollection <KeyValuePair <string, int> > yearFacet;

                facets.TryGetValue(args.FieldNameTranslator.GetIndexFieldName("daterange_hour", typeof(string)),
                                   out hourFacet);
                facets.TryGetValue(args.FieldNameTranslator.GetIndexFieldName("__smallcreateddate", typeof(DateTime)),
                                   out dateFacet);
                facets.TryGetValue(args.FieldNameTranslator.GetIndexFieldName("daterange_week", typeof(string)),
                                   out weekFacet);
                facets.TryGetValue(args.FieldNameTranslator.GetIndexFieldName("daterange_month", typeof(string)),
                                   out monthFacet);
                facets.TryGetValue(args.FieldNameTranslator.GetIndexFieldName("daterange_year", typeof(string)),
                                   out yearFacet);

                if (hourFacet != null)
                {
                    var hour = hourFacet.FirstOrDefault(v => v.Key == DateTime.Now.ToString("yyyyMMddHH"));

                    if (hour.Key != null)
                    {
                        dateRangeFacet.Add(new KeyValuePair <string, int>("lasthour", hour.Value));
                    }

                    var lastmonth =
                        hourFacet.FirstOrDefault(v => v.Key == DateTime.Now.AddHours(-1).ToString("yyyyMMddHH"));

                    if (lastmonth.Key != null)
                    {
                        dateRangeFacet.Add(new KeyValuePair <string, int>("lasthour", lastmonth.Value));
                    }
                }

                if (dateFacet != null)
                {
                    var today =
                        dateFacet.FirstOrDefault(
                            v => v.Key == DateTime.Today.ToString(ContentSearchConfigurationSettings.IndexDateFormat));

                    if (today.Key != null)
                    {
                        dateRangeFacet.Add(new KeyValuePair <string, int>("today", today.Value));
                    }

                    var yesterday =
                        dateFacet.FirstOrDefault(
                            v =>
                            v.Key ==
                            DateTime.Today.AddDays(-1).ToString(ContentSearchConfigurationSettings.IndexDateFormat));

                    if (yesterday.Key != null)
                    {
                        dateRangeFacet.Add(new KeyValuePair <string, int>("yesterday", yesterday.Value));
                    }
                }

                if (weekFacet != null)
                {
                    var week =
                        weekFacet.FirstOrDefault(
                            v =>
                            v.Key ==
                            DateTime.Now.ToString("yyyyMM") +
                            CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(DateTime.Now,
                                                                                CalendarWeekRule.FirstDay, DayOfWeek.Monday));

                    if (week.Key != null)
                    {
                        dateRangeFacet.Add(new KeyValuePair <string, int>("thisweek", week.Value));
                    }

                    var lastweek =
                        weekFacet.FirstOrDefault(
                            v =>
                            v.Key ==
                            DateTime.Now.AddDays(-7).ToString("yyyyMM") +
                            CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(DateTime.Now.AddDays(-7),
                                                                                CalendarWeekRule.FirstDay, DayOfWeek.Monday));

                    if (lastweek.Key != null)
                    {
                        dateRangeFacet.Add(new KeyValuePair <string, int>("lastweek", lastweek.Value));
                    }
                }

                if (monthFacet != null)
                {
                    var month = monthFacet.FirstOrDefault(v => v.Key == DateTime.Now.ToString("yyyyMM"));

                    if (month.Key != null)
                    {
                        dateRangeFacet.Add(new KeyValuePair <string, int>("thismonth", month.Value));
                    }

                    var lastmonth =
                        monthFacet.FirstOrDefault(v => v.Key == DateTime.Now.AddMonths(-1).ToString("yyyyMM"));

                    if (lastmonth.Key != null)
                    {
                        dateRangeFacet.Add(new KeyValuePair <string, int>("lastmonth", lastmonth.Value));
                    }
                }

                int thisYearCount = 0;

                if (yearFacet != null)
                {
                    var year = yearFacet.FirstOrDefault(v => v.Key == DateTime.Now.ToString("yyyy"));

                    if (year.Key != null)
                    {
                        dateRangeFacet.Add(new KeyValuePair <string, int>("thisyear", year.Value));
                        thisYearCount = year.Value;
                    }

                    var calculatedFacetCount = yearFacet.Sum(f => f.Value) - thisYearCount;
                    if (calculatedFacetCount >= daterangeFacet.First().MinimumResultCount)
                    {
                        dateRangeFacet.Add(new KeyValuePair <string, int>("older", calculatedFacetCount));
                    }
                }

                facets["daterange"] = dateRangeFacet;
                facets.Remove(args.FieldNameTranslator.GetIndexFieldName("daterange_hour"));
                facets.Remove(args.FieldNameTranslator.GetIndexFieldName("__smallcreateddate"));
                facets.Remove(args.FieldNameTranslator.GetIndexFieldName("daterange_week"));
                facets.Remove(args.FieldNameTranslator.GetIndexFieldName("daterange_month"));
                facets.Remove(args.FieldNameTranslator.GetIndexFieldName("daterange_year"));
            }

            return(facets);
        }