Skip to content

Commit 09f59b2

Browse files
authored
VCST-881: PriceRange Aggregation is not functioning as expected. (#726)
fix: Fixed the issue where the price range aggregation property was not being included in the response of the /api/catalog/search/products.
1 parent 480f6d9 commit 09f59b2

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/VirtoCommerce.CatalogModule.Data/Search/Indexing/AggregationConverter.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Text.RegularExpressions;
45
using System.Threading;
56
using System.Threading.Tasks;
67
using VirtoCommerce.CatalogModule.Core;
@@ -278,11 +279,36 @@ protected virtual Aggregation GetRangeAggregation(RangeFilter rangeFilter, IList
278279

279280
protected virtual Aggregation GetPriceRangeAggregation(PriceRangeFilter priceRangeFilter, IList<AggregationResponse> aggregationResponses)
280281
{
282+
var rangeAggregations = new List<AggregationResponse>();
283+
284+
// Merge All Virtual Price Ranges in rangeAggregations
285+
var priceFieldName = "price";
286+
287+
var priceValues = aggregationResponses.Where(a => a.Id.StartsWith(priceFieldName)).SelectMany(x => x.Values).ToArray();
288+
289+
if (priceValues.Length == 0)
290+
{
291+
return null;
292+
}
293+
294+
var matchIdRegEx = new Regex(@"^(?<left>[0-9*]+)-(?<right>[0-9*]+)$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
295+
296+
rangeAggregations.AddRange(priceValues.Select(x =>
297+
{
298+
var matchId = matchIdRegEx.Match(x.Id);
299+
var left = matchId.Groups["left"].Value;
300+
var right = matchId.Groups["right"].Value;
301+
x.Id = left == "*" ? $@"under-{right}" : x.Id;
302+
x.Id = right == "*" ? $@"over-{left}" : x.Id;
303+
return new AggregationResponse { Id = $@"{priceFieldName}-{x.Id}", Values = new List<AggregationResponseValue> { x } };
304+
}));
305+
306+
281307
var result = new Aggregation
282308
{
283309
AggregationType = "pricerange",
284310
Field = priceRangeFilter.Key,
285-
Items = GetRangeAggregationItems(priceRangeFilter.Key, priceRangeFilter.Values, aggregationResponses).ToArray(),
311+
Items = GetRangeAggregationItems(priceRangeFilter.Key, priceRangeFilter.Values, rangeAggregations).ToArray(),
286312
};
287313

288314

0 commit comments

Comments
 (0)