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>
}

3 comments (Add your own)

1. STEWbype wrote:
There are different ways to fry tomatoes, but each of them will require the hostess to spend row hours in the kitchen, so this food is usually better correct do on weekends or for special occasions. When tomatoes are roasted, they get a deep taste and are combined with seafood, antipasto and other roasted vegetables. Moreover, they are ideally suitable for use in the baking industry, in making bread or cake with custard.
http://stewedtomatoes.top/

Tue, July 10, 2018 @ 3:52 PM

2. KIRAtold wrote:
To determine internal swelling is difficult enough, therefore, consequently, they influence the body for a long time, which threatens the normal fetus. Fluids are dangerous because they break blood circulation. Such situation leads to the strengthening of negative tendencies feeding and the breath baby, created hypoxia.
Fighting such a pathology should be done with the help of correction feeding and special exercises so that water does not stay tissues. If the woman is resting, then under the feet better put a cushion or pillow to improve the blood circulation of tired legs. Prohibited long time to sit or stand, as this leads to stagnation in the body. It is recommended that the knee-elbow position several times a day in order to increase blood flow.
swollen feet after giving birth

Wed, July 11, 2018 @ 4:17 PM

3. Daniele wrote:
This snippet is working, except for this part

if(filter == null)
{
filter = criteria.GroupedOr(fields, term);
}
else
{
filter = filter.And().GroupedOr(fields, term);
}

this just is the reason why the search normally can't match multiple terms strings. If you remove that and change the for like this

var words = term.Split(' ');
for (int i = 0; i < words.Count(); i++)
{
var t = words[i];
if (i == words.Count() -1)
{
if (filter == null)
{
filter = searchCriteria.GroupedOr(fields, t.MultipleCharacterWildcard());
}
else
{
filter = filter.And().GroupedOr(fields, t.MultipleCharacterWildcard());
}
}
else
{
if (filter == null)
{
filter = searchCriteria.GroupedOr(fields, t);
}
else
{
filter = filter.And().GroupedOr(fields, t);
}
}
}

you can reach the goal of get multiple term search with trailing wildcard.

Tue, May 21, 2019 @ 11:41 AM

Add a New Comment


code
 

Comment Guidelines: No HTML is allowed. Off-topic or inappropriate comments will be edited or deleted. Thanks.