Whoosh library lets Python developers add search result highlighting without Elasticsearch
The pure-Python full-text search library Whoosh includes a built-in highlighting engine that displays relevant snippets around matched query terms, similar to how Google and GitHub present search results. Developers can install the maintained fork, Whoosh3, and use its highlighter by passing terms=True during a search query to accurately track which terms matched each document. The library offers multiple fragmenter options — including ContextFragmenter and SentenceFragmenter — to control snippet length and boundaries, as well as formatters for HTML output or plain-text CLI displays. For web interfaces, HtmlFormatter wraps matched terms in configurable HTML tags, while UppercaseFormatter or custom ANSI-coded formatters serve terminal-based applications. The approach requires no external search infrastructure, making it a lightweight option for adding contextual search highlighting to Python projects.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in