refactor index serach for better error handling

This commit is contained in:
mpostma 2021-03-03 11:53:01 +01:00
parent 7c7143d435
commit 70d935a2da
No known key found for this signature in database
GPG Key ID: CBC8A7C1D7A28C3A

View File

@ -87,9 +87,31 @@ impl<S: IndexStore + Sync + Send> IndexActor<S> {
async fn handle_search(&self, uuid: Uuid, query: SearchQuery, ret: oneshot::Sender<anyhow::Result<SearchResult>>) { async fn handle_search(&self, uuid: Uuid, query: SearchQuery, ret: oneshot::Sender<anyhow::Result<SearchResult>>) {
let index = self.store.get(uuid).await.unwrap().unwrap(); let index = self.store.get(uuid).await.unwrap().unwrap();
tokio::task::spawn_blocking(move || { tokio::task::spawn_blocking(move || {
let result = perform_search(&index, query);
ret.send(result)
});
}
async fn handle_create_index(&self, uuid: Uuid, primary_key: Option<String>, ret: oneshot::Sender<Result<IndexMetadata>>) {
let result = self.store.create_index(uuid, primary_key).await;
let _ = ret.send(result);
}
async fn handle_update(&self, meta: Processing<UpdateMeta>, data: File, ret: oneshot::Sender<UpdateResult>) {
info!("processing update {}", meta.id());
let uuid = meta.index_uuid().clone();
let index = self.store.get_or_create(uuid).await.unwrap();
let update_handler = self.update_handler.clone();
let result = tokio::task::spawn_blocking(move || update_handler.handle_update(meta, data, index.as_ref())).await;
let result = result.unwrap();
let _ = ret.send(result);
}
}
fn perform_search(index: &Index, query: SearchQuery) -> anyhow::Result<SearchResult> {
let before_search = Instant::now(); let before_search = Instant::now();
let rtxn = index.read_txn().unwrap(); let rtxn = index.read_txn()?;
let mut search = index.search(&rtxn); let mut search = index.search(&rtxn);
@ -110,7 +132,7 @@ impl<S: IndexStore + Sync + Send> IndexActor<S> {
found_words, found_words,
candidates, candidates,
.. ..
} = search.execute().unwrap(); } = search.execute()?;
let mut documents = Vec::new(); let mut documents = Vec::new();
let fields_ids_map = index.fields_ids_map(&rtxn).unwrap(); let fields_ids_map = index.fields_ids_map(&rtxn).unwrap();
@ -135,7 +157,7 @@ impl<S: IndexStore + Sync + Send> IndexActor<S> {
let stop_words = fst::Set::default(); let stop_words = fst::Set::default();
let highlighter = crate::data::search::Highlighter::new(&stop_words); let highlighter = crate::data::search::Highlighter::new(&stop_words);
for (_id, obkv) in index.documents(&rtxn, documents_ids).unwrap() { for (_id, obkv) in index.documents(&rtxn, documents_ids)? {
let mut object = milli::obkv_to_json(&displayed_fields_ids, &fields_ids_map, obkv).unwrap(); let mut object = milli::obkv_to_json(&displayed_fields_ids, &fields_ids_map, obkv).unwrap();
if let Some(ref attributes_to_highlight) = query.attributes_to_highlight { if let Some(ref attributes_to_highlight) = query.attributes_to_highlight {
highlighter.highlight_record(&mut object, &found_words, attributes_to_highlight); highlighter.highlight_record(&mut object, &found_words, attributes_to_highlight);
@ -151,12 +173,12 @@ impl<S: IndexStore + Sync + Send> IndexActor<S> {
if fields.iter().all(|f| f != "*") { if fields.iter().all(|f| f != "*") {
facet_distribution.facets(fields); facet_distribution.facets(fields);
} }
Some(facet_distribution.candidates(candidates).execute().unwrap()) Some(facet_distribution.candidates(candidates).execute()?)
} }
None => None, None => None,
}; };
let result = Ok(SearchResult { let result = SearchResult {
hits: documents, hits: documents,
nb_hits, nb_hits,
query: query.q.clone().unwrap_or_default(), query: query.q.clone().unwrap_or_default(),
@ -164,27 +186,8 @@ impl<S: IndexStore + Sync + Send> IndexActor<S> {
offset: query.offset.unwrap_or_default(), offset: query.offset.unwrap_or_default(),
processing_time_ms: before_search.elapsed().as_millis(), processing_time_ms: before_search.elapsed().as_millis(),
facet_distributions, facet_distributions,
}); };
Ok(result)
ret.send(result)
});
}
async fn handle_create_index(&self, uuid: Uuid, primary_key: Option<String>, ret: oneshot::Sender<Result<IndexMetadata>>) {
let result = self.store.create_index(uuid, primary_key).await;
let _ = ret.send(result);
}
async fn handle_update(&self, meta: Processing<UpdateMeta>, data: File, ret: oneshot::Sender<UpdateResult>) {
info!("processing update");
let uuid = meta.index_uuid().clone();
let index = self.store.get_or_create(uuid).await.unwrap();
let update_handler = self.update_handler.clone();
let result = tokio::task::spawn_blocking(move || update_handler.handle_update(meta, data, index.as_ref())).await;
let result = result.unwrap();
let _ = ret.send(result);
}
} }
#[derive(Clone)] #[derive(Clone)]