Skip to content

Instantly share code, notes, and snippets.

@sleslie321
Last active August 29, 2015 14:10
Show Gist options
  • Save sleslie321/98d07fed73187abaacc2 to your computer and use it in GitHub Desktop.
Save sleslie321/98d07fed73187abaacc2 to your computer and use it in GitHub Desktop.
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@using Examine
@using Examine.SearchCriteria
@using Examine.LuceneEngine.SearchCriteria
@{
//Get search query
var q = Request.QueryString["q"];
//Check if searchQuery is null from the posted form data...
if (q == null)
{
//If it is null then the form was not posted and the page was visited directly
<p>Please use the search box</p>
//Stop all other code running in this Macro
return;
}
var q_split = q.Split(' ');
var Searcher = ExamineManager.Instance.SearchProviderCollection["ContentSearcher"];
var searchCriteria = Searcher.CreateSearchCriteria(Examine.SearchCriteria.BooleanOperation.Or);
var fieldsToSearch = new[]
{
"nodeName", "bodyText", "headerText", "description"
};
IBooleanOperation filter = searchCriteria.GroupedOr(fieldsToSearch, q_split.First());
foreach (var term in q_split.Skip(1))
{
filter = filter.Or().GroupedOr(fieldsToSearch, term);
}
var searchResults = Searcher.Search(filter.Compile()).OrderByDescending(x => x.Score).TakeWhile(x => x.Score > 0.5f);
var noResults = searchResults.Count();
<form action="/library/new-content-search/">
<div id="searchLibraryBox" >
<div>
<input type="text" id="search-box" name="q" style="width:500px" value="@q" />&nbsp;&nbsp;
<input type="submit" class="button" style="padding-left: 10px;" name="LibGo" id="LibGo" value="Search" />
</div>
</div>
<p>You searched for <strong><em>@q</em></strong>, and found <strong><em>@noResults</em></strong> results</p>
<ul>
@foreach (var result in searchResults)
{
<li>
<a href="@umbraco.library.NiceUrl(result.Id)" >@result.Name</a>
</li>
}
</ul>
</form>
</div>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment