Solr Highlighting - Enterprise Solr vs ColdFusion's Built-in Solr

First, a big THANK YOU to everyone who attended my session at CF Objective. It was very rewarding to be able to spread the information rattling around in my head and answer your questions about Solr. One of the questions I received has inspired a blog post.

This particular attendee needed to be able to display highlighting from multiple areas of a document in the case that the matching search term appears more than once in that document. Using the Solr instance built into ColdFusion, she was unable to achieve this. I'll outline the different highlighting options available in both ColdFusion's and the full version of Apache Lucene Solr in this post.

First, I am in no way bashing Adobe's roll out of Solr. It will work fine for certain types of applications that only require basic search and indexing features. If you need a lot of details out of your search, however, the full version available from Apache is the way to go.

So let's see what ColdFusion has to offer us when we want highlighting results with our search.

I created a document containing 5 paragraphs of "Lorem Ipsum" and placed the word "dog" at four random locations within the document. I then used CFIndex to add the document to my ColdFusion collection and performed a search using CFSearch. In the "context" column of the returned query object I get:

ColdFusion provides us with a short snippet of text with the search term surrounded by HTML emphasis tags. Although the search term appears four times inside the document text, we only get the first match here. The HTML tags surrounding the matched term can be customized using the contextHighlightBegin and contextHighlightEnd attributes of the CFSearch tag in the case you wanted to change the background color or have your match noted a different way.

Okay, so if we want ALL of the highlighting data, that doesn't help us. Let's see what we get with the full version of Solr 4.8.0 with CFSolrLib to integrate it into our CF application.

I indexed the same text file, using Apache Tika to extract the text from the document and the "add" method of CFSolrLib to index my data. I placed the text in the "title" field since I already have that field set up for highlighting. There are two types of highlighters available in Apache Solr. There is a "simple" highlighting component, which is much like what you see in ColdFusion's instance, and a "Fast Vector Highligher", which is a much faster and far superior highlighting component. For this example, I will be using the Fast Vector Highlighter.

First thing to note, when using the Fast Vector Highlighter, the field you intend to pull your highlighting data from has to have Term Offsets, Term Positions and Term Vectors set to true. In the Solr instance distributed with CFSolrLib you'll find this line in your schema:

<field name="title" type="text_general" indexed="true" stored="true" termOffsets="true" termPositions="true" termVectors="true" />

This field has the proper attributes set to use the Fast Vector Highlighter.

Now let's look at our search example (searchExample.cfm distributed with CFSolrLib). Taking a look at the top of the example, there are highlighting attributes being passed to Solr.

<cfset local.params = structNew()>
<cfset local.params["hl"] = "on">
<cfset local.params["hl.fl"] = "title">
<cfset local.params["hl.fragListBuilder"] = "simple">
<cfset local.params["hl.fragsize"] = 20>
<cfset local.params["hl.snippets"] = 10>
<cfset local.params["hl.useFastVectorHighlighter"] = true>
<cfset local.params["hl.fragmentsBuilder"] = "colored">
<cfset local.params["hl.boundaryScanner"] = "default">
<cfset local.params["hl.usePhraseHighlighter"] = true>
<cfset searchResponse =,0,100,"title",local.params) />

hl.fl contains the list of fields we want highlighting results from and we're telling Solr to use the simple frag list builder. Fragsize tells Solr how long to make the snippets of text returned in the results and hl.snippets sets a cap on the number of snippets that will be returned. We're telling Solr we want to use the Fast Vector Highlighter and that we want it to automatically change the background color behind the matching text. This will set a new color for each matching term, if you're searching for more than one word. We've also enabled Phrase Highlighting in the case that we search for a phrase, which must be surrounded by quotes. Finally we're passing the field name "title" along with our parameters to the search function since we're expecting highlighting results from that field. Currently CFSolrLib is configured to return highlighting results from only one field, but it could be easily modified to accept a list of fields.

If I run searchExample.cfm in a browser and search for "dog" with highlighting enabled, I'll get my document back with just the first instance of "dog" highlighted. If we take a look at the code we'll see:

<cfif structKeyExists(currentResult,"highlightingResult")>#currentResult.highlightingResult[1]#</cfif>

If we get highlighting results back, it displays only the first value in an array. This was done for simplicity in the example. If you wanted to display all of the results, the array could easily be looped to show all of the results, or a specific row could be displayed. If we dump out our result we see:

You'll notice that instead of a query object, we get back a ColdFusion structure containing an array of results. In our highlighting node of the struct we get back an array containing each of the highlighting snippets Solr generated. This array can easily be accessed to display the highlighting result from any section of the document. The fragsize attribute can be adjusted to make these snippets of text longer or shorter. I recommend playing with the example some to see how you can change the way your highlighting results are returned.

As always I'm happy to answer any questions you have about this post and keep in mind, just because you're not getting what you want from ColdFusion's Solr, don't give up on it. Solr is fast and feature rich. Chances are, it will do more than you think.

CFSolrLib can be downloaded at The version available for download there is distributed with Solr 4.0.0, but I'm working on updating my fork of the repository,, with Solr 4.8.0. I'll update this post when it's done.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Copyright © 2008 - Jim Leether BlogCFC was created by Raymond Camden. This blog is running version Contact Jim