Skip to content

Commit

Permalink
PT-13020: Implement IIndexSchemaBuilder (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-dudarev authored Aug 14, 2023
1 parent 101a597 commit 84b1c1e
Showing 1 changed file with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<IList<IndexDocument>> GetDocumentsAsync(IList<string> documentIds)
{
var prices = await GetProductPrices(documentIds);
Expand Down Expand Up @@ -85,7 +95,8 @@ protected virtual void IndexPrice(IndexDocument document, IList<Price> 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<Price> prices, bool useMaxIndexationPrice)
Expand All @@ -95,7 +106,8 @@ protected virtual void IndexPriceByCurrency(IndexDocument document, IList<Price>
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);
}
}

Expand Down Expand Up @@ -167,7 +179,18 @@ private static void AddMinPrice(IndexDocument document, IList<Price> prices)
})
.ToList<object>();

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<object> value = null)
{
value ??= new List<object> { new IndexedPrice { Currency = SchemaStringValue } };
document.Add(new IndexDocumentField("__minVariationPrice", value, IndexDocumentFieldValueType.Complex) { IsRetrievable = false, IsFilterable = false, IsCollection = false });
}
}
}

0 comments on commit 84b1c1e

Please sign in to comment.