Highlighting search results

Olly Betts olly at survex.com
Thu Sep 8 20:59:15 BST 2022


On Thu, Sep 08, 2022 at 05:15:39PM +0200, Enrico Zini wrote:
> I built an interface where I search for text with Xapian, and I'd like
> to implement highlighting of matching terms on query results.
> 
> I could not find something to help with that in Xapian itself, so I
> tried implementing my own one. It works reasonably well, except in case
> of phrase searches, where it will highlight individual words even if
> they are not consecutive (due to turning the query into a set of words).
> 
> Can one do better, without ending up knee-deep in yak fur?

Xapian::MSet::snippet() can generate a highlighted snippet (or highlight
the full text if you set the required length to a value longer than the
text length).  It knows how to correctly highlight phrases (and also
handles stemming and wildcards):

https://xapian.org/docs/apidoc/html/classXapian_1_1MSet.html#ab3af7b20654dcc6e3335cc21be74efda

The highlighting itself is a bit specific to HTML (or XML) currently
which might be a limitation for some uses.

Cheers,
    Olly



More information about the Xapian-discuss mailing list