Hello everyone!
Been grappling this issue for a couple of days now and cannot find a way out of it. I'm using C# and NEST but the issue is in my filtration.
I have a product with nested prices array for each different price groups. I want to be able to get Min, Max and Avg aggregations but only based on the object from the nested list which is in the current price group.
The following code returns a valid response but the Min, Max and Avg values returned analyze all the prices in the price groups instead of just the one that matches. This leads me to believe that the issue is somewhere in a filtered query.
This is what the mapping looks like:
"_index": "products_da-dk",
"_type": "productbase",
"_id": "320",
"_version": 2,
"_score": 1,
"_source": {
"id": 320,
"sku": "1456",
"variantSku": "1364",
"productId": "320",
...
"productPrices": {
"priceForPriceGroups": [
{
"priceGroupName": "PriceGroup1",
"currency": "DKK",
"price": 124.5,
"priceWithVAT": 155.625,
"priceWithoutVAT": 124.5,
"priceBeforeDiscount": 124.5,
"vATValue": 31.125,
"priceGroupVATPercentage": 25
}
,
{
"priceGroupName": "PriceGroup10",
"currency": "GBP",
"price": 5.39,
"priceWithVAT": 6.7375,
"priceWithoutVAT": 5.39,
"priceBeforeDiscount": 5.39,
"vATValue": 1.3475,
"priceGroupVATPercentage": 25
}
]
}
And this is the C# code I am using to get the Aggregations.
return new SearchDescriptor<ProductBase>()
.Index(_indexName)
.From(0)
.Size(10000)
.Aggregations(ag => ag
.Terms("categories", t => t
.Field(p => p.Categories.Suffix("name").Suffix("keyword"))
.Size(100))
.Terms("certificates", term => term
.Field(field => field.Certificates.Suffix("keyword")))
.Terms("contents", term => term
.Field(field => field.Contents.Suffix("keyword")))
.Terms("type", term => term
.Field(field => field.Properties["Type"].Suffix("keyword")))
.Terms("function", term => term
.Field(field => field.Properties["Function"].Suffix("keyword")))
.Filter("filterPriceGroup", filter => filter
.Filter(f => f
.Term(t => t
.Field(field => field.ProductPrices.PriceForPriceGroups.Suffix("priceGroupName").Suffix("keyword"))
.Value(_currentPriceGroup.Name)))
.Aggregations( agg => agg
.Min("min", st => st
.Field(priceField))
.Max("max", st => st
.Field(priceField))
.Average("avg", st => st
.Field(priceField))
.Range("price", ra => ra
.Field(priceField)
.Ranges(
r => r.From(min).To(avg),
r => r.From(avg).To(max)))
))
);
(Just as extra context, the priceField is dynamically selected to fetch the price with or without VAT.)
Anyone has any ideas?