Skip to content

Commit

Permalink
VCST-881: PriceRange Aggregation is not functioning as expected. (#726)
Browse files Browse the repository at this point in the history
fix: Fixed the issue where the price range aggregation property was not being included in the response of the /api/catalog/search/products.
  • Loading branch information
OlegoO committed Apr 11, 2024
1 parent 480f6d9 commit 09f59b2
Showing 1 changed file with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using VirtoCommerce.CatalogModule.Core;
Expand Down Expand Up @@ -278,11 +279,36 @@ protected virtual Aggregation GetRangeAggregation(RangeFilter rangeFilter, IList

protected virtual Aggregation GetPriceRangeAggregation(PriceRangeFilter priceRangeFilter, IList<AggregationResponse> aggregationResponses)
{
var rangeAggregations = new List<AggregationResponse>();

// Merge All Virtual Price Ranges in rangeAggregations
var priceFieldName = "price";

var priceValues = aggregationResponses.Where(a => a.Id.StartsWith(priceFieldName)).SelectMany(x => x.Values).ToArray();

if (priceValues.Length == 0)
{
return null;
}

var matchIdRegEx = new Regex(@"^(?<left>[0-9*]+)-(?<right>[0-9*]+)$", RegexOptions.IgnoreCase | RegexOptions.Compiled);

rangeAggregations.AddRange(priceValues.Select(x =>
{
var matchId = matchIdRegEx.Match(x.Id);
var left = matchId.Groups["left"].Value;
var right = matchId.Groups["right"].Value;
x.Id = left == "*" ? $@"under-{right}" : x.Id;
x.Id = right == "*" ? $@"over-{left}" : x.Id;
return new AggregationResponse { Id = $@"{priceFieldName}-{x.Id}", Values = new List<AggregationResponseValue> { x } };
}));


var result = new Aggregation
{
AggregationType = "pricerange",
Field = priceRangeFilter.Key,
Items = GetRangeAggregationItems(priceRangeFilter.Key, priceRangeFilter.Values, aggregationResponses).ToArray(),
Items = GetRangeAggregationItems(priceRangeFilter.Key, priceRangeFilter.Values, rangeAggregations).ToArray(),
};


Expand Down

0 comments on commit 09f59b2

Please sign in to comment.