Skip to main content

sonic/executor/
suggest.rs

1// Sonic
2//
3// Fast, lightweight and schema-less search backend
4// Copyright: 2019, Valerian Saliou <valerian@valeriansaliou.name>
5// Copyright: 2026, Rémi Bardon <remi@remibardon.name>
6// License: Mozilla Public License v2.0 (MPL v2.0)
7
8use crate::lexer::TokenLexer;
9use crate::query::{QuerySearchID, QuerySearchLimit};
10use crate::store::StoreItem;
11use crate::store::fst::StoreFSTActionBuilder;
12
13impl super::Executor {
14    pub fn suggest(
15        &self,
16        item: StoreItem,
17        _event_id: QuerySearchID,
18        mut lexer: TokenLexer,
19        limit: QuerySearchLimit,
20    ) -> Result<Option<impl ExactSizeIterator<Item = String> + DoubleEndedIterator>, ()> {
21        if let StoreItem(collection, Some(bucket), None) = item {
22            // Important: acquire graph access read lock, and reference it in context. This \
23            //   prevents the graph from being erased while using it in this block.
24            let _fst_read_guard = self.fst_pool.lock_read_access();
25
26            if let Ok(fst_store) = self.fst_pool.acquire(collection, bucket) {
27                let fst_action = StoreFSTActionBuilder::access(fst_store);
28
29                if let (Some(word), None) = (lexer.next(), lexer.next()) {
30                    tracing::debug!("running suggest on word: {}", word.0);
31
32                    return match fst_action.suggest_words(&word.0, word.2, limit as usize, None) {
33                        Some(words) => Ok(Some(words.map(|(k, _)| k))),
34                        None => Ok(None),
35                    };
36                }
37            }
38        }
39
40        Err(())
41    }
42}