Introduce a route to retrieve a document with its id

This commit is contained in:
Clément Renault 2020-11-10 17:00:38 +01:00
parent c00fc6f8bb
commit 63fab07047
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4
2 changed files with 35 additions and 2 deletions

2
http-ui/Cargo.lock generated
View File

@ -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",

View File

@ -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<UpdateStore<UpdateMeta, String>>,
update_status_sender: broadcast::Sender<UpdateStatus<UpdateMeta, UpdateMetaProgress, String>>,
@ -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)