Skip to content

Instantly share code, notes, and snippets.

@christofur
Created March 23, 2018 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christofur/178b3a8ff93d6a899c00b15dcdd49966 to your computer and use it in GitHub Desktop.
Save christofur/178b3a8ff93d6a899c00b15dcdd49966 to your computer and use it in GitHub Desktop.
Highlighting With Sitecore and Solr
@using Sitecore.ContentSearch
@using Sitecore.ContentSearch.Linq.Parsing
@using Sitecore.ContentSearch.SearchTypes
@using Sitecore.ContentSearch.SolrProvider.SolrNetIntegration
@using SolrNet
@using SolrNet.Commands.Parameters
@{
const string searchField = "summary_t";
const string searchValue = "traditional";
var queryOptions = new QueryOptions
{
Highlight = new HighlightingParameters
{
Fields = new[] { searchField },
BeforeTerm = "<em>",
AfterTerm = "</em>"
},
ExtraParams = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("hl.method", "unified")
}
};
var index = ContentSearchManager.GetIndex(string.Format("sitecore_{0}_index", Sitecore.Context.Database.Name));
using (var context = index.CreateSearchContext())
{
var results = context.Query<SearchResultWithSummary>(new SolrQueryByField(searchField, searchValue), queryOptions);
foreach (var result in results)
{
var highlights = results.Highlights[result.Fields["_uniqueid"].ToString()];
if (highlights.Any())
{
<ul>
@foreach (var highlight in highlights)
{
<li style="color: #696969">@result.Name</li>
<li>@Html.Raw(string.Join(",", highlight.Value))</li>
}
</ul>
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment