Sling Academy
Home/SQLite/Understanding FTS3 and FTS5 in SQLite

Understanding FTS3 and FTS5 in SQLite

Last updated: December 07, 2024

SQLite is a software library that provides a relational database management system. It is known for its simplicity, efficiency, self-contained features, and serverless design. Within the SQLite library, Full-Text Search (FTS) is a significant module for performing full-text searches in databases. Specifically, FTS3 and FTS5 are powerful extensions used for full-text indexing and searching in SQLite. These extensions allow SQLite to create virtual tables to store substantial documents in a way that allows for efficient search operations.

Understanding FTS3

FTS3 is an earlier version of the SQLite full-text search extension. This module creates special virtual tables with rich text data which can be effortlessly indexed and searched. The use of FTS3 provides several advantages:

  • Fast searching capabilities through text data.
  • Handling of large volumes of text conveniently.
  • Ability to integrate with usual SQL queries seamlessly.

To get started with FTS3 in SQLite, you need to enable the extension and create an FTS virtual table:


-- Enable the FTS3 module
CREATE VIRTUAL TABLE document USING fts3(title TEXT, content TEXT);

-- Insert example data
INSERT INTO document (title, content) VALUES ('First Article', 'This is the content of the first article');
INSERT INTO document (title, content) VALUES ('Second Article', 'This is the content of the second article');

Once the data is inserted, you can utilize full-text search using SQL queries:


-- Perform a full-text search
SELECT * FROM document WHERE content MATCH 'first';

Given the declarative nature of SQL, integrating FTS3 with existing queries enables robust search functionalities.

Introducing FTS5

FTS5 is an improvement over FTS3, providing better performance and more flexible querying capabilities. In essence, FTS5 maintains the core advantages of FTS3 but with enhancements such as tokenization features and prefix indexing, allowing for faster and more accurate search results.

Using FTS5 involves similar steps starting with enabling the FTS5 module and creating a virtual table:


-- Enable the FTS5 module
CREATE VIRTUAL TABLE article USING fts5(title, body);

-- Insert example data
INSERT INTO article (title, body) VALUES ('Learning SQLite', 'SQLite is a popular database system. This document explains its use.');
INSERT INTO article (title, body) VALUES ('Mastering SQLite', 'Delve deeper into advanced techniques of SQLite.');

Running a search query in FTS5 might look like:


-- Execute a full-text search
SELECT * FROM article WHERE article MATCH 'SQLite';

FTS5 features advanced syntaxes allowing configurations on how terms like "AND", "OR", and phrase searching are processed, giving users significant flexibility.

Tokenizing and Query Improvements

Both FTS3 and FTS5 allow the use of custom tokenizers. Tokenizers break text into discrete searchable elements or tokens. FTS5 has an edge by letting users develop custom tokenizers tailored to specific requirements through user-defined functions, and advanced built-in tokenizers that can handle language specifics, prefix matching, and more sophisticated query handling.


// Example of defining a custom tokenizer in C
#include "fts5_tokenizer.h"

static int customTokenizer(void *pCtx, int flags, const char *pText, int nText, Fts5TokenCallback xToken, void *pTokenCtx) {
    // Custom tokenizing process 
    return SQLITE_OK;
}

int sqlite3_custom_tokenizer_init(sqlite3 *db){
    sqlite3_fts5_may_exist = -99; // Placeholder for missing implementation
    return sqlite3_fts5_may_exist;
}

Conclusion

FTS3 and FTS5 are essential tools for anyone leveraging SQLite who needs efficient and powerful full-text search capabilities. Understanding the subtle differences and access to features between the two allows developers to implement more finely-tuned search solutions. By using these extensions, robust text searching with an SQL database becomes not only possible but remarkably efficient.

Next Article: Top Use Cases for Full-Text Search in SQLite Applications

Previous Article: What is Full-Text Search in SQLite and How Does It Work?

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