6

SQLite FTS 的文档暗示应该使用 INSERT、UPDATE、DELETE 等填充和更新 FTS 表。

这就是我正在做的 - 添加行,删除它们等,但最近我注意到,一旦我创建 FTS 表,它就会使用源中的数据自动填充。我这样创建它:

CREATE VIRTUAL TABLE notes_fts USING fts4(content="notes", notindexed="id", id, title, body)

如果我在“notes”表中添加一行,它也会自动添加到 notes_fts。我想这就是虚拟表。

但是,为什么会有关于填充 FTS 表的章节呢?甚至有什么意义,例如,如果我删除一行,如果它仍在源表中,它将回来。

对此有任何想法吗?FTS 真的需要填充吗?

4

2 回答 2

4

经过进一步阅读,我发现 FTS 表确实需要手动与内容表保持同步。运行CREATE VIRTUAL TABLE调用时,FTS 表会自动填充,但之后必须手动完成删除、插入和更新。

在我的情况下,我使用以下触发器完成了它:

CREATE VIRTUAL TABLE notes_fts USING fts4(content="notes", notindexed="id", id, title, body

CREATE TRIGGER notes_fts_before_update BEFORE UPDATE ON notes BEGIN
    DELETE FROM notes_fts WHERE docid=old.rowid;
END

CREATE TRIGGER notes_fts_before_delete BEFORE DELETE ON notes BEGIN
    DELETE FROM notes_fts WHERE docid=old.rowid;
END

CREATE TRIGGER notes_after_update AFTER UPDATE ON notes BEGIN
    INSERT INTO notes_fts(docid, id, title, body) SELECT rowid, id, title, body FROM notes WHERE is_conflict = 0 AND encryption_applied = 0 AND new.rowid = notes.rowid;
END

CREATE TRIGGER notes_after_insert AFTER INSERT ON notes BEGIN
    INSERT INTO notes_fts(docid, id, title, body) SELECT rowid, id, title, body FROM notes WHERE is_conflict = 0 AND encryption_applied = 0 AND new.rowid = notes.rowid;
END;
于 2018-12-12T21:27:11.213 回答
1

根据sqlite文档

要删除条目,要么

-- Insert a row with rowid=14 into the fts5 table.
INSERT INTO ft(rowid, a, b, c) VALUES(14, $a, $b, $c);

-- Remove the same row from the fts5 table.
INSERT INTO ft(ft, rowid, a, b, c) VALUES('delete', 14, $a, $b, $c);

或者

CREATE TRIGGER tbl_ad AFTER DELETE ON tbl BEGIN
  INSERT INTO fts_idx(fts_idx, rowid, b, c) VALUES('delete', old.a, old.b, old.c);
END;

基于修改后的虚拟表进行重建

INSERT INTO ft(ft) VALUES('rebuild');
于 2019-05-06T07:34:42.723 回答