SaaS CMS has officially launched! Learn more now.

Filtering Find results according to website domain



I have multiple sites on my Episerver. Now, when I search for anything on my search pages then it gets me results from both the sites. I would like to limit this. Is there any parameter that I can send in the query that would enable me to specify the website from which results should be returned?

I hope it makes sense. Can anyone help with this?


Aug 29, 2019 13:37

Hi Piyush,

I think you can apply the Startpage condition inside the filter. Just retrieve the start page from sitedefintion and check the condition.

.Filter(x => x.Ancestors().Match(startpage.ContentLink.ID)) 
Aug 29, 2019 13:44
Piyush Bora - Aug 29, 2019 13:56
Thank you Ravindra.
I have a follow up question to make sure I got this right. So, this means that when the search page is opening at that moment I add a filter to the query specifying the start page of website?
how do I get this 'startpage.ContentLink.ID' value?

These questions may be a bit on the silly side but thanks!
Ravindra S. Rathore - Aug 29, 2019 14:08
1. When your search function is called then just check the start page and filter the result based on the start page
2. Use EPiServer.Core.ContentReference.StartPage to get the start page.
Piyush Bora - Aug 30, 2019 7:28
I tried to do this. I'm able to get the start page.
Below is the snippet of my code:

var queryResults = searchQuery.GetResults();
var startPage = EPiServer.Core.ContentReference.StartPage;
queryResults = queryResults.Filter(x => x.Ancestors().Match(startPage.ContentLink.ID));

queryResults is IContentResults

Am I doing it right or missing something?
Ravindra S. Rathore - Aug 30, 2019 7:47
Hey Piyush,

The filter should be applied before the "searchQuery.GetResults();" because if you apply the Filter after the "GetResults" then it will not make any sense of using the Find because "GetResults" will load all the items.

So I think your code should be like this-

var queryResults = searchQuery;
var startPage = EPiServer.Core.ContentReference.StartPage;
queryResults = queryResults.Filter(x => x.Ancestors().Match(startPage.ContentLink.ID));
var queryResults = queryResults .GetResults();

Johan Petersson - Aug 30, 2019 8:52
... and if you want compiling code:

var result = query
.Filter(x => x.Ancestors().Match(ContentReference.StartPage.ToString()))

Please also not that if you filter on start page you probably won't get any documents in the search result since they may be stored outside the star page.

Ravindra's question is a good suggestion, however it should be changed slightly to this

.Filter(x => x.Ancestors().Match(ContentReference.StartPage.ToString())) 
Aug 29, 2019 15:13

.FilterForVisitors() should already filter on current website. Every search document has a site ID property in the index. This filter will also handle expiry dates and permissions.

If you're using unified search then these filters are added to the query automatically, unless you specify that the search is not "public" (the parameter 'filterForPublicSearch' in .GetResult()).

Aug 29, 2019 16:48
Quan Mai - Aug 29, 2019 16:55
Unless I'm missing something obvious but the default implementation does not filter by site ... It does filter by language/published status in that language, for deleted and read access. But that's about it ...
Johan Petersson - Aug 29, 2019 16:58
Maybe it's just true for unified search then.
* 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.