(Elastic) Filter in stock for specific variants, based on another filter

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

No, it is not possible to execute c# code inside the query, the expression that you add for the field name will tried to be generated as a string for the field name on the search request. Try to get the query that is executed in the log to see how it was transformed.

In most cases when you build complex queries it can be easier to build a query in the “Dev tools” inside Kibana and after that translate that into NEST syntax that will be used inside Accelerator code.

Alright, I understand.

Is it possible to match a string with another string in elastic, without a field? I can’t find any documentation on this, so I guess not? If not, how would you solve this problem?

I’m using the dev tools in kibana, but I’m at a dead end, as I can’t index the actual stock status, as stock is dependent on the country. If I try to index the stock for each size, per country, the document gets too big, exceeding the maximum field limit (1000).

Still in need of some assistance here.

It’s possible to do scripting inside the query, see
Script Query | Elasticsearch Guide [6.8] | Elastic

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.