【问题标题】:Populate virtual SQLite FTS5 (full text search) table from content table从内容表填充虚拟 SQLite FTS5(全文搜索)表
【发布时间】:2022-01-25 20:05:10
【问题描述】:

我已关注https://kimsereylam.com/sqlite/2020/03/06/full-text-search-with-sqlite.html 设置SQLite's virtual table extension FTS5 以在external content table 上进行全文搜索。 虽然博客展示了如何设置触发器以保持虚拟 FTS 表随数据更新:

CREATE TABLE user (
    id INTEGER PRIMARY KEY,
    username TEXT NOT NULL UNIQUE,
    email TEXT NOT NULL UNIQUE,
    short_description TEXT
)

CREATE VIRTUAL TABLE user_fts USING fts5(
    username, 
    short_description, 
    email UNINDEXED, 
    content='user', 
    content_rowid='id' 
)

CREATE TRIGGER user_ai AFTER INSERT ON user
    BEGIN
        INSERT INTO user_fts (rowid, username, short_description)
        VALUES (new.id, new.username, new.short_description);
    END;
...

我无法以类似的方式从所有以前的数据中填充 FTS 表。 我将继续使用博客中的示例:

INSERT INTO user_fts (rowid, username, short_description) SELECT (id, username, short_description) FROM user;

但是,sqlite (3.37.2) 失败并显示row value misused

请说明idcontent_rowidrowidnew.id 之间的关系以及如何修改查询以正确更新 FTS 表。

【问题讨论】:

标签: sql sqlite full-text-search fts5


【解决方案1】:

INSERT INTO user_fts (rowid, username, short_description) SELECT id, username, short_description FROM user;(无括号)有效。

rowid 是唯一的 64 位无符号整数行 ID。 如果表包含一个整数主键(如user 中的id),它们是相同的(别名)。 IE。 user.rowid == user.id = user_fts.rowid.
文档:https://www.sqlite.org/lang_createtable.html#rowid

new 指的是被插入的元素。
文档:https://www.sqlite.org/lang_createtrigger.html

content_rowid 将虚拟 FTS 表链接到外部数据表 row id 列(默认为 rowid)。
文档:https://www.sqlite.org/fts5.html#external_content_tables

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-14
    • 1970-01-01
    • 2020-06-01
    • 2021-09-26
    • 2019-08-29
    • 2015-06-26
    • 2016-10-06
    • 2018-10-14
    相关资源
    最近更新 更多