Filtering categories using OR filters inside an AND filter

Vote:
 

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 recieved in the solution, we get a multiple lists of Categories that have been selected inside a category list.

E.g. 

Categories: 
[
    [
        "Category 1",
        "Category 2"
    ],
    [
        "Category 11",
        "Category 12"
    ]
]

After this, when searching using Find, I need to check for items that have "Category 1" OR "Category 2" AND "Category 11" OR "Category 12". If none of the items have these categories assigned to them, or as an example only have Category 1 assigned and nothing from the second list, no results should be returned.

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 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

        public static ITypeSearch MatchCategories(
            this ITypeSearch search, IList categoryList)
        {
            var categoryFilter = SearchClient.Instance.BuildFilter();

            foreach (var category in categoryList)
            {
                categoryFilter =
                    categoryFilter.Or(x => x.RelatedCourses.MatchItem(p => p.CourseCategories.Match(category)));                
            }

            return search.Filter(categoryFilter);
        }



#187629
Edited, Jan 30, 2018 10:09
Vote:
 

Have you tried adding a separate property that is just IEnumerable<string>? It will make the filtering easier and you might be able to use In() or just Exists() and Match().

#189681
Edited, Mar 22, 2018 23:44
Vote:
 

Hi @Steven Galton

Somethign like below could solve issue.

 public static ITypeSearch MatchCategories(
          this ITypeSearch search, IList categoryList)
        {

            foreach (var category in categoryList)
            {
                var categoryFilter = SearchClient.Instance.BuildFilter();

                foreach (var cat in category)
                {
                    categoryFilter =
                        categoryFilter.Or(x => x.RelatedCourses.MatchItem(p => p.CourseCategories.MatchContained(cat)));
                }

                search = search.Filter(categoryFilter);

            }

            return search;
        }
#189878
Mar 27, 2018 8: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.