feat: Try SQLite FTS5 (#5052)

This commit is contained in:
iequidoo
2024-03-04 15:28:51 -03:00
parent 12587684ca
commit 15df57900b
2 changed files with 32 additions and 2 deletions

View File

@@ -1099,12 +1099,14 @@ impl Context {
.query_map(
"SELECT m.id AS id
FROM msgs m
INNER JOIN msgs_search ms
ON m.id=ms.rowid
LEFT JOIN contacts ct
ON m.from_id=ct.id
WHERE m.chat_id=?
AND m.hidden=0
AND ct.blocked=0
AND txt LIKE ?
AND ms.txt LIKE ?
ORDER BY m.timestamp,m.id;",
(chat_id, str_like_in_text),
|row| row.get::<_, MsgId>("id"),
@@ -1132,6 +1134,8 @@ impl Context {
.query_map(
"SELECT m.id AS id
FROM msgs m
INNER JOIN msgs_search ms
ON m.id=ms.rowid
LEFT JOIN contacts ct
ON m.from_id=ct.id
LEFT JOIN chats c
@@ -1140,7 +1144,7 @@ impl Context {
AND m.hidden=0
AND c.blocked!=1
AND ct.blocked=0
AND m.txt LIKE ?
AND ms.txt LIKE ?
ORDER BY m.id DESC LIMIT 1000",
(str_like_in_text,),
|row| row.get::<_, MsgId>("id"),
@@ -1537,6 +1541,8 @@ mod tests {
msg2.set_text("barbaz".to_string());
send_msg(&alice, chat.id, &mut msg2).await?;
alice.send_text(chat.id, "Δ-Chat").await;
// Global search with a part of text finds the message.
let res = alice.search_msgs(None, "ob").await?;
assert_eq!(res.len(), 1);
@@ -1549,6 +1555,12 @@ mod tests {
assert_eq!(res.first(), Some(&msg2.id));
assert_eq!(res.get(1), Some(&msg1.id));
// Search is case-insensitive.
for chat_id in [None, Some(chat.id)] {
let res = alice.search_msgs(chat_id, "δ-chat").await?;
assert_eq!(res.len(), 1);
}
// Global search with longer text does not find any message.
let res = alice.search_msgs(None, "foobarbaz").await?;
assert!(res.is_empty());

View File

@@ -911,6 +911,24 @@ CREATE INDEX msgs_status_updates_index2 ON msgs_status_updates (uid);
.await?;
}
if dbversion < 111 {
sql.execute_migration(
"CREATE VIRTUAL TABLE msgs_search USING fts5(txt, content='msgs', content_rowid='id'); \
CREATE TRIGGER msgs_search_insert AFTER INSERT ON msgs BEGIN \
INSERT INTO msgs_search (rowid, txt) VALUES (new.id, new.txt); \
END; \
CREATE TRIGGER msgs_search_delete AFTER DELETE ON msgs BEGIN \
INSERT INTO msgs_search (msgs_search, rowid, txt) VALUES ('delete', old.id, old.txt); \
END; \
CREATE TRIGGER msgs_search_update AFTER UPDATE ON msgs BEGIN \
INSERT INTO msgs_search (msgs_search, rowid, txt) VALUES ('delete', old.id, old.txt); \
INSERT INTO msgs_search (rowid, txt) VALUES (new.id, new.txt); \
END; \
INSERT INTO msgs_search (msgs_search) VALUES ('rebuild');",
111,
).await?;
}
let new_version = sql
.get_raw_config_int(VERSION_CFG)
.await?