I was given a challenge to develop a mechanism to list all pages in the current site and its sub-sites. Why not do it using Content By Search Web Part was my initial thought. In the end I managed to achieve this, and the prototype looks like below.
One of the most satisfying things in this exercise was coming up with the query to return the right search results, which is:
SPSiteUrl:{SiteCollection} Path:{Site} (IsDocument:”True” OR contentclass:STS_Site OR contentclass:STS_Web) FileExtension:aspx
SPSiteUrl:{SiteCollection} is used to restrict items to the current site collection. Without this clause, other site collections that are under managed paths (e.g. http://server/sites/mySC) may be included in the result when searching from the root site collection (e.g. http://server).
Path:{Site} is used to restrict items to those under the current site’s URL.
The rest should be self-explanatory.
The grouping/indenting by sites is performed in my custom display templates. To achieve this I had to ensure that the items are returned in the correct order, i.e. ordered by site and by URL depth.
I configured my search result to be sorted by SPWebUrl, then UrlDepth, and finally Path.
Note that the managed property SPWebUrl does not exist OOTB (at the time of writing). I had to create this in Central Admin and map it to the ows_taxId_SPLocationSite crawled property. I had to enable Query, Retrieve and Sort on this managed property.
The other thing is Sort is also not enabled by default for the Path managed property. I also had to change this in Central Admin.
If you are interested in the display templates then let me know and I’ll update the post.
would you please elaborate as i want to restrict the search results in one site collection.