Varun
Varun

Reputation: 526

Higlight words in the search results of solr

I have an application which is using solr as a search engine, and displaying search results in a data grid ASP.Net site. Now I need to highlight "searched word" partially or fully in the data grid.

Like let us say I am searching "California" Then I need to highligt california as a word anywhere found in the resul grid.

If I have some IDs like 'CA 0012*' Then I need to highlight CA, California, 0012*.. and CA 0012 also.

I want to write this logic in C# 4.

Thanks in advance...:)

Upvotes: 1

Views: 705

Answers (2)

Imran Rizvi
Imran Rizvi

Reputation: 7438

I have written similar logic for my Telerik Hierarchy grid in java script.

First I load the found record to the grid (searching server side)

Then I used following jQuery to highlight found rows.

the main logic is to create a SPAN dynamically around found text and give it a css class that shows it highlight by changing background (or any other thing you want).

Following is the jQuery

jQuery.fn.highlight = function(pat)
{
 function innerHighlight(node, pat) 
 {
    var skip = 0;
    if (node.nodeType == 3) 
    {
      var pos = node.data.toUpperCase().indexOf(pat);
      if (pos >= 0) 
      {
        var spannode = document.createElement('span');
        spannode.className = 'highlight';
        var middlebit = node.splitText(pos);
        var endbit = middlebit.splitText(pat.length);
        var middleclone = middlebit.cloneNode(true);
        spannode.appendChild(middleclone);
        middlebit.parentNode.replaceChild(spannode, middlebit);
        skip = 1;
     }
  }
  else if (node.nodeType == 1 && node.childNodes && !/(script|style)    /i.test(node.tagName)) {
   for (var i = 0; i < node.childNodes.length; ++i) {
    i += innerHighlight(node.childNodes[i], pat);
   }
  }
  return skip;
 }
 return this.each(function() 
 {
  innerHighlight(this, pat.toUpperCase());
 });
};

If you want to get all the rows which contains your found text , you can use the following jquery

allrows = $.map("tr.rbi:contains('California')")

Surely you have to write some logic to get CA inplace of California by yourself.

Upvotes: 0

Jesvin Jose
Jesvin Jose

Reputation: 23098

You need the highlighter: http://wiki.apache.org/solr/HighlightingParameters

Here are a few relevant extracts from that document

hl

Set to "true" enable highlighted snippets...

hl.fl

A comma- or space- delimited list of fields for which to generate highlighted snippets

hl.simple.pre/hl.simple.post

The text which appears before and after a highlighted term...

The default values are "< em>" and "< /em>"


You may need Solr's synonyms or client logic to identify CA and California. You also need a .Net binding for Solr; SolrNet is discussed here often.

Upvotes: 2

Related Questions