From 63fab0704711113109b2655888cd968084bbe3f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Tue, 10 Nov 2020 17:00:38 +0100 Subject: [PATCH] Introduce a route to retrieve a document with its id --- http-ui/Cargo.lock | 2 -- http-ui/src/main.rs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/http-ui/Cargo.lock b/http-ui/Cargo.lock index b6539085f..7c7759e2e 100644 --- a/http-ui/Cargo.lock +++ b/http-ui/Cargo.lock @@ -812,7 +812,6 @@ checksum = "55e2e4c765aa53a0424761bf9f41aa7a6ac1efa87238f59560640e27fca028f2" dependencies = [ "autocfg 1.0.1", "hashbrown", - "serde", ] [[package]] @@ -981,7 +980,6 @@ dependencies = [ "grenad", "heed", "human_format", - "indexmap", "itertools", "jemallocator", "levenshtein_automata", diff --git a/http-ui/src/main.rs b/http-ui/src/main.rs index a72799770..281eb0c8e 100644 --- a/http-ui/src/main.rs +++ b/http-ui/src/main.rs @@ -527,11 +527,13 @@ async fn main() -> anyhow::Result<()> { } let disable_highlighting = opt.disable_highlighting; + let index_cloned = index.clone(); let query_route = warp::filters::method::post() .and(warp::path!("query")) .and(warp::body::json()) .map(move |query: QueryBody| { let before_search = Instant::now(); + let index = index_cloned.clone(); let rtxn = index.read_txn().unwrap(); let mut search = index.search(&rtxn); @@ -567,6 +569,38 @@ async fn main() -> anyhow::Result<()> { .body(serde_json::to_string(&documents).unwrap()) }); + let index_cloned = index.clone(); + let document_route = warp::filters::method::get() + .and(warp::path!("document" / String)) + .map(move |id: String| { + let index = index_cloned.clone(); + let rtxn = index.read_txn().unwrap(); + + let users_ids_documents_ids = index.users_ids_documents_ids(&rtxn).unwrap(); + let fields_ids_map = index.fields_ids_map(&rtxn).unwrap(); + let displayed_fields = match index.displayed_fields(&rtxn).unwrap() { + Some(fields) => Cow::Borrowed(fields), + None => Cow::Owned(fields_ids_map.iter().map(|(id, _)| id).collect()), + }; + + match users_ids_documents_ids.get(&id) { + Some(document_id) => { + let document_id = document_id as u32; + let (_, obkv) = index.documents(&rtxn, Some(document_id)).unwrap().pop().unwrap(); + let document = obkv_to_json(&displayed_fields, &fields_ids_map, obkv).unwrap(); + + Response::builder() + .header("Content-Type", "application/json") + .body(serde_json::to_string(&document).unwrap()) + }, + None => { + Response::builder() + .status(404) + .body(format!("Document with id {:?} not found.", id)) + }, + } + }); + async fn buf_stream( update_store: Arc>, update_status_sender: broadcast::Sender>, @@ -730,6 +764,7 @@ async fn main() -> anyhow::Result<()> { .or(dash_logo_white_route) .or(dash_logo_black_route) .or(query_route) + .or(document_route) .or(indexing_csv_route) .or(indexing_json_route) .or(indexing_json_stream_route)