Umbraco Examine Multiple Word Search using Razor

Umbraco Examine Multiple Word Search using Razor

After reading the excellent Umbraco blog post, Examining Examine and the Examine documentation on CodePlex I set about implementing an Umbraco Examine search. And sure enough it is not too difficult to setup. For an explanation of the various configuration properties please read Examining Examine

config/ExamineIndex.config

Firstly edit config/ExamineIndex.config to create an Index Set.

<IndexSet SetName="SiteIndexSet" IndexPath="~/App_Data/TEMP/ExamineIndexes/Site/">
  <IndexAttributeFields>
    <add Name="id"/>
    <add Name="nodeName"/>
    <add Name="updateDate"/>
    <add Name="writerName"/>
    <add Name="nodeTypeAlias"/>
    <add Name="parentID"/>
  </IndexAttributeFields>
  <IndexUserFields>
    <add Name="bodyText"/>
  </IndexUserFields>
  <IncludeNodeTypes>
    <add Name="Page"/>
  </IncludeNodeTypes>
  <ExcludeNodeTypes />
</IndexSet>

config/ExamineSettings.config

Next edit ExamineSettings.config to create an Index Provider.

<ExamineIndexProviders>
  <providers>
    ...
    <add name="SiteIndexer" type="UmbracoExamine.UmbracoContentIndexer, UmbracoExamine"
      dataService="UmbracoExamine.DataServices.UmbracoDataService, UmbracoExamine"
      supportUnpublished="false"
      supportProtected="false"
      runAsync="false"
      interval="10"
      analyzer="Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net"
      enableDefaultEventHandler="true"/>
  </providers>
</ExamineIndexProviders>
<ExamineSearchProviders defaultProvider="InternalSearcher">
  <providers>
    ...
    <add name="SiteSearcher" type="UmbracoExamine.UmbracoExamineSearcher, UmbracoExamine"
      analyzer="Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net"/>
  </providers>
</ExamineSearchProviders>

Search.cshtml

Last of all create a Razor script to build, execute and render the search query results.

@using Examine
@using UmbracoExamine

@inherits umbraco.MacroEngines.DynamicNodeContext

@{
  // Store the search value
  var term = Request["searchTerm"];
  //   
  var criteria = ExamineManager.Instance.SearchProviderCollection["SiteSearcher"]
    .CreateSearchCriteria();
  Examine.SearchCriteria.IBooleanOperation filter = null;
  // Create array of node fields to be searched
  var fields = new string[] { "nodeName", "bodyText" };
  // Split & iterate each word within the search term
  foreach (var t in term.Split(' '))
  {
    if (filter == null)
    {
      filter = criteria.GroupedOr(fields, t);
    }
    else
    {
      filter = filter.And().GroupedOr(fields, t);
    }
  }
  if(filter == null)
  {
    filter = criteria.GroupedOr(fields, term);
  }
  else
  {
    filter = filter.And().GroupedOr(fields, term);
  }
  // Not include pages hidden from navigation
  filter.Not().Field("umbracoNaviHide", "1");
  // Execute the search query & order by score
  var results = Examine.ExamineManager.Instance
    .SearchProviderCollection["SiteSearcher"]
    .Search(filter.Compile())
    .OrderByDescending(x => x.Score);
}
// Display search results
@if(results.Count() > 0)
{
  <p>@results.Count() found for @term</p>
  foreach(var item in results)
  {
    @item
  }
}
else
{
  <p>No results found for @term</p>
}