diff --git a/src/VirtoCommerce.PricingModule.Data/Search/ProductPriceDocumentBuilder.cs b/src/VirtoCommerce.PricingModule.Data/Search/ProductPriceDocumentBuilder.cs index 81193790..859b15d5 100644 --- a/src/VirtoCommerce.PricingModule.Data/Search/ProductPriceDocumentBuilder.cs +++ b/src/VirtoCommerce.PricingModule.Data/Search/ProductPriceDocumentBuilder.cs @@ -13,10 +13,11 @@ using VirtoCommerce.SearchModule.Core.Extensions; using VirtoCommerce.SearchModule.Core.Model; using VirtoCommerce.SearchModule.Core.Services; +using static VirtoCommerce.SearchModule.Core.Extensions.IndexDocumentExtensions; namespace VirtoCommerce.PricingModule.Data.Search { - public class ProductPriceDocumentBuilder : IIndexDocumentBuilder + public class ProductPriceDocumentBuilder : IIndexSchemaBuilder, IIndexDocumentBuilder { private const int _batchSize = 50; @@ -31,6 +32,15 @@ public ProductPriceDocumentBuilder(IPricingEvaluatorService pricingEvaluatorServ _productsSearchService = productsSearchService; } + public Task BuildSchemaAsync(IndexDocument schema) + { + schema.AddFilterableCollection("is"); + AddDecimalCollection(schema, "price"); + AddMinVariationPrice(schema); + + return Task.CompletedTask; + } + public virtual async Task> GetDocumentsAsync(IList documentIds) { var prices = await GetProductPrices(documentIds); @@ -85,7 +95,8 @@ protected virtual void IndexPrice(IndexDocument document, IList prices, b var price = useMaxIndexationPrice ? prices.Max(x => x.EffectiveValue) : prices.Min(x => x.EffectiveValue); - document.Add(new IndexDocumentField("price", price, IndexDocumentFieldValueType.Decimal) { IsRetrievable = true, IsFilterable = true, IsCollection = true }); + + AddDecimalCollection(document, "price", price); } protected virtual void IndexPriceByCurrency(IndexDocument document, IList prices, bool useMaxIndexationPrice) @@ -95,7 +106,8 @@ protected virtual void IndexPriceByCurrency(IndexDocument document, IList var currencyPrice = useMaxIndexationPrice ? group.Max(x => x.EffectiveValue) : group.Min(x => x.EffectiveValue); - document.Add(new IndexDocumentField($"price_{group.Key}".ToLowerInvariant(), currencyPrice, IndexDocumentFieldValueType.Decimal) { IsRetrievable = true, IsFilterable = true, IsCollection = true }); + + AddDecimalCollection(document, $"price_{group.Key}".ToLowerInvariant(), currencyPrice); } } @@ -167,7 +179,18 @@ private static void AddMinPrice(IndexDocument document, IList prices) }) .ToList(); - document.Add(new IndexDocumentField("__minVariationPrice", minPricesByCurrency, IndexDocumentFieldValueType.Complex) { IsRetrievable = false, IsFilterable = false, IsCollection = false }); + AddMinVariationPrice(document, minPricesByCurrency); + } + + private static void AddDecimalCollection(IndexDocument document, string name, decimal value = 0m) + { + document.Add(new IndexDocumentField(name, value, IndexDocumentFieldValueType.Decimal) { IsRetrievable = true, IsFilterable = true, IsCollection = true }); + } + + private static void AddMinVariationPrice(IndexDocument document, List value = null) + { + value ??= new List { new IndexedPrice { Currency = SchemaStringValue } }; + document.Add(new IndexDocumentField("__minVariationPrice", value, IndexDocumentFieldValueType.Complex) { IsRetrievable = false, IsFilterable = false, IsCollection = false }); } } }