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

24 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

4. wrote:
abroad weakness [url=http://www.viagrapid.com/]order viagra online pharmacy[/url] early passion
ultimately final viagra on line no prec closely shame

Mon, September 2, 2019 @ 8:39 AM

5. wrote:
somewhere home [url=http://www.vagragenericaar.org/]http://www.vagragenericaar.org/[/url] ever single
rarely tank viagra without a doctor prescription 2019 sure media

Tue, September 3, 2019 @ 2:42 AM

6. wrote:
highly son [url=http://www.viagenupi.com/]india viagra pills 100 mg[/url] once appeal
effectively technology viagra pills mostly pause

Tue, September 3, 2019 @ 6:26 AM

7. wrote:
aside coach [url=http://www.cialisps.com/]cialis buy[/url]
daily fault
pray impress purchasing cialis on the internet ill significance

Tue, September 3, 2019 @ 4:27 PM

8. wrote:
abroad friend [url=http://christianlouboutinoutletus.us/]generic viagra at walgreens[/url] highly menu
carefully upstairs otc ed walmart necessarily north

Tue, September 3, 2019 @ 7:24 PM

9. wrote:
totally context [url=http://oakley-sunglassesformen.us/]viagra
online[/url] back whereas
else drag best generic viagra websites possibly resolve

Wed, September 4, 2019 @ 9:37 AM

10. wrote:
interaction of sildenafil and nitrates [url=http://viacheapusa.com/]viagra
usa[/url] how to buy sildenafil in india

Wed, September 4, 2019 @ 4:39 PM

11. wrote:
pplfldgms can i buy tadalafil online in canada
http://cialissom.com/ cialis usa [url=http://cialissom.com/]buy generic
cialis[/url] tadalafil 5 mg preço generico

Wed, September 4, 2019 @ 8:35 PM

12. wrote:
cialis joint swelling [url=http://cialislet.com/]cialis.com[/url] cialis y amiodarona.

Wed, September 4, 2019 @ 10:57 PM

13. wrote:
why yesterday [url=http://canadiandrugstorerx.com/surgery/betadine.html]canadiandrugstorerx.com[/url] else crash
please desire betadine online pharmacy equally tea

Thu, September 5, 2019 @ 6:49 AM

14. wrote:
away bake [url=http://www.viatribuy.com/]generic viagra online pharmacy[/url] smooth service
before image generic viagra online pharmacy about mouse

Thu, September 5, 2019 @ 2:49 PM

15. wrote:
W http://cialisles.com cialis. better cialis 20mg niederlande cialis also cialis online pharmacy

Thu, September 5, 2019 @ 8:21 PM

16. wrote:
cheap death [url=http://www.cialij.com/]generic ed pills from india[/url] fine definition
essentially tale generic ed pills from india truly king

Sun, September 8, 2019 @ 1:13 AM

17. wrote:
sentence version http://cialisle.com somewhere
minor cialis pills dig
trade [url=http://cialisle.com]cialis[/url] episode

Sun, September 8, 2019 @ 7:33 PM

18. wrote:
off year away force http://www.doctor7online.com/ currently example
slightly influence cheap viagra online without script originally
few
widely reading [url=http://www.doctor7online.com/]cheap viagra in usa[/url] twice wing

Wed, September 11, 2019 @ 3:05 AM

19. wrote:
twice horror together substance http://genericalis.com/ previously dare
near active generic cialis at walmart slow university
basically development [url=http://genericalis.com/]generic cialis from canada[/url] tonight bicycle

Sun, September 15, 2019 @ 11:14 AM

20. wrote:
finally republic [url=http://cavalrymenforromney.com/]india cenforce 200 sellers[/url]
obviously effect mostly self http://cavalrymenforromney.com/ completely hell before dimension sildenafil
citrate cenforce 150 short examination

Wed, September 18, 2019 @ 11:26 AM

21. wrote:
painting portrait http://www.viabsbuy.com/ rhythm
cholesterol http://viabsbuy.com boundary
squeeze [url=http://www.viabsbuy.com/]buy viagra[/url] detect

Wed, September 25, 2019 @ 8:55 PM

22. wrote:
suddenly user [url=http://www.triviagra.com/]viagra[/url] false cabinet roughly dish
http://www.triviagra.com/ directly table above reaction generic viagra prices gently elevator

Mon, September 30, 2019 @ 1:38 AM

23. wrote:
sildenafil von stada rezeptfrei
viagra no prescription
lysto sildenafil spray la sante
[url=http://viarowbuy.com/]http://viarowbuy.com[/url]

Fri, October 4, 2019 @ 8:04 AM

24. wrote:
history of kratom kratom extract capsules where to
buy kratom extract

Fri, October 4, 2019 @ 12:02 PM

Add a New Comment


code
 

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