Hi,
I’m trying to only show products in stock if a specific filter is active.
For example, if use the filter “Size”, and select “S”, I only want to show products where the variants with size S is in stock.
I tried to index the value, but quickly realized that the document would eventually grow too big if I would index all sizes and their stock status, per country.
Somehow I need to add a query where the stock status is evaluated during runtime, as I cannot index the value. I tried with the following code:
public IEnumerable<Func<QueryContainerDescriptor<ProductDocument>, QueryContainer>> GetStockStatusFilterTags(
SearchQuery searchQuery,
IEnumerable<string> sizes)
{
if (searchQuery.Sizes?.Count > 0)
{
foreach (var size in sizes)
{
yield return q => q.Nested(n => n
.Path(x => x.VariantSystemIds)
.Query(nq
=> nq.Term(t => t.Field(f => _variantService.Get(f.VariantSystemIds.First()).Fields.GetValue<string>("Size")).Value(size))
&& nq.Term(t => t.Field(f => _stockService.HasStock(f.VariantSystemIds.First(), null)).Value(true))
)
);
}
}
}
Then in the BuildQuery method:
if (searchQuery.Sizes?.Count() > 0)
{
var sizeStockFilters = _stockFilterService
.GetStockStatusFilterTags(searchQuery, searchQuery.Sizes, countryId)
.ToList();
if (sizeStockFilters.Count > 0)
{
allQueries.Add(qc.Bool(b => b.Filter(bf => bf.Bool(bb => bb.Should(sizeStockFilters)))));
}
}
searchQuery.Sizes is a string list of text option values, for field “Size”, which is populated from the selected filters.
For some reason, this does not work, and my guess is that I can’t use Field() that way, and by that way I mean get a value from a function inside the NEST query. I tried google but couldn’t find any good example, or if this is even possible, or if I’m doing something else wrong.
The error I’m getting is all shards failed, which could basically mean anything and nothing at the same time. Not sure where to look to exactly see what went wrong with the query?
Litium version: 7.6.2