By default in Sharepoint whole web application is crawled and content from all site collections in this web application is shown on all site collections. What to do if you need to limit search results to the current site collection only, i.e. when you perform search on /sites/test1 site collection, it will show content only from this site collection on search results page? You can create new content source in search service application, specify URL of specific site collection and then configure search results web part to use this specific content source. However this approach requires more work, and if you have a lot of site collection, it will be significant disadvantage.
There is another simpler way to limit search results to the current site collection. Go to Site settings > Search results source and create new default result source (I wrote about how to do it in the following post: Exclude AllItems.aspx from search results in Sharepoint 2013). After that you need to modify Query transform field of the copied result source: add the following rule “Path:{SiteCollection.URL}”. E.g. if you used Local SharePoint Results result source as a base, then you should have the following rules:
{?{searchTerms} -ContentClass=urn:content-class:SPSPeople Path:{SiteCollection.URL}}
After this save the result source. If it is set by default, then OTB search results web parts will use it without additional actions.
I have tested with your method. However when I input nothing, the search will return all the item within the site collection. (just like search wildcard) How do you prevent it happen?
ReplyDeletelegendgod,
ReplyDeleteI don't remember about this particular scenario, but if it is OTB behavior, most probably we didn't prevent it at all. One of the ways of how you may ensure that user entered something is to check it in javascript (search box has own javascript handler attached to search icon. You may override it with custom function which will check that entered value is not empty first and if it is not, call original handler which will redirect user to the search results page).
If I want to get the search results from all site collections within my web application, where will io create the result source and what's the query transform string I need to build. I don't want my search will pull the results from other web applications.
ReplyDeleteI have created and enabled result sources at the search service appln level for single site collection and 2 site collections. and it worked. But if the number of site collections are more than 50 within my web application, how can I enter all the site collections path in the query transform text box? if I give all the 50 site collections' name, will my search work?
ReplyDeleteThanks
Samol
hi Samol,
ReplyDeleteinstead if enumerating all site collections' urls in query transform, try to put only web application's url there:
Path:http://example.com