Searchable Trees (ISearchableTree)
This page is a work in progress and may undergo further revisions, updates, or amendments. The information contained herein is subject to change without notice.
When you type a search term into the Umbraco backoffice search field, you'll see search results from all the Section Trees that your user account has permission to access:
The results are grouped by 'Section Tree' like Content, Media, Document Types. Each 'Tree' has its own associated search mechanism that receives the search term and looks for matches in the tree that is responsible for searching.
You can create your own search mechanisms for your own custom sections or replace the default search implementation for a particular section tree.
Custom Tree Search
To create a search for your own custom tree you need to create a C# class that implements the interface Umbraco.Cms.Core.Trees.ISearchableTree
.
ISearchableTree
Your implementation needs to return an IEnumerable of SearchResultEntity
items:
A SearchResultEntity
consists of a Score (a Float value) identifying its relevance to the search term, and the set of EntityBasic
properties that all Umbraco objects share: eg Name, Id, Udi, Icon, Trashed, Key, ParentId, Path, Alias, AdditionalData.
Example implementation of ISearchableTree
If we have a custom section Tree with the alias 'favouriteThingsAlias' (see the custom tree example) then we could implement searchability by creating the following C# class in our site:
That's all we need, after an application pool recycle, if we now search in the backoffice we'll see matches from our custom 'Favourite Things' tree:
Umbraco automatically finds any implementation of ISearchableTree
in your site and automatically configures it to be used for the custom section mentioned in the TreeAlias property. Be careful not to accidentally have two ISearchableTree
implementations trying to search the 'same' TreeAlias, it's one ISearchableTree
per TreeAlias.
Replacing an existing Section Tree Search
Perhaps you want to change the logic for searching an existing section of the site, (why? - well you might have a 'company name' property on a MemberType in the Member section, and you want searches for that company name to filter the members who work there, the default implementation will only search on Member Name).
Or perhaps you want to replace Examine search in the backoffice with an external Search Service, e.g. Azure Search. In a cloud-hosted implementation you don't need to build the Examine indexes on each new server as your cloud hosting scales out.
Example
First create your replacement custom ISearchableTree
implementation, using the same approach as above, but specifying the TreeAlias of the Tree you aim to replace, e.g. 'Member'.
To avoid your custom implementation clashing with the default ISearchableTree
for a Tree, you need to remove its ISearchableTree
implementation from the collection of SearchableTrees using an IComposer
when Umbraco starts up:
This would then allow your custom implementation of ISearchableTree
with TreeAlias 'member' to be used when searching the Member Section Tree.
Last updated