Using Highlighting Component in Solr
Posted By : Jasgeet Singh | 01-May-2013
Search components must be registered with Solr to be activated so that they can then be referred to in a components list. All of the standard components are pre-registered. Here's an example of how a search component named “elevator” is registered in solrconfig.xml:
<searchcomponent class="solr.QueryElevationComponent" name="elevator"> <str name="queryFieldType">string</str> <str name="config-file">elevate.xml</str> </searchcomponent>
The Highlighting Component
The component is using for returning highlighted text snippets of matching text in the original data.
The following are the parameters observed by the highlighter search component:
• hl: Set to true to enable search highlighting. Without this, the other parameters are ignored, and highlighting is effectively disabled.
SolrQuery solrQuery = new SolrQuery(“*”) solrQuery.setHighlight(true)
• hl.fl: A comma or space separated list of fields that will be highlighted. It is important for a field to be marked as stored in the schema in order to highlight on it. You may use an asterisk wildcard to conveniently highlight on all of the text fields, such as * or r_*. If you use a wildcard, then consider enabling the hl.requireFieldMatch option.
SolrQuery solrQuery = new SolrQuery(“*”) solrQuery.setHighlight(true) solrQuery.setHighlightRequireFieldMatch(true)
• hl.snippets: This is the maximum number of highlighted snippets that will be generated per field. It defaults to 1, which you will probably not change. By setting this to 0 for a particular field, you can effectively disable highlighting for that field. You might do that if you used a wildcard for hl.fl and want to make an exception.
SolrQuery solrQuery = new SolrQuery(“*”) solrQuery.setHighlight(true) solrQuery.setHighlightSnippets(1)
• hl.fragsize: The maximum number of characters returned in each snippet, measured in characters. The default is 100. If 0 is specified, then the field is not fragmented and whole field values are returned. Obviously, be wary of doing this for large text fields.
SolrQuery solrQuery = new SolrQuery(“*”) solrQuery.setHighlight(true) solrQuery.setHighlightSnippets(1) solrQuery.setHighlightFragsize(100)
• hl.formatter: Choose how to format the highlighting. This parameter refers to a named <formatter/> element in <highlighting/> in solconfig.xml. The default implementation named html further specifies two more parameters:
° hl.simple.pre and hl.simple.post: This is the text that will be inserted immediately before and after matched terms in the snippet in order to demarcate them from the surrounding text. Note that the circumstantial presence of whatever values are chosen in the original text, such as HTML with pre-existing emphasis tags, are not escaped, and in rare circumstances may lead to a false highlight.
SolrQuery solrQuery = new SolrQuery(“*”) solrQuery.setHighlight(true) solrQuery.setHighlightSimplePre(“before”) solrQuery.setHighlightSimplePost(“after”)
Hope it helps!
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Jasgeet Singh
Jasgeet is a Sr. Lead developer .He is an experienced Groovy and Grails and has worked on designing & developing B2B and B2C portals using Grails technologies.