Ben Chuanlong Du's Blog

It is never too late to learn.

Hands on Full-text Search in SQLite3

FTS5 (Full-text Search) in SQLite3

  1. If tokens in the search phrase are separated by either spaces or plus signs, then they are matched in order by tokens in the text. To match tokens orderless, you can separate tokens in the search phrase with the keyword AND.
In [1]:
%defaultDatasource jdbc:sqlite:fts.sqlite3
In [2]:
CREATE VIRTUAL TABLE posts 
USING fts5(title, category, tags, content)
;
In [3]:
INSERT INTO posts (
    title,
    category,
    tags,
    content
) VALUES(
    'Use JupyterLab Notebooks in Pelican',
    'Life',
    'life, blog',
    'Introduction on how to use JupyterLab notebooks in Pelican to make blogging easier.'
), (
    'ODBC vs JDBC in Python',
    'Programming',
    'programming, Python, JDBC, ODBC',
    'Overall speaking, Python has better ODBC support than jDBC support. Most database related packages in Python support or rely on ODBC.'
), (
    'Tips for JavaScript',
    'Programming',
    'programming, JavaScript, JS, forntend',
    'Node.js is a javaScript runtime environment outside browsers.'
)
;
In [4]:
select * from posts

Match

In [5]:
SELECT * FROM posts WHERE posts MATCH 'Python'
In [6]:
SELECT * FROM posts WHERE posts MATCH 'programming'

The Equal Sign

The equal sign (=) is equivalent to the match keyword.

In [7]:
SELECT * FROM posts WHERE posts = 'programming'

The Table-valued function Syntax

In [8]:
SELECT * FROM posts('programming')

Order by the Default Rank

In [9]:
SELECT * FROM posts WHERE posts MATCH 'programming' ORDER BY rank

Order by BM25

In [10]:
SELECT * FROM posts WHERE posts MATCH 'programming' ORDER BY bm25(posts)
In [11]:
SELECT highlight(email, 2, '<b>', '</b>') FROM email WHERE email MATCH 'fts5'
org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (no such table: email)

Highlight

1st param: the name of the virtual table.
2nd param: 0-based column index specifying the column from which to search the keywords.
3rd param: left quoting string.
4th param: right quoting string.

In [12]:
SELECT highlight(posts, 3, '<b>', '</b>') from posts WHERE posts MATCH 'programming' ORDER BY rank

Comments