implement get document

This commit is contained in:
mpostma 2021-02-11 10:59:23 +01:00
parent 8fd9dc231c
commit 6766de437f
No known key found for this signature in database
GPG Key ID: CBC8A7C1D7A28C3A
3 changed files with 61 additions and 10 deletions

View File

@ -1,7 +1,6 @@
use std::collections::HashSet;
use std::mem;
use std::time::Instant;
use std::ops::RangeBounds;
use anyhow::{bail, Context};
use meilisearch_tokenizer::{Analyzer, AnalyzerConfig};
@ -171,15 +170,14 @@ impl Data {
&self,
index: impl AsRef<str>,
offset: usize,
count: usize,
limit: usize,
attributes_to_retrieve: Option<&[&str]>,
) -> anyhow::Result<Vec<Map<String, Value>>> {
let index = self.index_controller
.index(&index)?
.with_context(|| format!("Index {:?} doesn't exist", index.as_ref()))?;
let txn = index.read_txn()?;
let mut documents = Vec::new();
let txn = index.read_txn()?;
let fields_ids_map = index.fields_ids_map(&txn)?;
@ -194,7 +192,9 @@ impl Data {
let iter = index.documents.range(&txn, &(..))?
.skip(offset)
.take(count);
.take(limit);
let mut documents = Vec::new();
for entry in iter {
let (_id, obkv) = entry?;
@ -204,4 +204,42 @@ impl Data {
Ok(documents)
}
pub fn retrieve_document(
&self,
index: impl AsRef<str>,
document_id: impl AsRef<str>,
attributes_to_retrieve: Option<&[&str]>,
) -> anyhow::Result<Map<String, Value>> {
let index = self.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
.as_ref()
.iter()
.filter_map(|f| fields_ids_map.id(f))
.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()),
}
}
}

View File

@ -254,6 +254,7 @@ where
/// Trying to abort an update that is currently being processed, an update
/// that as already been processed or which doesn't actually exist, will
/// return `None`.
#[allow(dead_code)]
pub fn abort_update(&self, update_id: u64) -> heed::Result<Option<Aborted<M>>> {
let mut wtxn = self.env.write_txn()?;
let key = BEU64::new(update_id);
@ -281,6 +282,7 @@ where
/// Aborts all the pending updates, and not the one being currently processed.
/// Returns the update metas and ids that were successfully aborted.
#[allow(dead_code)]
pub fn abort_pendings(&self) -> heed::Result<Vec<(u64, Aborted<M>)>> {
let mut wtxn = self.env.write_txn()?;
let mut aborted_updates = Vec::new();

View File

@ -33,8 +33,8 @@ type Document = IndexMap<String, Value>;
#[derive(Deserialize)]
struct DocumentParam {
_index_uid: String,
_document_id: String,
index_uid: String,
document_id: String,
}
pub fn services(cfg: &mut web::ServiceConfig) {
@ -52,10 +52,21 @@ pub fn services(cfg: &mut web::ServiceConfig) {
wrap = "Authentication::Public"
)]
async fn get_document(
_data: web::Data<Data>,
_path: web::Path<DocumentParam>,
data: web::Data<Data>,
path: web::Path<DocumentParam>,
) -> Result<HttpResponse, ResponseError> {
todo!()
let index = &path.index_uid;
let id = &path.document_id;
match data.retrieve_document(index, id, None) {
Ok(document) => {
let json = serde_json::to_string(&document).unwrap();
Ok(HttpResponse::Ok().body(json))
}
Err(e) => {
error!("{}", e);
unimplemented!()
}
}
}
#[delete(