Highlighting search results

Enrico Zini enrico at enricozini.org
Fri Sep 9 10:05:37 BST 2022


On Thu, Sep 08, 2022 at 08:59:15PM +0100, Olly Betts wrote:

> 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.

Thanks, for me it worked perfectly, especially together with
SNIPPET_EMPTY_WITHOUT_MATCH

The function I posted in my previous message became as simple as this,
and works much better:


    def get_highlights(self, result: ResultEntry) -> Generator[str, None, None]:
        """
        Return text with highlighted search terms
        """
        snippet_flags = (
                xapian.MSet.SNIPPET_BACKGROUND_MODEL |
                xapian.MSet.SNIPPET_EXHAUSTIVE |
                xapian.MSet.SNIPPET_EMPTY_WITHOUT_MATCH)
                
        for text in result.entry.iter_text_paragraphs():
            if not text:
                continue
            highlighted = result.mset.snippet(text, 500, self.stemmer, snippet_flags)
            if highlighted:
                yield highlighted.decode()


Enrico

-- 
GPG key: 4096R/634F4BD1E7AD5568 2009-05-08 Enrico Zini <enrico at enricozini.org>



More information about the Xapian-discuss mailing list