How to get highlighted results using Solr and Solrj

29 / Jan / 2016 by Priyanku Dwivedi 1 comments

UseCase

I am using Apache Solr as a search platform integrated with AEM CMS. 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’t have javascript enabled.

Solution

To achieve the highlighting functionality I used Solr’s out of the box Highlighting Component  which returns the highlighted text snippets of matching text in the original data.

Query Parameters required to achieve highlighting are as follows:-

hl – Should be set to true as it enables highlighted snippets to be generated in the query response.

hl.fl – Specifies a list of fields to highlight. Wild char * will highlight all the fields

hl.fragsize – The size, in characters, of the snippets (aka fragments) created by the highlighter. In the original Highlighter, “0” indicates that the whole field value should be used with no fragmenting. By default fragment is of size 100 characters

hl.simple.pre – Specifies the text that should appear before a highlighted term.(e.g. <span>). It defaults to <em>

hl.simple.post –  Specifies the text that should appear after a highlighted term. (e.g. </span>). It defaults to </em>

 

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.

[java]
private SolrQuery prepareQuery(SlingSolrSearchRequest request) {

    String query = request.query;

    SolrQuery queryParams = new SolrQuery();

    queryParams.set(CommonParams.Q, <query-Search-Term>);
 

//Setting Highlighting parameters using Solrj

//this will set the all the highlighting parameter to default values

    queryParams.setHighlight(true).setHighlightSnippets(1);
    queryParams.setParam("hl.fl", "*");

    queryParams.setParam("hl.fragsize", "0");

    return queryParams;

}

SolrQuery queryParams = prepareQuery(request);

SolrClient solrClient = getSolrClient()// returns SolrClient

queryResponse = solrClient.query(queryParams);
[/java]

When the above code is executed it will generate following queryString

q=title%3A<searchText>&hl=true&hl.snippets=1&hl.fl=*&hl.fragsize=0

The Response from the Solr will have highlighting element. It will have the results with highlighted hits as

[javascript]

"highlighting": {
"/content/<page1>/<page2>/<page3>": {
"indexed_field1": ["01 |<em>Search-Term</em> This is highlighted"],
"title": ["<em> Search-Term </em> This got highlighted"]
}
}
[/javascript]

Following code snippet will read highlighted result data .

[code]
String content =queryResponse.getHighlighting().get(id).get("title")[0];
[/code]

FOUND THIS USEFUL? SHARE IT

comments (1 “How to get highlighted results using Solr and Solrj”)

  1. Cedric

    Does this code work with Java? As I am also trying to do the Solr highlight function. But I am doing on Java Eclipse, JSP & Servlet. Able to give some example on how do I achieved it? Basically, when user search a particular word. When the result come out, the word they search will be highlighted. Thank you.

    Reply

Leave a Reply to Cedric Cancel reply

Your email address will not be published. Required fields are marked *