Search Umbraco using Examine and Razor

walk through to create a simple search using razor and Examine in Umbraco

Posted on Friday, November 28, 2014


STEP ONE : Examine Configuration

(Ref : http://our.umbraco.org/documentation/Reference/Searching/Examine/full-configuration)

Within umbraco site in the 'Config' folder there are two relevant config files:

  • ExamineIndex.config
  • ExamineSettings.config

ExamineIndex.config

within this file we want to create a indexset. This file contains info on which doctypes and fields to index, also defining the path (IndexPath) - where the index will be stored on the filesystem.

eg the default out of the box contains a SetName of 'ExternalIndexSet' whuch will index all fields and all types of nodes.

<IndexSet SetName="ExternalIndexSet" IndexPath="~/App_Data/TEMP/ExamineIndexes/External/" />

you can of course configure things like which fields and documentTypes that are indexed or excluded. eg

<IndexSet SetName="ContentSearch" IndexPath="~/App_Data/ExamineIndexes/MySearch/">
    <IndexAttributeFields>
      <add Name="id" />
      <add Name="nodeName"/>
      <add Name="updateDate" />
      <add Name="writerName" />
      <add Name="nodeTypeAlias" />
    </IndexAttributeFields>
    <IndexUserFields>
      <add Name="bodyText"/>
      <add Name="siteName"/>
    </IndexUserFields>
    <IncludeNodeTypes>
      <add Name="umbHomePage" />
      <add Name="umbNewsItem" />
      <add Name="umbTextPage" />
    </IncludeNodeTypes>
  </IndexSet>

 

 ExamineSettings.config

Inside this configuration file you need to do 2 things, namely add a index provider an search provider.

eg Index Provider:

add name="ContenSearchIndexer" type="UmbracoExamine.UmbracoContentIndexer, UmbracoExamine"
      supportUnpublished="false"
      supportProtected="true"
      interval="10"
      analyzer="Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net"
      indexSet="ContentSearch"/>

 Note the indexSet name must match the IndexSet that was added to the ExamineIndex.config file above.

and Search provider:

<add name="ContentSearchSearcher" type="UmbracoExamine.UmbracoExamineSearcher, UmbracoExamine"
       analyzer="Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net" indexSet="MySearch" enableLeadingWildcards="true"/>

 You can then view the created index with Umbraco's backoffice within the Developer section. "Examine Management". You can view the settings and do a quick search to view what content items are in your search index.

You can many tools to look at the lucence index (LUKE for example) or the Examine Inpsector package. (not sure if this works in Umbraco ver 7 -ed)

Razor Script

 

Notes:

line 22: sometimes developers find that no no results are not matched or not returned, however ths setting will force the search to into search "OR" thereby finding best matches.

line 35 : The orderbydescending filters out "low scoring matches"- 0.5f seems to be a good setting - but you can of cource change this - it is a scale upto 1.

line 50 : <a href="@umbraco.library.NiceUrl(result.Id)" >@result.Name</a>
Clearly you may wish to bring back your own Fields - simple use result.Fields["fieldname"], you may want to check the Field exists/is not null for the searchresults before trying to access it - or your code will blow up! eg.

@if(result.Fields.ContainsKey("description"))
{
 @result.Fields["description"];

 

 * References: