feat(sdk): Lazily create RoomIndex on search.

This commit is contained in:
Shrey Patel
2025-09-17 11:14:11 +01:00
committed by Damir Jelić
parent 79aa0ab60d
commit 4b87dfea0b
3 changed files with 18 additions and 19 deletions

View File

@@ -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<usize>,
) -> Option<Vec<OwnedEventId>> {
let search_index_guard = self.client.search_index().lock().await;
) -> Result<Vec<OwnedEventId>, 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())
}

View File

@@ -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<usize>,
room_id: &RoomId,
) -> Option<Vec<OwnedEventId>> {
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<Vec<OwnedEventId>, 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

View File

@@ -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.")