Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

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

Vote:
 

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
                .FilterForVisitor()
                .GetContentResult()
                .ToList();
        }

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.

#155237
Sep 13, 2016 8:05
Vote:
 

Hi,

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));
                }
#155238
Sep 13, 2016 9:13
Vote:
 

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

#155239
Sep 13, 2016 9:24
Vote:
 

Np! An easy mistake to make.

#155240
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.