跳转到主要内容

用于与SQLite FTS4搜索一起工作的Python函数

项目描述

sqlite-fts4

PyPI Changelog Tests License

使用Python编写的自定义SQLite函数,用于对使用FTS4扩展索引的文档进行排名。

阅读 使用SQLite探索搜索相关性算法 以获取有关此项目的更多信息。

演示

您可以使用这个交互式演示 使用此交互式演示 尝试这些SQL函数。

安装

pip install sqlite-fts4

使用方法

此模块实现了几个自定义SQLite3函数。您可以像这样注册它们与现有的SQLite连接

import sqlite3
from sqlite_fts4 import register_functions

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

如果您只想注册函数的子集,可以这样做

from sqlite_fts4 import rank_score

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

如果您想使用这些函数与 Datasette 一起使用,可以通过安装 datasette-sqlite-fts4 插件来启用它们

pip install datasette-sqlite-fts4

rank_score()

这是一个非常简单的排名函数,基于SQLite文档中的一个示例。它使用每个列的分数总和为每个文档生成一个分数。每列的分数是搜索匹配数除以索引中每列的搜索匹配数,这是一个经典的 TF-IDF 计算。

您可以在查询中使用它,如下所示

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

必须 使用 "pcx" matchinfo格式字符串,否则您将得到错误的结果。

rank_bm25()

Okapi BM25评分算法的实现。使用方法如下:

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

在这里你必须使用"pcnalx"matchinfo格式字符串,否则将得到错误的结果。如果你的日志中出现了任何math domain错误,可能是因为你没有使用正确的格式字符串。

decode_matchinfo()

SQLite的内置matchinfo()函数返回结果为二进制字符串。这个二进制表示一个32位无符号整数的列表,但读取二进制结果并不特别适合人类。

decode_matchinfo()函数将二进制字符串解码并转换为整数的JSON列表。

使用方法

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

示例输出

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

annotate_matchinfo()

此函数将matchinfo文档解码为详尽的JSON结构,该结构精确描述了每个返回的整数实际上代表什么。

有关不同格式字符串选项的完整文档可以在此处找到:https://www.sqlite.org/fts3.html#matchinfo

你需要使用与传递给matchinfo()相同的格式字符串调用此函数 - 例如

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

返回的JSON将包括格式字符串中每个字母的键。例如

{
    "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
            }
        ]
    }
}

项目详情


下载文件

下载适合您平台文件的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

sqlite-fts4-1.0.3.tar.gz (9.7 kB 查看散列)

上传时间

构建分布

sqlite_fts4-1.0.3-py3-none-any.whl (10.0 kB 查看散列)

上传时间 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面