In a view the IExamineManager can be injected as well:
@inject IExamineManager ExamineManager;
This returns an active instance of the ExamineManager which exposes operations such as:
Default index & search providers
Full collection of index & search providers
All indexing and searching methods
Searching
Important to note that the Search methods on the ExamineManager will call the Search methods of the default search provider specified in config. If you want to search using a specific provider, there are generally two approaches for this.
If you want to use the searcher of a specific index, you should get the the searcher via the index:
@inherits UmbracoViewPage@using Examine@using Umbraco.Cms.Core@inject IExamineManager ExamineManager@{ // Get the search text from the query stringstring query =Context.Request.Query["query"]; // Try to get the "ExternalIndex" from the Examine managerif (ExamineManager.TryGetIndex(Constants.UmbracoIndexes.ExternalIndexName,outIIndex index)) { // Search via the searcher of the indexISearchResults searchResults =index.Searcher.Search(query); // Check whether the search revealed any resultsif (searchResults.Any()) {<ul>@foreach (ISearchResult result in searchResults) { // Skip the result if the ID is nullif (result.Idisnull) continue; // Skip the result if not found in the content cacheif (Umbraco.Content(result.Id) isnot {} node) continue;<li><a href="@node.Url()">@node.Name</a></li> }</ul> } }}
If you have configured a custom searcher that you wish to use instead, you can access the searcher directly via the IExamineManager instance:
@inherits UmbracoViewPage@using Examine@inject IExamineManager ExamineManager@{ // Get the search text from the query stringstring query =Context.Request.Query["query"]; // Try to get the "MyCustomSearcher" searcherif (ExamineManager.TryGetSearcher("MyCustomSearcher",outISearcher searcher)) { // Search via the searcherISearchResults searchResults =searcher.Search(query); // Check whether the search revealed any resultsif (searchResults.Any()) {<ul>@foreach (ISearchResult result in searchResults) { // Skip the result if the ID is nullif (result.Idisnull) continue; // Skip the result if not found in the content cacheif (Umbraco.Content(result.Id) isnot {} node) continue;<li><a href="@node.Url()">@node.Name</a></li> }</ul> } }}
Indexing
When you wanna populate an index, you will need to use the IExamineManager and get the specific index. The build-in index names are all available as constants from the Umbraco.Cms.Core.Constants.UmbracoIndexes namespace
if (_examineManager.TryGetIndex(Umbraco.Cms.Core.Constants.UmbracoIndexes.ExternalIndexName,outIIndex index)){ // Use index here}
The indexing methods available on a single index are: