mirror of
https://github.com/chatmail/core.git
synced 2026-04-18 22:16:30 +03:00
feat: Try SQLite FTS5 (#5052)
This commit is contained in:
@@ -1099,12 +1099,14 @@ impl Context {
|
|||||||
.query_map(
|
.query_map(
|
||||||
"SELECT m.id AS id
|
"SELECT m.id AS id
|
||||||
FROM msgs m
|
FROM msgs m
|
||||||
|
INNER JOIN msgs_search ms
|
||||||
|
ON m.id=ms.rowid
|
||||||
LEFT JOIN contacts ct
|
LEFT JOIN contacts ct
|
||||||
ON m.from_id=ct.id
|
ON m.from_id=ct.id
|
||||||
WHERE m.chat_id=?
|
WHERE m.chat_id=?
|
||||||
AND m.hidden=0
|
AND m.hidden=0
|
||||||
AND ct.blocked=0
|
AND ct.blocked=0
|
||||||
AND txt LIKE ?
|
AND ms.txt LIKE ?
|
||||||
ORDER BY m.timestamp,m.id;",
|
ORDER BY m.timestamp,m.id;",
|
||||||
(chat_id, str_like_in_text),
|
(chat_id, str_like_in_text),
|
||||||
|row| row.get::<_, MsgId>("id"),
|
|row| row.get::<_, MsgId>("id"),
|
||||||
@@ -1132,6 +1134,8 @@ impl Context {
|
|||||||
.query_map(
|
.query_map(
|
||||||
"SELECT m.id AS id
|
"SELECT m.id AS id
|
||||||
FROM msgs m
|
FROM msgs m
|
||||||
|
INNER JOIN msgs_search ms
|
||||||
|
ON m.id=ms.rowid
|
||||||
LEFT JOIN contacts ct
|
LEFT JOIN contacts ct
|
||||||
ON m.from_id=ct.id
|
ON m.from_id=ct.id
|
||||||
LEFT JOIN chats c
|
LEFT JOIN chats c
|
||||||
@@ -1140,7 +1144,7 @@ impl Context {
|
|||||||
AND m.hidden=0
|
AND m.hidden=0
|
||||||
AND c.blocked!=1
|
AND c.blocked!=1
|
||||||
AND ct.blocked=0
|
AND ct.blocked=0
|
||||||
AND m.txt LIKE ?
|
AND ms.txt LIKE ?
|
||||||
ORDER BY m.id DESC LIMIT 1000",
|
ORDER BY m.id DESC LIMIT 1000",
|
||||||
(str_like_in_text,),
|
(str_like_in_text,),
|
||||||
|row| row.get::<_, MsgId>("id"),
|
|row| row.get::<_, MsgId>("id"),
|
||||||
@@ -1537,6 +1541,8 @@ mod tests {
|
|||||||
msg2.set_text("barbaz".to_string());
|
msg2.set_text("barbaz".to_string());
|
||||||
send_msg(&alice, chat.id, &mut msg2).await?;
|
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.
|
// Global search with a part of text finds the message.
|
||||||
let res = alice.search_msgs(None, "ob").await?;
|
let res = alice.search_msgs(None, "ob").await?;
|
||||||
assert_eq!(res.len(), 1);
|
assert_eq!(res.len(), 1);
|
||||||
@@ -1549,6 +1555,12 @@ mod tests {
|
|||||||
assert_eq!(res.first(), Some(&msg2.id));
|
assert_eq!(res.first(), Some(&msg2.id));
|
||||||
assert_eq!(res.get(1), Some(&msg1.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.
|
// Global search with longer text does not find any message.
|
||||||
let res = alice.search_msgs(None, "foobarbaz").await?;
|
let res = alice.search_msgs(None, "foobarbaz").await?;
|
||||||
assert!(res.is_empty());
|
assert!(res.is_empty());
|
||||||
|
|||||||
@@ -911,6 +911,24 @@ CREATE INDEX msgs_status_updates_index2 ON msgs_status_updates (uid);
|
|||||||
.await?;
|
.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
|
let new_version = sql
|
||||||
.get_raw_config_int(VERSION_CFG)
|
.get_raw_config_int(VERSION_CFG)
|
||||||
.await?
|
.await?
|
||||||
|
|||||||
Reference in New Issue
Block a user