November Happy Hour will be moved to Thursday December 5th.

Unified Search - Excluding pages by property

Vote:
 

I have a Boolean property on all the pages in my site called 'IncludeInSearch'.

I want to configure an 'AlwaysApplyFilter' on the UnifiedSearchRegistry to exclude pages from unified searches when IncludeInSearch=false.

As far as I am aware, I cannot see any IClient extensions to allow me to-do this.

I can see Henrick has created an example custom filter or fuzzy searches which I guess I could do, however I can see there already a Boolean custom filter at EPiServer.Find.Api.Querying.Filters.BoolFilter which I think I can use?

Im not sure how this all pieces together but essentially I want the end result to look like this in the find initialization:

SearchClient.Instance.Conventions.UnifiedSearchRegistry
                .ForInstanceOf<SitePageBase>()
                .AlwaysApplyFilter(c => c.BuildFilter<SitePageBase>().FilterExcludeFromSearch());

 Can anyone help?

#72401
Jun 14, 2013 17:17
Vote:
 

Hi,


You can do like this:

SearchClient.Instance.Conventions.UnifiedSearchRegistry.ForInstanceOf<PageData>()
    .PublicSearchFilter(page => !page.MatchTypeHierarchy(typeof(SitePageBase)) | ((SitePageBase)page).IncludeInSearch.Match(true));

If you're indexing SitePageBase you can remove the MatchTypeHierarchy expression and use it as the type parameter instead. Often you don't want to index all of your page types inheriting from SitePageBase. So then it would be like:

SearchClient.Instance.Conventions.UnifiedSearchRegistry.ForInstanceOf<SitePageBase>()
    .PublicSearchFilter(page => page.IncludeInSearch.Match(true));

    

 

 

#72403
Jun 15, 2013 11:39
Vote:
 

... and if you really want to use AlwaysApplyFilter:

SearchClient.Instance.Conventions.UnifiedSearchRegistry.ForInstanceOf<SitePageBase>()
    .AlwaysApplyFilter(c => c.BuildFilter<SitePageBase>()
        .And(page => page.IncludeInSearch.Match(true)));

    

#72404
Jun 15, 2013 11:42
Vote:
 

Thanks Johan, 

I adapted the third example to work

            SearchClient.Instance.Conventions.UnifiedSearchRegistry
                .Add<SitePageBase>()
                .AlwaysApplyFilter(c => c.BuildFilter<SitePageBase>()
                    .And(page => page.IncludeInSearch.Match(true)));

    

I had to include the Add<T> else the website was throwing an exception on startup with 'The type SitePageBase has not been added'.

I could not get the first two examples to work as in each instance I was getting an exception 'Cannot cast IClient to SitePageBase'. It seems in each, the page variable in the lambda expression is of type IClient.

#72408
Edited, Jun 16, 2013 10:02
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.