Try our conversational search powered by Generative AI!

NestedTermsFacetFor with additional Filter on the none nested object

Vote:
 

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.

Generate Find Query For a Nested Facet that works: (Notice the facet_filter --> Nested)

 "SearchPrices.CustomerPricing.PriceCode":{
         "terms":{
            "field":"SearchPrices$$nested.CustomerPricing.PriceCode$$string"
         },
         "nested":"SearchPrices$$nested",
         "facet_filter":{
                  "nested":{
                     "path":"SearchPrices$$nested",
                     "filter":{
                        "and":[
                           {
                              "term":{
                                 "SearchPrices$$nested.CustomerPricing.PriceTypeId":0
                              }
                           },
                           {
                              "term":{
                                 "SearchPrices$$nested.CustomerPricing.PriceCode$$string":"sale"
                              }
                           },
                           {
                              "range":{
                                 "SearchPrices$$nested.UnitPrice.Amount$$number":{
                                    "from":0.0,
                                    "to":79228162514264337593543950335.0,
                                    "include_lower":false,
                                    "include_upper":true
                                 }
                              }
                           },
                           {
                              "range":{
                                 "SearchPrices$$nested.ValidFrom$$date":{
                                    "from":"0001-01-01T00:00:00Z",
                                    "to":"2019-01-30T18:28:47.2294088Z",
                                    "include_lower":true,
                                    "include_upper":false
                                 }
                              }
                           },
                           {
                              "or":[
                                 {
                                    "not":{
                                       "filter":{
                                          "exists":{
                                             "field":"SearchPrices$$nested.ValidUntil$$date"
                                          }
                                       }
                                    }
                                 },
                                 {
                                    "range":{
                                       "SearchPrices$$nested.ValidUntil$$date":{
                                          "from":"2019-01-30T18:28:47.2294088Z",
                                          "include_lower":false
                                       }
                                    }
                                 }
                              ]
                           }
                        ]
                     },
                     "join":false
                  }
               }
      }
   },

Generate Find Query For a Nested Facet that does not work when trying to add a And: (Notice the facet_filter --> And)

   "SearchPrices.CustomerPricing.PriceCode":{
         "terms":{
            "field":"SearchPrices$$nested.CustomerPricing.PriceCode$$string"
         },
         "nested":"SearchPrices$$nested",
         "facet_filter":{
            "and":[
               {
                  "nested":{
                     "path":"SearchPrices$$nested",
                     "filter":{
                        "and":[
                           {
                              "term":{
                                 "SearchPrices$$nested.CustomerPricing.PriceTypeId":0
                              }
                           },
                           {
                              "term":{
                                 "SearchPrices$$nested.CustomerPricing.PriceCode$$string":"sale"
                              }
                           },
                           {
                              "range":{
                                 "SearchPrices$$nested.UnitPrice.Amount$$number":{
                                    "from":0.0,
                                    "to":79228162514264337593543950335.0,
                                    "include_lower":false,
                                    "include_upper":true
                                 }
                              }
                           },
                           {
                              "range":{
                                 "SearchPrices$$nested.ValidFrom$$date":{
                                    "from":"0001-01-01T00:00:00Z",
                                    "to":"2019-01-30T18:28:47.2294088Z",
                                    "include_lower":true,
                                    "include_upper":false
                                 }
                              }
                           },
                           {
                              "or":[
                                 {
                                    "not":{
                                       "filter":{
                                          "exists":{
                                             "field":"SearchPrices$$nested.ValidUntil$$date"
                                          }
                                       }
                                    }
                                 },
                                 {
                                    "range":{
                                       "SearchPrices$$nested.ValidUntil$$date":{
                                          "from":"2019-01-30T18:28:47.2294088Z",
                                          "include_lower":false
                                       }
                                    }
                                 }
                              ]
                           }
                        ]
                     },
                     "join":false
                  }
               },
               {
                  "terms":{
                     "Brand$$string":[
                        "Litehouse"
                     ]
                  }
               }
            ]
         }
      }
   },

The only solution I see is generating independant facet query with pre-filtering outside of the facet

#200922
Edited, Jan 30, 2019 17:11
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.