From 4b87dfea0bd7b302fcfda3eeda06b574c5a01e23 Mon Sep 17 00:00:00 2001 From: Shrey Patel Date: Wed, 17 Sep 2025 11:14:11 +0100 Subject: [PATCH] feat(sdk): Lazily create `RoomIndex` on search. --- crates/matrix-sdk/src/room/mod.rs | 6 ++++-- crates/matrix-sdk/src/search_index/mod.rs | 23 ++++++++++------------- labs/multiverse/src/main.rs | 8 ++++---- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/crates/matrix-sdk/src/room/mod.rs b/crates/matrix-sdk/src/room/mod.rs index 59aa97827..d8cd42fc8 100644 --- a/crates/matrix-sdk/src/room/mod.rs +++ b/crates/matrix-sdk/src/room/mod.rs @@ -56,6 +56,8 @@ use matrix_sdk_common::{ timeout::timeout, }; #[cfg(feature = "experimental-search")] +use matrix_sdk_search::error::IndexError; +#[cfg(feature = "experimental-search")] #[cfg(doc)] use matrix_sdk_search::index::RoomIndex; use mime::Mime; @@ -4122,8 +4124,8 @@ impl Room { query: &str, max_number_of_results: usize, pagination_offset: Option, - ) -> Option> { - let search_index_guard = self.client.search_index().lock().await; + ) -> Result, IndexError> { + let mut search_index_guard = self.client.search_index().lock().await; search_index_guard.search(query, max_number_of_results, pagination_offset, self.room_id()) } diff --git a/crates/matrix-sdk/src/search_index/mod.rs b/crates/matrix-sdk/src/search_index/mod.rs index 705709f2f..bd7394f90 100644 --- a/crates/matrix-sdk/src/search_index/mod.rs +++ b/crates/matrix-sdk/src/search_index/mod.rs @@ -36,7 +36,7 @@ use ruma::{ room_version_rules::RedactionRules, }; use tokio::sync::{Mutex, MutexGuard}; -use tracing::{debug, error, warn}; +use tracing::{debug, warn}; use crate::event_cache::RoomEventCache; @@ -141,23 +141,20 @@ impl SearchIndexGuard<'_> { /// Search a [`Room`]'s index for the query and return at most /// max_number_of_results results. pub(crate) fn search( - &self, + &mut self, query: &str, max_number_of_results: usize, pagination_offset: Option, room_id: &RoomId, - ) -> Option> { - if let Some(index) = self.index_map.get(room_id) { - index - .search(query, max_number_of_results, pagination_offset) - .inspect_err(|err| { - error!("error occurred while searching index: {err:?}"); - }) - .ok() - } else { - debug!("Tried to search in a room with no index"); - None + ) -> Result, IndexError> { + if !self.index_map.contains_key(room_id) { + let index = self.create_index(room_id)?; + self.index_map.insert(room_id.to_owned(), index); } + + let index = self.index_map.get_mut(room_id).expect("index should exist"); + + index.search(query, max_number_of_results, pagination_offset) } /// Given a [`TimelineEvent`] this function will derive a diff --git a/labs/multiverse/src/main.rs b/labs/multiverse/src/main.rs index 63921fcfd..f6894c8c4 100644 --- a/labs/multiverse/src/main.rs +++ b/labs/multiverse/src/main.rs @@ -475,8 +475,10 @@ impl App { Enter => { if let Some(query) = view.get_text() { if let Some(room) = self.room_view.room() { - if let Some(results) = - room.search(&query, 100, None).await + if let Ok(results) = + room.search(&query, 100, None).await.inspect_err(|err| { + error!("error occurred while searching index: {err:?}"); + }) { let results = get_events_from_event_ids( &self.client, @@ -486,8 +488,6 @@ impl App { .await; view.results(results); - } else { - debug!("No results found in search.") } } else { warn!("No room in view.")