Created
March 23, 2018 14:50
-
-
Save christofur/178b3a8ff93d6a899c00b15dcdd49966 to your computer and use it in GitHub Desktop.
Highlighting With Sitecore and Solr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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