mdoTomofumi Chiba
3/14/2024, 9:27:51 PM

SQLite の全文検索と Cloudflare D1 を試す

Cloudflare D1 で日本語全文検索が動いた。
トリガー作る必要があるのが面倒。。

試したコード
CREATE TABLE tbl(a INTEGER PRIMARY KEY, b, c);
CREATE VIRTUAL TABLE v_tbl USING fts5(b, c, content='tbl', content_rowid='a', tokenize="trigram");

CREATE TRIGGER tbl_ai AFTER INSERT ON tbl BEGIN
  INSERT INTO v_tbl(rowid, b, c) VALUES (new.a, new.b, new.c);
END;
CREATE TRIGGER tbl_ad AFTER DELETE ON tbl BEGIN
  INSERT INTO v_tbl(v_tbl, rowid, b, c) VALUES('delete', old.a, old.b, old.c);
END;
CREATE TRIGGER tbl_au AFTER UPDATE ON tbl BEGIN
  INSERT INTO v_tbl(v_tbl, rowid, b, c) VALUES('delete', old.a, old.b, old.c);
  INSERT INTO v_tbl(rowid, b, c) VALUES (new.a, new.b, new.c);
END;                                                                                                                                                                                                                                            insert into tbl values (1, 'abcde', 'あいうえお');
insert into tbl (aaa) values ('hoge');                                                                                                                                                                                                          select * from v_tbl where b match 'bcd';                                                                                select * from v_tbl where c match 'いうえ';                                                                             
参考


TweetBlueskyLike