Umbraco Multisearcher Examine example

Using Multisearcher Examine provider to searching across multiple indexes


Posted on 4/12/2017 12:00:00 AM


I have blogged about using Examine to search your content in umbraco before , Search Umbraco using Examine and Razor; and there is good articles on setting up Examine available on the web - Rasmus Fjord 24days article is a good start, along with blog on Umbraco.com : Examining Examine and the documentation on Our Umbraco is good reference.

A common question appearing on twitter and the umbraco forums is how to search two or more indexes. 

It is fairly straight forward - using the provider in the Examine namespace (source code on Github )

I have created a project available over at bit bucket which demonstrates (in a simple example) searching both content items in the umbraco project and pdf media items (including text within the pdf using UmbracoExamine.PDF)

Steps

  • Setup content index (in this case ExternalIndexer) - you can see this working on it's own in the  'single-search' branch.
  • Setup pdf search index ( in this case PDFIndexer) - you can see this working in the 'PDFSearch' branch.
  • The final step was to introduce the MultiSearcher in the ExamineSettings.config :
 <add name="MultiSearcher" type="Examine.LuceneEngine.Providers.MultiIndexSearcher, Examine"
          analyzer="Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net" enableLeadingWildcard="true" indexSets="ExternalIndexSet,PDFIndexSet" />

 Code of search razor code shown below.

@using Examine.Providers
@using Umbraco.Web
@using UmbracoExamine
@using Examine.SearchCriteria
@using Examine
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage

@{
BaseSearchProvider _searcher = ExamineManager.Instance.SearchProviderCollection["MultiSearcher"];

var fieldsToSearch = new[]
{
"contents","FileTextContent" };


var criteria = _searcher.CreateSearchCriteria(BooleanOperation.Or);

var searchTerm = string.Empty;
searchTerm = string.IsNullOrEmpty(Request["q"]) ? string.Empty : Request["q"];
if (searchTerm == string.Empty)
{}
else
{
var examineQuery = criteria.Field("contents",searchTerm)
.Or()
.Field("FileTextContent",searchTerm); var results = _searcher.Search(examineQuery.Compile());
var umbraco = new UmbracoHelper(UmbracoContext.Current);
if (results.Any())
{
<div style="margin-top:20px">
<p>You search for ""<strong>@searchTerm</strong>" found @results.Count() results:</p>
</div>
<ul class="list-group col-md-8">
@foreach (var result in results)
{
if (result.Fields.ContainsKey("__IndexType") && result.Fields["__IndexType"].ToLower() == "media")
{
var media = umbraco.TypedMedia(result.Id);
<li class="list-group-item justify-content-between">
<span class="search-title"><a href="@media.Url">@media.Name</a></span>
<span class="badge badge-default badge-pill">@result.Score</span>
</li>

}
else
{
var node = umbraco.TypedContent(result.Id);
<li class="list-group-item justify-content-between">
<span class="search-title"><a href="@node.Url">@node.Name</a></span>
<span class="badge badge-default badge-pill">@result.Score</span>
@if (result.Fields.ContainsKey("description"))

{

<p> @result.Fields["description"]</p>

}

</li>

}

}
</ul>

else
{

<p>No results found for query @searchTerm</p>

}
<div class="col-md-12" style="padding-left:0px;padding-right:0px;">
<pre class="bg-info">Examine criteria: @criteria.ToString()</pre>
</div>
}
}

 Please note, a gotcha is that in Umbraco backoffice, within Examine Management, using the multisearcher 'Search Tools' no results will be displayed.

 


Umbraco
Umbraco Certified Expert