{"id":31954,"date":"2016-01-29T10:48:00","date_gmt":"2016-01-29T05:18:00","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=31954"},"modified":"2016-02-08T13:11:15","modified_gmt":"2016-02-08T07:41:15","slug":"how-to-get-highlighted-results-using-solr-and-solrj","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-get-highlighted-results-using-solr-and-solrj\/","title":{"rendered":"How to get highlighted results using Solr and Solrj"},"content":{"rendered":"<p><span style=\"text-decoration: underline;\"><strong>UseCase<\/strong><\/span><\/p>\n<p>I am using Apache Solr as a search platform integrated with <a title=\"AEM Consulting Services\" href=\"http:\/\/www.tothenew.com\/wcm\/cq-aem-development-consulting\">AEM\u00a0CMS<\/a>. I had a requirement where I needed to highlight the matching part of the text in the search results. Another part was that it should work even if the browser doesn\u2019t have javascript enabled.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Solution<\/strong><\/span><\/p>\n<p>To achieve the highlighting functionality I used Solr\u2019s out of the box Highlighting Component\u00a0 which returns the highlighted text snippets of matching text in the original data.<\/p>\n<p>Query Parameters required to achieve highlighting are as follows:-<\/p>\n<p><strong>hl<\/strong> \u2013 Should be set to true as it enables highlighted snippets to be generated in the query response.<\/p>\n<p><strong>hl.fl<\/strong> \u2013 Specifies a list of fields to highlight. Wild char * will highlight all the fields<\/p>\n<p><strong>hl.fragsize <\/strong>\u2013 The size, in characters, of the snippets (aka fragments) created by the highlighter. In the original Highlighter, &#8220;<tt class=\"backtick\">0<\/tt>&#8221; indicates that the whole field value should be used with no fragmenting. By default fragment is of size 100 characters<\/p>\n<p><strong>hl.simple.pre<\/strong> \u2013 Specifies the text that should appear before a highlighted term.(e.g. &lt;span&gt;). It defaults to &lt;em&gt;<\/p>\n<p><strong>hl.simple.post<\/strong> \u2013\u00a0 Specifies the text that should appear after a highlighted term. (e.g. &lt;\/span&gt;). It defaults to &lt;\/em&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>I have used Solrj, a java client to communicate with Solr. Following is the code to set various parameters in Solrj so as to make Solr return highlighted results.<\/p>\n<p>[java]<br \/>\nprivate SolrQuery prepareQuery(SlingSolrSearchRequest request) {<\/p>\n<p>\u00a0\u00a0\u00a0 String query = request.query;<\/p>\n<p>\u00a0\u00a0\u00a0 SolrQuery queryParams = new SolrQuery();<\/p>\n<p>\u00a0\u00a0\u00a0 queryParams.set(CommonParams.Q, &lt;query-Search-Term&gt;);<br \/>\n \u00a0<\/p>\n<p>\/\/Setting Highlighting parameters using Solrj<\/p>\n<p> \/\/this will set the all the highlighting parameter to default values<\/p>\n<p>\u00a0\u00a0\u00a0 queryParams.setHighlight(true).setHighlightSnippets(1);<br \/>\n\u00a0 \u00a0 queryParams.setParam(&quot;hl.fl&quot;, &quot;*&quot;);<\/p>\n<p>\u00a0\u00a0\u00a0 queryParams.setParam(&quot;hl.fragsize&quot;, &quot;0&quot;);<\/p>\n<p>\u00a0\u00a0\u00a0 return queryParams;<\/p>\n<p>}<\/p>\n<p> SolrQuery queryParams = prepareQuery(request);<\/p>\n<p> SolrClient solrClient = getSolrClient()\/\/ returns SolrClient<\/p>\n<p> queryResponse = solrClient.query(queryParams);<br \/>\n[\/java]<\/p>\n<p>When the above code is executed it will generate following queryString<\/p>\n<p><strong>q=title%3A&lt;searchText&gt;&amp;hl=true&amp;hl.snippets=1&amp;hl.fl=*&amp;hl.fragsize=0<\/strong><\/p>\n<p>The Response from the Solr will have highlighting element. It will have the results with highlighted hits as<\/p>\n<p>[javascript]<\/p>\n<p>&quot;highlighting&quot;: {<br \/>\n &quot;\/content\/&lt;page1&gt;\/&lt;page2&gt;\/&lt;page3&gt;&quot;: {<br \/>\n &quot;indexed_field1&quot;: [&quot;01 |&lt;em&gt;Search-Term&lt;\/em&gt; This is highlighted&quot;],<br \/>\n &quot;title&quot;: [&quot;&lt;em&gt; Search-Term &lt;\/em&gt; This got highlighted&quot;]<br \/>\n }<br \/>\n}<br \/>\n[\/javascript]<\/p>\n<p>Following code snippet will read highlighted result data .<\/p>\n<p>[code]<br \/>\nString content =queryResponse.getHighlighting().get(id).get(&quot;title&quot;)[0];<br \/>\n[\/code]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>UseCase I am using Apache Solr as a search platform integrated with AEM\u00a0CMS. I had a requirement where I needed to highlight the matching part of the text in the search results. Another part was that it should work even if the browser doesn\u2019t have javascript enabled. Solution To achieve the highlighting functionality I used [&hellip;]<\/p>\n","protected":false},"author":552,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":27},"categories":[1],"tags":[3075,3076,1874,3028,3029],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/31954"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/552"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=31954"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/31954\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=31954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=31954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=31954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}