是否需要手动填充SQLite FTS表?

时间:2018-12-11 22:56:17

标签: sqlite full-text-search fts4

有关SQLite FTS implies的文档,其中应使用INSERT,UPDATE,DELETE等填充和更新FTS表。

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

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

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

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

对此有任何想法吗? FTS实际上是否需要填充?

2 个答案:

答案 0 :(得分:0)

进一步阅读后,我发现确实需要手动将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;

答案 1 :(得分:0)

根据sqlite document

要删除条目,要么

-- 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');