Ejemplo n.º 1
0
        public CatalogSearchQuery WithRating(
            double?fromRate,
            double?toRate,
            bool?includeFrom = null,
            bool?includeTo   = null)
        {
            if (fromRate == null && toRate == null)
            {
                return(this);
            }
            if (fromRate.HasValue)
            {
                Guard.InRange(fromRate.Value, 0.0, 5.0, nameof(fromRate.Value));
            }
            if (toRate.HasValue)
            {
                Guard.InRange(toRate.Value, 0.0, 5.0, nameof(toRate.Value));
            }

            if (fromRate.HasValue && toRate.HasValue && fromRate == toRate)
            {
                var forbidden = includeFrom.HasValue && includeTo.HasValue && !includeFrom.Value && !includeTo.Value;

                return(WithFilter(SearchFilter.ByField("rating", fromRate.Value).Mandatory(!forbidden).ExactMatch().NotAnalyzed()));
            }
            else
            {
                var filter = SearchFilter.ByRange(
                    "rating",
                    fromRate,
                    toRate,
                    includeFrom ?? fromRate.HasValue,
                    includeTo ?? toRate.HasValue);

                return(WithFilter(filter.Mandatory().ExactMatch().NotAnalyzed()));
            }
        }
Ejemplo n.º 2
0
        public CatalogSearchQuery PriceBetween(
            Money?fromPrice,
            Money?toPrice,
            bool?includeFrom = null,
            bool?includeTo   = null)
        {
            if (fromPrice == null && toPrice == null)
            {
                return(this);
            }

            Guard.NotEmpty(CurrencyCode, nameof(CurrencyCode));

            var fieldName = "price_c-" + CurrencyCode.EmptyNull().ToLower();

            if (fromPrice.HasValue && toPrice.HasValue && fromPrice == toPrice)
            {
                var forbidden = includeFrom.HasValue && includeTo.HasValue && !includeFrom.Value && !includeTo.Value;

                return(WithFilter(SearchFilter
                                  .ByField(fieldName, decimal.ToDouble(fromPrice.Value.Amount))
                                  .Mandatory(!forbidden)
                                  .ExactMatch()
                                  .NotAnalyzed()));
            }
            else
            {
                var filter = SearchFilter.ByRange(fieldName,
                                                  fromPrice.HasValue ? decimal.ToDouble(fromPrice.Value.Amount) : null,
                                                  toPrice.HasValue ? decimal.ToDouble(toPrice.Value.Amount) : null,
                                                  includeFrom ?? fromPrice.HasValue,
                                                  includeTo ?? toPrice.HasValue);

                return(WithFilter(filter.Mandatory().ExactMatch().NotAnalyzed()));
            }
        }