To show users why a search result matched, do three separate jobs: retrieve and rank documents, select a readable excerpt from the matching text, then mark the matched words in that excerpt. For a pure-Python search stack, Whoosh provides a built-in highlighting pipeline; a custom highlighter can work for simple match rules, but it must keep match offsets accurate and escape document text before adding markup.
How do I show snippets for search results?
A result snippet is not simply a search term wrapped in bold. It is a short, useful fragment chosen from the document, with the matching portions visibly marked. This helps users understand relevance without opening the full result.
As an Amazon Associate I earn from qualifying purchases.
Keep the work in three stages:
- Retrieve and rank: the search engine decides which documents match and in what order.
- Select a fragment: choose a readable passage near useful matching text, rather than always showing the beginning of a long document.
- Format matches: mark the terms or spans that explain the match in the selected passage.
Whoosh’s highlighting system separates fragmenters, scorers, order functions, and formatters. Fragmenters break text into candidate pieces, scorers help assess those pieces, order functions determine their sequence, and formatters render marked text. That division makes it possible to tune excerpt selection independently from the interface markup. See the Whoosh highlighting documentation.
Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Clear out junk files and repair common Windows errors3Scan for outdated or missing drivers - takes under a minuteHow do I highlight search terms in Python with Whoosh?
Whoosh is the most direct pure-Python option in the sources for a search application that also needs result excerpts. Its hit highlighting API can produce a formatted excerpt for a field, provided the original field text is available: store the text in the index or supply the original text to the highlight method. If the text is absent at this stage, the application cannot mark its contents.
#1 Best Overall
- CRISP CLARITY: This 23.8″ Philips V line monitor delivers crisp Full HD 1920x1080 visuals. Enjoy movies, shows and videos with remarkable detail
- INCREDIBLE CONTRAST: The VA panel produces brighter whites and deeper blacks. You get true-to-life images and more gradients with 16.7 million colors
- THE PERFECT VIEW: The 178/178 degree extra wide viewing angle prevents the shifting of colors when viewed from an offset angle, so you always get consistent colors
- WORK SEAMLESSLY: This sleek monitor is virtually bezel-free on three sides, so the screen looks even bigger for the viewer. This minimalistic design also allows for seamless multi-monitor setups that enhance your workflow and boost productivity
- A BETTER READING EXPERIENCE: For busy office workers, EasyRead mode provides a more paper-like experience for when viewing lengthy documents
- Run the search and retain the hits. If you need the matched terms for other display logic, use Whoosh’s term-tracking option where appropriate.
- For each hit, request a highlighted excerpt from the field that matched. Confirm that the field is stored or pass its original text.
- Set fragment length and surrounding context to suit your results-page layout. A useful excerpt should provide enough surrounding words to make the match understandable.
- Choose a formatter that emits markup supported by your interface, then render the result as HTML only when its text and markup are handled safely.
The tutorial Highlighted search-result snippets with Whoosh demonstrates term tracking, context controls, and a <mark> formatter. Its example uses whoosh3 3.18 with Python 3.11; treat that as the tutorial’s tested context, not a guarantee about compatibility with every current environment. Check the package and documentation against your own Python version and deployment.
Can I build a small custom highlighter?
Yes, when the application’s matching rules are simple and explicit. Python regular expressions can locate text patterns, but they do not reproduce a search engine’s indexing or query analyzer automatically. Define the behavior before implementing it, especially for case sensitivity, whole words versus substrings, repeated terms, overlapping matches, punctuation, and Unicode text. The Python regular expression HOWTO explains scanning and word-boundary matching; test those semantics against the behavior your search actually uses.
Rank #2
- CRISP CLARITY: This 22 inch class (21.5″ viewable) Philips V line monitor delivers crisp Full HD 1920x1080 visuals. Enjoy movies, shows and videos with remarkable detail
- 100HZ FAST REFRESH RATE: 100Hz brings your favorite movies and video games to life. Stream, binge, and play effortlessly
- SMOOTH ACTION WITH ADAPTIVE-SYNC: Adaptive-Sync technology ensures fluid action sequences and rapid response time. Every frame will be rendered smoothly with crystal clarity and without stutter
- INCREDIBLE CONTRAST: The VA panel produces brighter whites and deeper blacks. You get true-to-life images and more gradients with 16.7 million colors
- THE PERFECT VIEW: The 178/178 degree extra wide viewing angle prevents the shifting of colors when viewed from an offset angle, so you always get consistent colors
A safe implementation retains each match as a pair of offsets into the original text. Build the excerpt from untouched text segments and escaped matched segments, adding only trusted markup around the latter. Do not concatenate raw document content into HTML: text from documents may contain characters that alter the page or introduce unsafe markup.
Free tools Windows power users keep installed
One-click scans. No signup required.
For example, if the source text is Python & search, escape the source text for HTML before inserting it into a page, including the ampersand. Apply the same escaping to the text inside highlighted spans; only the wrapper tags should remain markup. Use a formatter or rendering method that preserves this boundary rather than trusting a string simply because it contains your own highlight tags.
Rank #3
- Clear visuals. Fluid motion: A 144Hz refresh rate and 1ms MPRT deliver smooth, tear‑free motion across work, gaming, and streaming for clearer, more fluid viewing.
- Eye comfort: TÜV Rheinland 3‑star* certification reduces harmful blue light while preserving stunning color quality without compromise. *TÜV Rheinland 3-star eye comfort certification.
- Wide viewing angle: Get consistent views across a wide 178° /178° viewing angle.
- In-Plane Switching (IPS): See excellent color accuracy and consistency across wide viewing angles with In-plane Switching (IPS) technology.
- Ultra-thin bezels: Maximize your viewing experience with thin bezels.
Keep highlights aligned with matching behavior
If search is case-insensitive, the visible marker should normally treat case the same way. But literal highlighting can still fail to explain a match when the index uses stemming, tokenization, or synonyms. A query can match a document through a normalized term or an alternate token even when the original query text does not appear as a literal substring. In those cases, use analyzer-aware match information where available, or make the limitation clear rather than marking an unrelated literal occurrence.
Which approach fits the application?
| Approach | Best fit | Important consideration |
|---|---|---|
| Whoosh | A pure-Python search application that needs fragment selection and formatted excerpts. | Needs access to the source text; its fragmenter, scorer, ordering, and formatter can be configured. |
| Custom regular-expression highlighter | Small applications with explicitly defined text-matching rules. | The application must define matching semantics, preserve offsets, escape output, and maintain behavior as search rules change. |
| Pocketsearch | A Python option to evaluate when snippet extraction and highlighting are desired. | Its PyPI description lists both features; check current maintenance and version details before adopting it: Pocketsearch on PyPI. |
| Elasticsearch | Applications already using Elasticsearch and its highlighting support. | Its documentation notes that highlighted text may not fully reflect complex Boolean query logic: Elasticsearch highlighting reference. |
Choose based on how closely highlighting must match the search analyzer, whether source text is available, excerpt relevance, control over markup and escaping, performance for long documents or many results, deployment constraints, and how much behavior your team is prepared to maintain.
Rank #4
- CURVED FOR ENHANCED ENGAGEMENT: An immersive viewing experience with a curved monitor that wraps more closely around your field of vision; It creates a wider view, enhancing depth perception and minimizing peripheral distraction
- SMOOTH PERFORMANCE FOR SEAMLESS CONTENT: Stay in the action when playing games, watching videos, or working on creative projects; The 100Hz refresh rate reduces lag and motion blur so you don't miss a thing in fast-paced moments¹
- MORE GAMING POWER: Gain the edge with optimizable game settings; Color and image contrast can be adjusted to see scenes more vividly and spot enemies hiding in the dark; Game Mode adjusts any game to fill the screen so you can view every detail²
- KEEP IT EASY ON THE EYES: Care for your eyes and stay comfortable, even during long sessions; Advanced eye comfort technology certified by TÜV reduces eye strain by minimizing blue light and reducing irritating screen flicker²
- INCREASED VERSATILITY: Connect to more; Plug devices straight into your monitor for increased flexibility, making your computing environment even more convenient
Why isn’t this syntax highlighting?
Search-result highlighting marks text that helps explain why a document matched a query. Syntax highlighting colors language constructs such as keywords, strings, and comments in source code. Python’s IDLE documentation and Pygments quickstart cover code syntax coloring, not selecting excerpts or marking search matches.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Quick Recap
Best Value
- 【INTEGRATED SPEAKERS】Whether you're at work or in the midst of an intense gaming session, our built-in speakers provide rich and seamless audio, all while keeping your desk clutter-free.
- 【EASY ON THE EYES】 Protect your eyes and enhance your comfort with Blue-Light Shift technology. This feature reduces harmful blue light emissions from your screen, helping to alleviate eye strain during long hours of use and promoting healthier viewing habits.
- 【WIDEN YOUR PERSPECTIVE】Our sleek minimal bezel design ensures undivided attention. The nearly bezel-free display seamlessly connects in a dual monitor arrangement, delivering an unobstructed view that lets you focus on more at once, completely distraction-free.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




