Skip to content

Instantly share code, notes, and snippets.

@RyanABailey
Created October 30, 2014 03:47
Show Gist options
  • Select an option

  • Save RyanABailey/fb692a2496959ba1127e to your computer and use it in GitHub Desktop.

Select an option

Save RyanABailey/fb692a2496959ba1127e to your computer and use it in GitHub Desktop.
sitecore search
var searchResults = new List<SearchResult>();
string searchTerm = Request.QueryString["term"];
var index = ContentSearchManager.GetIndex("content_index");
using (var context = index.CreateSearchContext())
{
var results = context.GetQueryable<MyResultItem>().Where(resultItem => resultItem.PageHeading.Like(searchTerm) || resultItem.PageContent.Contains(searchTerm)).GetResults();
foreach (var result in results)
{
var searchResult = new SearchResult();
var item = result.Document.GetItem();
searchResult.Title = item.Fields["Heading"].ToString();
string content = Utilities.Utility.HtmlRemoval.StripTags(item.Fields["Content"].ToString());
searchResult.Summary = Utilities.Utility.TruncateString(content, 300);
searchResult.Url = Sitecore.Links.LinkManager.GetItemUrl(item);
searchResults.Add(searchResult);
}
}
return View(searchResults);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment