There was a previous post almost with same requirements there was no accepted answer in that post and my requirements are but different
I am currently creating a service using Episerver Find. I have managed to get everything working apart from the filtering of Categories. From the API that is received in the solution, we get multiple lists of Categories that have been selected inside a category list.
E.g.
var requiredCategories = new List<string>{"Category 1","Category 2"};
var optionalCategories = new List<string>{"Category 3","Category 4"};
After this, when searching using Find, I need to check for Content that has "Category 1 AND Category 2 AND Category 3" OR "Category 1 AND Category 2 AND Category 4". If none of the items have these categories assigned to them, or as an example only have Category 1 AND Category 3 assigned and not Category 2 assigned, no results should be returned.
Updated
if optionalCategories is empty then the logic is straight forward but if there are values in it, it makes the AND OR combination with requiredCategories
for example
var requiredCategories = new List<string>{"Category 1","Category 2"};
var optionalCategories = new List<string>{"Category 3","Category 4"};
Find Query Should like this: Get me Articles which have [Categogy 1 AND Category 2 AND Category 3] OR [Categogy 1 AND Category 2 AND Category 4]
Now if
Article 1 has [Category 1, Category 2, Category 3]
Article 2 has [Category 1, Category 2, Category 4]
Article 3 has [Category 1, Category 3, Category 4]
Article 4 has [Category 2, Category 3]
Article 5 has [Category 1, Category 2]
According to the logic, only Article 1 & Article 2 qualifies and Article 3 , Article 4 & Article 5 should be rejected
I can not seem to find a way to do this. I Have tried a Filter Builder using the OR method and then passing that into the Search.Filter() method but that seems to get changed to an OR filter instead of an AND filter. Here is the code of that just in case I there is a mistake
//Code in a function
foreach (var category in optionalCategories)
{
var categoriesToMatch = new List<string> { category };
categoriesToMatch.AddRange(requiredCategories);
query = query.MatchCategories(categoriesToMatch);
}
// End of function
public static ITypeSearch MatchCategories(this ITypeSearch search, List<string> categoryList)
{
var categoryFilter = SearchClient.Instance.BuildFilter();
foreach (var category in categoryList)
{
categoryFilter = categoryFilter.And(x => x.PageCategories.Match(category);
}
return search.OrFilter(categoryFilter);
}