I'm using elasticsearch nest within an asp.net mvc application.
Following elasticsearch query is throwing an exception because fields like categories and brands could be null or empty. How do add if statements and build the filters conditionally. Thank you!
I have to use bool & must to combine (AND) filters for search criteria. As an example a user want products in "shoes" category and retailer "macys".
s.Query(q => q
.Bool(bq => bq
.Must(
mq => mq.Filtered(fq => fq
.Filter(f => f.Terms("Categories", request.Categories))
),
mq => mq.Filtered(fq => fq
.Filter(f => f.Terms("BrandName", request.Brands))
),
mq => mq.Filtered(fq => fq
.Filter(f => f.Terms("RetailerName", request.Retailers))
),
mq => mq.Filtered(fq => fq
.Filter(f => f.Terms("RetailerName", request.Retailers))
),
mq => mq.Range(r => r
.OnField("SellingPrice")
.GreaterOrEquals((double)request.PriceRanges[0].Start)
.LowerOrEquals((double)request.PriceRanges[0].End)
)
)
)
);