Quantcast
Channel: Developer to developer
Viewing all articles
Browse latest Browse all 9076

NestedTermsFacetFor with additional Filter on the none nested object

$
0
0

Hi,

I created a term facet to show products that are currently on sale for a user this works well.

However I would like to combine this facet with additional none nested filters.

By default, TermsFacetFor does not allow to filter on none nested object.
I would want the Facet to be a combination of the nested expression and anoter regular filter at the product level like FoodProduct.Name.Match(..)

            query = query.TermsFacetFor<T, Price>(p => ((FoodProduct)(object)p).SearchPrices,
                                        price => price.CustomerPricing.PriceCode,
                                        priceFilter =>
                                                       (
                                                           priceFilter.CustomerPricing.PriceTypeId.Match(applicablePriceType) & priceFilter.CustomerPricing.PriceCode.Match(applicableSalesCode + "sale")
                                                        )& priceFilter.UnitPrice.Amount.GreaterThan(0) //if a price is at zero ignore it& priceFilter.ValidFrom.LessThan(utcNow)& (!priceFilter.ValidUntil.Exists() | priceFilter.ValidUntil.GreaterThan(utcNow))                                              
                                                        ,
                                                        null, 
                                                        filter);


Thus I created an overload for TermsForFacetFor as follow to created a AndFilter if any additional filter are required:

public static ITypeSearch<TSource> TermsFacetFor<TSource, TListItem>(this ITypeSearch<TSource> search, Expression<Func<TSource, IEnumerable<TListItem>>> enumerableFieldSelector, Expression<Func<TListItem, string>> itemFieldSelector, Expression<Func<TListItem, Filter>> filterExpression = null, Action<NestedTermsFacetRequest> facetRequestAction = null, Filter orignialFilter = null)
        {
            Filter facetFilter = NestedFilter.Create(search.Client.Conventions, enumerableFieldSelector, filterExpression);
            Action<NestedTermsFacetRequest> action = null;
            action = ((!facetRequestAction.IsNotNull()) ? ((Action<NestedTermsFacetRequest>)delegate (NestedTermsFacetRequest x)
            {
                x.FacetFilter = orignialFilter == null ? facetFilter : new AndFilter(facetFilter, orignialFilter);
            }) : ((Action<NestedTermsFacetRequest>)delegate (NestedTermsFacetRequest x)
            {
                x.FacetFilter = orignialFilter == null ? facetFilter : new AndFilter(facetFilter, orignialFilter);
                facetRequestAction(x);
            }));
            return search.AddNestedTermsFacetFor(enumerableFieldSelector, itemFieldSelector, action);
        }

It always returns 0 results and I am unsure on my approch to resolve this issue.

I know, I could pre-filter outside of the facet but it is not what I want to achieve.


Viewing all articles
Browse latest Browse all 9076

Trending Articles