Sling Academy
Home/SQLite/Highlighting Search Terms in SQLite Full-Text Queries

Highlighting Search Terms in SQLite Full-Text Queries

Last updated: December 07, 2024

SQLite is a popular database engine that is both powerful and lightweight, making it suitable for applications ranging from simple web applications to complex engineering or scientific systems. One of the useful features provided by SQLite is Full-Text Search (FTS), which allows for efficient searching within large volumes of text data. However, a common requirement when implementing search functionality is the highlighting of the search terms within the results returned from a query. In this article, we will delve into how you can highlight search terms in SQLite full-text queries.

SQLite provides full-text search capabilities via an extension that allows you to create virtual tables with full-text indexes. These virtual tables can then be queried using natural language processing, supporting rapid matches for search terms.

CREATE VIRTUAL TABLE documents USING fts5(content);

The fts5 module is currently the most advanced full-text search extension available within SQLite. It provides tools for creating and indexing full-text search to improve retrieval speed.

Highlighting Search Terms in Queries

To highlight search terms in SQLite using FTS5, we can utilize the built-in functionalities provided by the extension. Specifically, the highlight() function enables various text manipulation tasks. It tags terms found within the stored content with custom markers.

SELECT highlight(documents, 0, '<b>', '</b>') AS result
FROM documents
WHERE documents MATCH 'searchTerm';

In this example:

  • documents is the already created FTS5 virtual table.
  • The second argument 0 specifies which column of the table to search.
  • '<b>' and '</b>' represent opening and closing HTML tags that are used to highlight terms.
  • 'searchTerm' is the term we are looking to find and highlight in the text.

Customizing Highlighting

You might want to use different styles for the highlights depending on the platform where the content will be displayed. Fortunately, the highlight() function is customizable, and you can use different tags or styles to meet specific design requirements.

SELECT highlight(documents, 0, '<mark>', '</mark>') AS highlighted_text
FROM documents
WHERE documents MATCH 'education';

Here, instead of using bold tags, we're wrapping search terms with <mark> tags, which offer a different presentation.

Practical Example

Consider we have a table articles where we store textual content:

CREATE VIRTUAL TABLE articles USING fts5(title, body);

We want to search for all entries where 'internet' appears and highlight those occurrences in the body of the text.

SELECT title, highlight(articles, 1, '<span style="background-color: yellow">', '</span>') AS highlighted_body
FROM articles
WHERE articles MATCH 'internet';

By wrapping terms in a "span" with custom styles, we can highlight text in ways that render clearly for users. This method is flexible and highly adaptive to aesthetic needs.

Conclusion

Incorporating term highlighting in full-text search results not only enhances user experience by making relevant data easily discoverable but also showcases the powerful capabilities of SQLite's FTS features. With SQLite's highlight() function, developers can swiftly implement highlighting without requiring significant restructuring.

Whether you're building a simple search portal or a sophisticated information retrieval system, leveraging the power of SQLite FTS, integrated with effective UI practices such as highlighted text, can significantly improve the usability and functionality of your application.

Next Article: Advanced Full-Text Search Features in SQLite You Should Know

Previous Article: Ranking Full-Text Search Results in SQLite Explained

Series: Full-Text Search with SQLite

SQLite

You May Also Like

  • How to use regular expressions (regex) in SQLite
  • SQLite UPSERT tutorial (insert if not exist, update if exist)
  • What is the max size allowed for an SQLite database?
  • SQLite Error: Invalid Value for PRAGMA Configuration
  • SQLite Error: Failed to Load Extension Module
  • SQLite Error: Data Type Mismatch in INSERT Statement
  • SQLite Warning: Query Execution Took Longer Than Expected
  • SQLite Error: Cannot Execute VACUUM on Corrupted Database
  • SQLite Error: Missing Required Index for Query Execution
  • SQLite Error: FTS5 Extension Malfunction Detected
  • SQLite Error: R-Tree Node Size Exceeds Limit
  • SQLite Error: Session Extension: Invalid Changeset Detected
  • SQLite Error: Invalid Use of EXPLAIN Statement
  • SQLite Warning: Database Connection Not Closed Properly
  • SQLite Error: Cannot Attach a Database in Encrypted Mode
  • SQLite Error: Insufficient Privileges for Operation
  • SQLite Error: Cannot Bind Value to Parameter
  • SQLite Error: Maximum String or Blob Size Exceeded
  • SQLite Error: Circular Reference in Foreign Key Constraints