Search code examples
pythonsolrhighlightsunburnt

Solr, sunburnt (python) and highlighting: how-to?


What's the best way to implement sunburnt's highlight response into an application (django based, in this case)?

This link shows how's the response structured.

As they say

The results are shown as a dictionary of dictionaries

which is fair understandable enough. What i don't understand is this:

The text is highlighted with HTML, and the fragments should be suitable for dropping straight into a search template

How can i "drop the fragments in the template"? In the example they do highlight the word "Game". How can I use those highlighted fragments? Do i have to do a "search-and-replace regex" on my text? Is there another (hopefully smarter) way to deal with this?

I'm really stuck this time, and cannot come up with any solution. Thanks all in advance.


Solution

  • The text to be highlighted is already surrounded by <em> tags. So, if you render the result text as is (in the response HTML that's sent across), highlighted words would appear in bold (because of the <em> tag surrounding them).

    If you'd like it to be highlighted differently, you can do that with CSS. Something like:

    em {
        background-color: yellow;
    }
    

    in your css, would highlight the matches in yellow, for example.