Try our conversational search powered by Generative AI!

Can't get BuildFilter<T>() to work


I have this method

public IEnumerable GetAllStickyNewsForCurrentUser(IEnumerable categoryIds, ContentReference rootPage) where TModel : PageData, IAttachable
            var categoryFilter = _searchClient.BuildFilter();
            foreach (var categoryId in categoryIds)
                categoryFilter.Or(x => x.Category.Match(categoryId));

            return _searchClient.Search()
                .Filter(x => x.IsSticky.Match(true))
                .Filter(x => x.Ancestors().Match(rootPage.ID.ToString()))
                .Filter(categoryFilter)  // returns the same result with or without this line

What I want it to do is to filter on a boolean called IsSticky where the root page is the root page parameter and where the page has the category parameters provided. Now this method works equally with or without the categoryFilter which makes me doubt I follow write correct code. Even if I change categoryFilter.Or to categoryFilter.And the result is the same, also categoryFilter.HasFilter always returns false.

What do I need to change to make this method take the category filtering into account? Thanks.

Sep 13, 2016 8:05


in this case, I think the right approach would be to use the "In" method:

.Filter(x => x.Category.In(categoryIds))

However, the reason why your code is not working is because you are never assigning the categoryFilter with the actualy filtering. Correct code should be:

        foreach (var categoryId in categoryIds)
                    categoryFilter = categoryFilter.Or(x => x.Category.Match(categoryId));
Sep 13, 2016 9:13

You are correct, embarrassing mistake... Thanks for the .In() tip as well!

Sep 13, 2016 9:24

Np! An easy mistake to make.

Sep 13, 2016 9:29
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.