meilisearch/src/data/search.rs

74 lines
2.5 KiB
Rust
Raw Normal View History

2021-02-17 01:21:16 +08:00
use serde_json::{Map, Value};
2020-12-12 20:32:06 +08:00
2021-03-04 18:23:41 +08:00
use crate::index::{SearchQuery, SearchResult};
2020-12-29 18:11:06 +08:00
use super::Data;
2020-12-12 20:32:06 +08:00
2020-12-29 18:11:06 +08:00
impl Data {
pub async fn search<S: AsRef<str>>(
2021-02-17 01:21:16 +08:00
&self,
index: S,
search_query: SearchQuery,
2021-02-17 01:21:16 +08:00
) -> anyhow::Result<SearchResult> {
self.index_controller.search(index.as_ref().to_string(), search_query).await
2020-12-24 19:58:34 +08:00
}
2021-02-11 00:08:37 +08:00
2021-03-04 21:20:19 +08:00
pub async fn retrieve_documents(
2021-02-11 00:08:37 +08:00
&self,
2021-03-04 21:20:19 +08:00
index: String,
offset: usize,
limit: usize,
attributes_to_retrieve: Option<Vec<String>>,
) -> anyhow::Result<Vec<Map<String, Value>>> {
self.index_controller.documents(index, offset, limit, attributes_to_retrieve).await
2021-02-11 00:08:37 +08:00
}
2021-02-11 17:59:23 +08:00
2021-02-16 06:02:20 +08:00
pub async fn retrieve_document<S>(
2021-02-11 17:59:23 +08:00
&self,
2021-02-26 16:10:04 +08:00
_index: impl AsRef<str> + Sync + Send + 'static,
_document_id: impl AsRef<str> + Sync + Send + 'static,
2021-03-04 21:20:19 +08:00
_attributes_to_retrieve: Option<Vec<String>>,
2021-02-16 06:02:20 +08:00
) -> anyhow::Result<Map<String, Value>>
{
2021-02-26 16:10:04 +08:00
todo!()
//let index_controller = self.index_controller.clone();
//let document: anyhow::Result<_> = tokio::task::spawn_blocking(move || {
//let index = index_controller
//.index(&index)?
//.with_context(|| format!("Index {:?} doesn't exist", index.as_ref()))?;
//let txn = index.read_txn()?;
//let fields_ids_map = index.fields_ids_map(&txn)?;
//let attributes_to_retrieve_ids = match attributes_to_retrieve {
//Some(attrs) => attrs
//.iter()
//.filter_map(|f| fields_ids_map.id(f.as_ref()))
//.collect::<Vec<_>>(),
//None => fields_ids_map.iter().map(|(id, _)| id).collect(),
//};
//let internal_id = index
//.external_documents_ids(&txn)?
//.get(document_id.as_ref().as_bytes())
//.with_context(|| format!("Document with id {} not found", document_id.as_ref()))?;
//let document = index
//.documents(&txn, std::iter::once(internal_id))?
//.into_iter()
//.next()
//.map(|(_, d)| d);
//match document {
//Some(document) => Ok(obkv_to_json(
//&attributes_to_retrieve_ids,
//&fields_ids_map,
//document,
//)?),
//None => bail!("Document with id {} not found", document_id.as_ref()),
//}
//})
//.await?;
//document
2021-02-11 17:59:23 +08:00
}
2020-12-24 19:58:34 +08:00
}