Reputation: 9
I am new to solr and have been asked to implement search result highlighting. My search query is something like this,
query = text like 'searchterm1' AND 'searchterm2'
Now I need to instruct solr to highlight 'searchterm1', 'searchterm2' in yellow and blue colors respectively. Is this even possible to do with solr? and if yes how do I go about it?
This is the code snippet that is currently highlighting the search result.
private AbstractSolrQuery _query = new SolrQuery(searchterm1) && new SolrQuery(searchterm2);
public SolrQueryResults<Document> Execute(string defaultField)
{
var results = _solr.Query(_query, new QueryOptions
{
Rows = 100,
Fields = Document.GetPropertiesExceptList(new List<string>{"text","text_exact"}).ToArray(),
ExtraParams = new Dictionary<string, string> {
{ "df", defaultField },
{ "hl.fragsize", "0" },
{ "hl", "true" }
}
});
Highlight(results);
return results;
}
private void Highlight(SolrQueryResults<Document> results)
{
foreach (var result in results)
{
foreach (var highlightedSnippet in results.Highlights[result.Id.ToString()])
{
result.SetProperty("content", highlightedSnippet.Value.ToList());
}
}
}
Any help with this would be very much appreciated,
Thanks
Upvotes: 1
Views: 1206
Reputation: 26012
You can set the following parameters into the requestHandler
definition in solrconfig.xml
to highlight in different color. You can set the background color as you want.
<str name="hl.simple.pre"><b style="background:yellow"></str>
<str name="hl.simple.post"></b></str>
The full example requestHandler could be something like :
<requestHandler name="/select" class="solr.SearchHandler">
<lst name="defaults">
<str name="echoParams">explicit</str>
<int name="rows">10</int>
<str name="df">text</str>
<!-- Highlighting defaults -->
<str name="hl">on</str>
<str name="hl.fl">*</str>
<str name="hl.simple.pre"><b style="background:yellow"></str>
<str name="hl.simple.post"></b></str>
</lst>
</requestHandler>
For details you can check HighlightingParameters.
Upvotes: 0
Reputation: 52799
You can check upon hl.fragmentsBuilder which will allow you to return the snippets in different color. I am just not sure if it applies to search terms, matches or field.
Upvotes: 2