Skip to main content

sqlite-fts4

PyPI Changelog Tests License

Custom SQLite functions written in Python for ranking documents indexed using the FTS4 extension.

Read Exploring search relevance algorithms with SQLite for further details on this project.

Demo

You can try out these SQL functions using this interactive demo.

Installation

pip install sqlite-fts4

Usage

This module implements several custom SQLite3 functions. You can register them against an existing SQLite connection like so:

import sqlite3
from sqlite_fts4 import register_functions

conn = sqlite3.connect(":memory:")
register_functions(conn)

If you only want a subset of the functions registered you can do so like this:

from sqlite_fts4 import rank_score

conn = sqlite3.connect(":memory:")
conn.create_function("rank_score", 1, rank_score)

if you want to use these functions with Datasette you can enable them by installing the datasette-sqlite-fts4 plugin:

pip install datasette-sqlite-fts4

rank_score()

This is an extremely simple ranking function, based on an example in the SQLite documentation. It generates a score for each document using the sum of the score for each column. The score for each column is calculated as the number of search matches in that column divided by the number of search matches for every column in the index - a classic TF-IDF calculation.

You can use it in a query like this:

select *, rank_score(matchinfo(docs, "pcx")) as score
from docs where docs match "dog"
order by score desc

You must use the "pcx" matchinfo format string here, or you will get incorrect results.

rank_bm25()

An implementation of the Okapi BM25 scoring algorithm. Use it in a query like this:

select *, rank_bm25(matchinfo(docs, "pcnalx")) as score
from docs where docs match "dog"
order by score desc

You must use the "pcnalx" matchinfo format string here, or you will get incorrect results. If you see any math domain errors in your logs it may be because you did not use exactly the right format string here.

decode_matchinfo()

SQLite's built-in matchinfo() function returns results as a binary string. This binary represents a list of 32 bit unsigned integers, but reading the binary results is not particularly human-friendly.

The decode_matchinfo() function decodes the binary string and converts it into a JSON list of integers.

Usage:

select *, decode_matchinfo(matchinfo(docs, "pcx"))
from docs where docs match "dog"

Example output:

hello dog, [1, 1, 1, 1, 1]

annotate_matchinfo()

This function decodes the matchinfo document into a verbose JSON structure that describes exactly what each of the returned integers actually means.

Full documentation for the different format string options can be found here: https://www.sqlite.org/fts3.html#matchinfo

You need to call this function with the same format string as was passed to matchinfo() - for example:

select annotate_matchinfo(matchinfo(docs, "pcxnal"), "pcxnal")
from docs where docs match "dog"

The returned JSON will include a key for each letter in the format string. For example:

{
    "p": {
        "value": 1,
        "title": "Number of matchable phrases in the query"
    },
    "c": {
        "value": 1,
        "title": "Number of user defined columns in the FTS table"
    },
    "x": {
        "value": [
            {
                "column_index": 0,
                "phrase_index": 0,
                "hits_this_column_this_row": 1,
                "hits_this_column_all_rows": 2,
                "docs_with_hits": 2
            }
        ],
        "title": "Details for each phrase/column combination"
    },
    "n": {
        "value": 3,
        "title": "Number of rows in the FTS4 table"
    },
    "a": {
        "title":"Average number of tokens in the text values stored in each column",
        "value": [
            {
                "column_index": 0,
                "average_num_tokens": 2
            }
        ]
    },
    "l": {
        "title": "Length of value stored in current row of the FTS4 table in tokens for each column",
        "value": [
            {
                "column_index": 0,
                "length_of_value": 2
            }
        ]
    }
}

Metadata

Release files for sqlite-fts4 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sqlite-fts4 1.0.3
File Size Uploaded
sqlite-fts4-1.0.3.tar.gz 9.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sqlite-fts4 1.0.3
File Interpreter ABI Platform
sqlite_fts4-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 19.7 kB

Release files / sqlite-fts4-1.0.3.tar.gz

Download URL sqlite-fts4-1.0.3.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
78b05eeaf6680e9dbed8986bde011e9c086a06cb0c931b3cf7da94c214e8930c
BLAKE2b-256 checksum
How to use checksums
c26d9dad6c3b433ab8912ace969c66abd595f8e0a2ccccdb73602b1291dbda29
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release files / sqlite_fts4-1.0.3-py3-none-any.whl

Download URL sqlite_fts4-1.0.3-py3-none-any.whl
Size 10.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0359edd8dea6fd73c848989e1e2b1f31a50fe5f9d7272299ff0e8dbaa62d035f
BLAKE2b-256 checksum
How to use checksums
51290096e8b1811aaa78cfb296996f621f41120c21c2f5cd448ae1d54979d9fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release history Release notifications | RSS feed

This release

1.0.3 This release

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0

2 release files

0.5.2

1 release file

0.5.1

1 release file

0.5.0

1 release file

0.4.3

1 release file

0.4.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page