Start change http server; finish document endpoint

This commit is contained in:
Quentin de Quelen 2020-04-07 17:47:35 +02:00 committed by qdequele
parent 6cc80d2565
commit 6d6c8e8fb2
No known key found for this signature in database
GPG key ID: B3F0A000EBF11745
10 changed files with 1125 additions and 813 deletions

View file

@ -1,111 +1,104 @@
use std::collections::{BTreeSet, HashSet};
use std::collections::BTreeSet;
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use serde::Deserialize;
use serde_json::Value;
use tide::{Request, Response};
use actix_web::*;
use crate::error::{ResponseError, SResult};
use crate::helpers::tide::RequestExt;
use crate::helpers::tide::ACL::*;
use crate::error::ResponseError;
use crate::Data;
use crate::routes::IndexUpdateResponse;
pub async fn get_document(ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(Public)?;
type Document = IndexMap<String, Value>;
let index = ctx.index()?;
#[get("/indexes/{index_uid}/documents/{document_id}")]
pub async fn get_document(
data: web::Data<Data>,
path: web::Path<(String, String)>,
) -> Result<HttpResponse> {
let index = data.db.open_index(&path.0)
.ok_or(ResponseError::IndexNotFound(path.0.clone()))?;
let document_id = meilisearch_core::serde::compute_document_id(path.1.clone());
let original_document_id = ctx.document_id()?;
let document_id = meilisearch_core::serde::compute_document_id(original_document_id.clone());
let reader = data.db.main_read_txn()
.map_err(|_| ResponseError::CreateTransaction)?;
let db = &ctx.state().db;
let reader = db.main_read_txn()?;
let response = index.document::<Document, String>(&reader, None, document_id)
.map_err(|_| ResponseError::DocumentNotFound(path.1.clone()))?
.ok_or(ResponseError::DocumentNotFound(path.1.clone()))?;
let response = index
.document::<IndexMap<String, Value>>(&reader, None, document_id)?
.ok_or(ResponseError::document_not_found(&original_document_id))?;
if response.is_empty() {
return Err(ResponseError::document_not_found(&original_document_id));
}
Ok(tide::Response::new(200).body_json(&response)?)
Ok(HttpResponse::Ok().json(response))
}
#[derive(Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct IndexUpdateResponse {
pub update_id: u64,
}
#[delete("/indexes/{index_uid}/documents/{document_id}")]
pub async fn delete_document(
data: web::Data<Data>,
path: web::Path<(String, String)>,
) -> Result<HttpResponse> {
let index = data.db.open_index(&path.0)
.ok_or(ResponseError::IndexNotFound(path.0.clone()))?;
let document_id = meilisearch_core::serde::compute_document_id(path.1.clone());
pub async fn delete_document(ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(Private)?;
let mut update_writer = data.db.update_write_txn()
.map_err(|_| ResponseError::CreateTransaction)?;
let index = ctx.index()?;
let document_id = ctx.document_id()?;
let document_id = meilisearch_core::serde::compute_document_id(document_id);
let db = &ctx.state().db;
let mut update_writer = db.update_write_txn()?;
let mut documents_deletion = index.documents_deletion();
documents_deletion.delete_document_by_id(document_id);
let update_id = documents_deletion.finalize(&mut update_writer)?;
update_writer.commit()?;
let update_id = documents_deletion.finalize(&mut update_writer)
.map_err(|_| ResponseError::Internal(path.1.clone()))?;
let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body)?)
update_writer.commit()
.map_err(|_| ResponseError::CommitTransaction)?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
}
#[derive(Default, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct BrowseQuery {
pub struct BrowseQuery {
offset: Option<usize>,
limit: Option<usize>,
attributes_to_retrieve: Option<String>,
}
pub async fn get_all_documents(ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(Private)?;
#[get("/indexes/{index_uid}/documents")]
pub async fn get_all_documents(
data: web::Data<Data>,
path: web::Path<String>,
params: web::Query<BrowseQuery>,
) -> Result<HttpResponse> {
let index = ctx.index()?;
let query: BrowseQuery = ctx.query().unwrap_or_default();
let index = data.db.open_index(path.clone())
.ok_or(ResponseError::IndexNotFound(path.clone()))?;
let offset = query.offset.unwrap_or(0);
let limit = query.limit.unwrap_or(20);
let offset = params.offset.unwrap_or(0);
let limit = params.limit.unwrap_or(20);
let db = &ctx.state().db;
let reader = db.main_read_txn()?;
let reader = data.db.main_read_txn()
.map_err(|_| ResponseError::CreateTransaction)?;
let documents_ids: Result<BTreeSet<_>, _> = index
.documents_fields_counts
.documents_ids(&reader)?
.documents_ids(&reader)
.map_err(|_| ResponseError::Internal(path.clone()))?
.skip(offset)
.take(limit)
.collect();
let documents_ids = match documents_ids {
Ok(documents_ids) => documents_ids,
Err(e) => return Err(ResponseError::internal(e)),
};
let documents_ids = documents_ids.map_err(|err| ResponseError::Internal(err.to_string()))?;
let attributes = params.attributes_to_retrieve.clone()
.map(|a| a.split(',').map(|a| a.to_string()).collect());
let mut response_body = Vec::<IndexMap<String, Value>>::new();
if let Some(attributes) = query.attributes_to_retrieve {
let attributes = attributes.split(',').collect::<HashSet<&str>>();
for document_id in documents_ids {
if let Ok(Some(document)) = index.document(&reader, Some(&attributes), document_id) {
response_body.push(document);
}
}
} else {
for document_id in documents_ids {
if let Ok(Some(document)) = index.document(&reader, None, document_id) {
response_body.push(document);
}
for document_id in documents_ids {
if let Ok(Some(document)) = index.document(&reader, attributes.clone(), document_id) {
response_body.push(document);
}
}
Ok(tide::Response::new(200).body_json(&response_body)?)
Ok(HttpResponse::Ok().json(response_body))
}
fn find_primary_key(document: &IndexMap<String, Value>) -> Option<String> {
@ -119,40 +112,49 @@ fn find_primary_key(document: &IndexMap<String, Value>) -> Option<String> {
#[derive(Default, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct UpdateDocumentsQuery {
pub struct UpdateDocumentsQuery {
primary_key: Option<String>,
}
async fn update_multiple_documents(mut ctx: Request<Data>, is_partial: bool) -> SResult<Response> {
ctx.is_allowed(Private)?;
async fn update_multiple_documents(
data: web::Data<Data>,
path: web::Path<String>,
params: web::Query<UpdateDocumentsQuery>,
body: web::Json<Vec<Document>>,
is_partial: bool
) -> Result<HttpResponse> {
let index = ctx.index()?;
let index = data.db.open_index(path.clone())
.ok_or(ResponseError::IndexNotFound(path.clone()))?;
let data: Vec<IndexMap<String, Value>> =
ctx.body_json().await.map_err(ResponseError::bad_request)?;
let query: UpdateDocumentsQuery = ctx.query().unwrap_or_default();
let reader = data.db.main_read_txn()
.map_err(|_| ResponseError::CreateTransaction)?;
let db = &ctx.state().db;
let reader = db.main_read_txn()?;
let mut schema = index
.main
.schema(&reader)?
.ok_or(ResponseError::internal("schema not found"))?;
.schema(&reader)
.map_err(|_| ResponseError::Schema)?
.ok_or(ResponseError::Schema)?;
if schema.primary_key().is_none() {
let id = match query.primary_key {
let id = match params.primary_key.clone() {
Some(id) => id,
None => match data.first().and_then(|docs| find_primary_key(docs)) {
Some(id) => id,
None => return Err(ResponseError::bad_request("Could not infer a primary key")),
},
None => {
body.first()
.and_then(|docs| find_primary_key(docs))
.ok_or(ResponseError::InferPrimaryKey)?
}
};
let mut writer = db.main_write_txn()?;
schema.set_primary_key(&id).map_err(ResponseError::bad_request)?;
index.main.put_schema(&mut writer, &schema)?;
writer.commit()?;
let mut writer = data.db.main_write_txn()
.map_err(|_| ResponseError::CreateTransaction)?;
schema.set_primary_key(&id)
.map_err(|e| ResponseError::Internal(e.to_string()))?;
index.main.put_schema(&mut writer, &schema)
.map_err(|e| ResponseError::Internal(e.to_string()))?;
writer.commit()
.map_err(|_| ResponseError::CommitTransaction)?;
}
let mut document_addition = if is_partial {
@ -161,63 +163,88 @@ async fn update_multiple_documents(mut ctx: Request<Data>, is_partial: bool) ->
index.documents_addition()
};
for document in data {
for document in body.into_inner() {
document_addition.update_document(document);
}
let mut update_writer = db.update_write_txn()?;
let update_id = document_addition.finalize(&mut update_writer)?;
update_writer.commit()?;
let mut update_writer = data.db.update_write_txn()
.map_err(|_| ResponseError::CreateTransaction)?;
let update_id = document_addition.finalize(&mut update_writer)
.map_err(|e| ResponseError::Internal(e.to_string()))?;
update_writer.commit()
.map_err(|_| ResponseError::CommitTransaction)?;
let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body)?)
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
}
pub async fn add_or_replace_multiple_documents(ctx: Request<Data>) -> SResult<Response> {
update_multiple_documents(ctx, false).await
#[post("/indexes/{index_uid}/documents")]
pub async fn add_documents(
data: web::Data<Data>,
path: web::Path<String>,
params: web::Query<UpdateDocumentsQuery>,
body: web::Json<Vec<Document>>
) -> Result<HttpResponse> {
update_multiple_documents(data, path, params, body, false).await
}
pub async fn add_or_update_multiple_documents(ctx: Request<Data>) -> SResult<Response> {
update_multiple_documents(ctx, true).await
#[put("/indexes/{index_uid}/documents")]
pub async fn update_documents(
data: web::Data<Data>,
path: web::Path<String>,
params: web::Query<UpdateDocumentsQuery>,
body: web::Json<Vec<Document>>
) -> Result<HttpResponse> {
update_multiple_documents(data, path, params, body, true).await
}
pub async fn delete_multiple_documents(mut ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(Private)?;
#[post("/indexes/{index_uid}/documents/delete-batch")]
pub async fn delete_documents(
data: web::Data<Data>,
path: web::Path<String>,
body: web::Json<Vec<Value>>
) -> Result<HttpResponse> {
let data: Vec<Value> = ctx.body_json().await.map_err(ResponseError::bad_request)?;
let index = ctx.index()?;
let index = data.db.open_index(path.clone())
.ok_or(ResponseError::IndexNotFound(path.clone()))?;
let db = &ctx.state().db;
let mut writer = db.update_write_txn()?;
let mut writer = data.db.update_write_txn()
.map_err(|_| ResponseError::CreateTransaction)?;
let mut documents_deletion = index.documents_deletion();
for document_id in data {
for document_id in body.into_inner() {
if let Some(document_id) = meilisearch_core::serde::value_to_string(&document_id) {
documents_deletion
.delete_document_by_id(meilisearch_core::serde::compute_document_id(document_id));
}
}
let update_id = documents_deletion.finalize(&mut writer)?;
let update_id = documents_deletion.finalize(&mut writer)
.map_err(|e| ResponseError::Internal(e.to_string()))?;
writer.commit()?;
writer.commit()
.map_err(|_| ResponseError::CommitTransaction)?;
let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body)?)
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
}
pub async fn clear_all_documents(ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(Private)?;
#[delete("/indexes/{index_uid}/documents")]
pub async fn clear_all_documents(
data: web::Data<Data>,
path: web::Path<String>,
) -> Result<HttpResponse> {
let index = ctx.index()?;
let index = data.db.open_index(path.clone())
.ok_or(ResponseError::IndexNotFound(path.clone()))?;
let db = &ctx.state().db;
let mut writer = db.update_write_txn()?;
let mut writer = data.db.update_write_txn()
.map_err(|_| ResponseError::CreateTransaction)?;
let update_id = index.clear_all(&mut writer)?;
writer.commit()?;
let update_id = index.clear_all(&mut writer)
.map_err(|e| ResponseError::Internal(e.to_string()))?;
let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body)?)
writer.commit()
.map_err(|_| ResponseError::CommitTransaction)?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
}

View file

@ -1,130 +1,135 @@
use crate::data::Data;
use std::future::Future;
use tide::IntoResponse;
use tide::Response;
use serde::Serialize;
pub mod document;
pub mod health;
pub mod index;
pub mod key;
pub mod search;
pub mod setting;
pub mod stats;
pub mod stop_words;
pub mod synonym;
// pub mod health;
// pub mod index;
// pub mod key;
// pub mod search;
// pub mod setting;
// pub mod stats;
// pub mod stop_words;
// pub mod synonym;
async fn into_response<T: IntoResponse, U: IntoResponse>(
x: impl Future<Output = Result<T, U>>,
) -> Response {
match x.await {
Ok(resp) => resp.into_response(),
Err(resp) => resp.into_response(),
#[derive(Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct IndexUpdateResponse {
pub update_id: u64,
pub see_more: String,
}
impl IndexUpdateResponse {
pub fn with_id(update_id: u64) -> Self {
Self {
update_id,
see_more: "https://docs.meilisearch.com/guides/advanced_guides/asynchronous_updates.html".to_string()
}
}
}
pub fn load_routes(app: &mut tide::Server<Data>) {
app.at("/").get(|_| async {
tide::Response::new(200)
.body_string(include_str!("../../public/interface.html").to_string())
.set_mime(mime::TEXT_HTML_UTF_8)
});
app.at("/bulma.min.css").get(|_| async {
tide::Response::new(200)
.body_string(include_str!("../../public/bulma.min.css").to_string())
.set_mime(mime::TEXT_CSS_UTF_8)
});
// pub fn load_routes(app: &mut tide::Server<Data>) {
// app.at("/").get(|_| async {
// tide::Response::new(200)
// .body_string(include_str!("../../public/interface.html").to_string())
// .set_mime(mime::TEXT_HTML_UTF_8)
// });
// app.at("/bulma.min.css").get(|_| async {
// tide::Response::new(200)
// .body_string(include_str!("../../public/bulma.min.css").to_string())
// .set_mime(mime::TEXT_CSS_UTF_8)
// });
app.at("/indexes")
.get(|ctx| into_response(index::list_indexes(ctx)))
.post(|ctx| into_response(index::create_index(ctx)));
// app.at("/indexes")
// .get(|ctx| into_response(index::list_indexes(ctx)))
// .post(|ctx| into_response(index::create_index(ctx)));
app.at("/indexes/search")
.post(|ctx| into_response(search::search_multi_index(ctx)));
// app.at("/indexes/search")
// .post(|ctx| into_response(search::search_multi_index(ctx)));
app.at("/indexes/:index")
.get(|ctx| into_response(index::get_index(ctx)))
.put(|ctx| into_response(index::update_index(ctx)))
.delete(|ctx| into_response(index::delete_index(ctx)));
// app.at("/indexes/:index")
// .get(|ctx| into_response(index::get_index(ctx)))
// .put(|ctx| into_response(index::update_index(ctx)))
// .delete(|ctx| into_response(index::delete_index(ctx)));
app.at("/indexes/:index/search")
.get(|ctx| into_response(search::search_with_url_query(ctx)));
// app.at("/indexes/:index/search")
// .get(|ctx| into_response(search::search_with_url_query(ctx)));
app.at("/indexes/:index/updates")
.get(|ctx| into_response(index::get_all_updates_status(ctx)));
// app.at("/indexes/:index/updates")
// .get(|ctx| into_response(index::get_all_updates_status(ctx)));
app.at("/indexes/:index/updates/:update_id")
.get(|ctx| into_response(index::get_update_status(ctx)));
// app.at("/indexes/:index/updates/:update_id")
// .get(|ctx| into_response(index::get_update_status(ctx)));
app.at("/indexes/:index/documents")
.get(|ctx| into_response(document::get_all_documents(ctx)))
.post(|ctx| into_response(document::add_or_replace_multiple_documents(ctx)))
.put(|ctx| into_response(document::add_or_update_multiple_documents(ctx)))
.delete(|ctx| into_response(document::clear_all_documents(ctx)));
// app.at("/indexes/:index/documents")
// .get(|ctx| into_response(document::get_all_documents(ctx)))
// .post(|ctx| into_response(document::add_or_replace_multiple_documents(ctx)))
// .put(|ctx| into_response(document::add_or_update_multiple_documents(ctx)))
// .delete(|ctx| into_response(document::clear_all_documents(ctx)));
app.at("/indexes/:index/documents/:document_id")
.get(|ctx| into_response(document::get_document(ctx)))
.delete(|ctx| into_response(document::delete_document(ctx)));
// app.at("/indexes/:index/documents/:document_id")
// .get(|ctx| into_response(document::get_document(ctx)))
// .delete(|ctx| into_response(document::delete_document(ctx)));
app.at("/indexes/:index/documents/delete-batch")
.post(|ctx| into_response(document::delete_multiple_documents(ctx)));
// app.at("/indexes/:index/documents/delete-batch")
// .post(|ctx| into_response(document::delete_multiple_documents(ctx)));
app.at("/indexes/:index/settings")
.get(|ctx| into_response(setting::get_all(ctx)))
.post(|ctx| into_response(setting::update_all(ctx)))
.delete(|ctx| into_response(setting::delete_all(ctx)));
// app.at("/indexes/:index/settings")
// .get(|ctx| into_response(setting::get_all(ctx)))
// .post(|ctx| into_response(setting::update_all(ctx)))
// .delete(|ctx| into_response(setting::delete_all(ctx)));
app.at("/indexes/:index/settings/ranking-rules")
.get(|ctx| into_response(setting::get_rules(ctx)))
.post(|ctx| into_response(setting::update_rules(ctx)))
.delete(|ctx| into_response(setting::delete_rules(ctx)));
// app.at("/indexes/:index/settings/ranking-rules")
// .get(|ctx| into_response(setting::get_rules(ctx)))
// .post(|ctx| into_response(setting::update_rules(ctx)))
// .delete(|ctx| into_response(setting::delete_rules(ctx)));
app.at("/indexes/:index/settings/distinct-attribute")
.get(|ctx| into_response(setting::get_distinct(ctx)))
.post(|ctx| into_response(setting::update_distinct(ctx)))
.delete(|ctx| into_response(setting::delete_distinct(ctx)));
// app.at("/indexes/:index/settings/distinct-attribute")
// .get(|ctx| into_response(setting::get_distinct(ctx)))
// .post(|ctx| into_response(setting::update_distinct(ctx)))
// .delete(|ctx| into_response(setting::delete_distinct(ctx)));
app.at("/indexes/:index/settings/searchable-attributes")
.get(|ctx| into_response(setting::get_searchable(ctx)))
.post(|ctx| into_response(setting::update_searchable(ctx)))
.delete(|ctx| into_response(setting::delete_searchable(ctx)));
// app.at("/indexes/:index/settings/searchable-attributes")
// .get(|ctx| into_response(setting::get_searchable(ctx)))
// .post(|ctx| into_response(setting::update_searchable(ctx)))
// .delete(|ctx| into_response(setting::delete_searchable(ctx)));
app.at("/indexes/:index/settings/displayed-attributes")
.get(|ctx| into_response(setting::displayed(ctx)))
.post(|ctx| into_response(setting::update_displayed(ctx)))
.delete(|ctx| into_response(setting::delete_displayed(ctx)));
// app.at("/indexes/:index/settings/displayed-attributes")
// .get(|ctx| into_response(setting::displayed(ctx)))
// .post(|ctx| into_response(setting::update_displayed(ctx)))
// .delete(|ctx| into_response(setting::delete_displayed(ctx)));
app.at("/indexes/:index/settings/accept-new-fields")
.get(|ctx| into_response(setting::get_accept_new_fields(ctx)))
.post(|ctx| into_response(setting::update_accept_new_fields(ctx)));
// app.at("/indexes/:index/settings/accept-new-fields")
// .get(|ctx| into_response(setting::get_accept_new_fields(ctx)))
// .post(|ctx| into_response(setting::update_accept_new_fields(ctx)));
app.at("/indexes/:index/settings/synonyms")
.get(|ctx| into_response(synonym::get(ctx)))
.post(|ctx| into_response(synonym::update(ctx)))
.delete(|ctx| into_response(synonym::delete(ctx)));
// app.at("/indexes/:index/settings/synonyms")
// .get(|ctx| into_response(synonym::get(ctx)))
// .post(|ctx| into_response(synonym::update(ctx)))
// .delete(|ctx| into_response(synonym::delete(ctx)));
app.at("/indexes/:index/settings/stop-words")
.get(|ctx| into_response(stop_words::get(ctx)))
.post(|ctx| into_response(stop_words::update(ctx)))
.delete(|ctx| into_response(stop_words::delete(ctx)));
// app.at("/indexes/:index/settings/stop-words")
// .get(|ctx| into_response(stop_words::get(ctx)))
// .post(|ctx| into_response(stop_words::update(ctx)))
// .delete(|ctx| into_response(stop_words::delete(ctx)));
app.at("/indexes/:index/stats")
.get(|ctx| into_response(stats::index_stats(ctx)));
// app.at("/indexes/:index/stats")
// .get(|ctx| into_response(stats::index_stats(ctx)));
app.at("/keys").get(|ctx| into_response(key::list(ctx)));
// app.at("/keys").get(|ctx| into_response(key::list(ctx)));
app.at("/health")
.get(|ctx| into_response(health::get_health(ctx)))
.put(|ctx| into_response(health::change_healthyness(ctx)));
// app.at("/health")
// .get(|ctx| into_response(health::get_health(ctx)))
// .put(|ctx| into_response(health::change_healthyness(ctx)));
app.at("/stats")
.get(|ctx| into_response(stats::get_stats(ctx)));
// app.at("/stats")
// .get(|ctx| into_response(stats::get_stats(ctx)));
app.at("/version")
.get(|ctx| into_response(stats::get_version(ctx)));
// app.at("/version")
// .get(|ctx| into_response(stats::get_version(ctx)));
app.at("/sys-info")
.get(|ctx| into_response(stats::get_sys_info(ctx)));
// app.at("/sys-info")
// .get(|ctx| into_response(stats::get_sys_info(ctx)));
app.at("/sys-info/pretty")
.get(|ctx| into_response(stats::get_sys_info_pretty(ctx)));
}
// app.at("/sys-info/pretty")
// .get(|ctx| into_response(stats::get_sys_info_pretty(ctx)));
// }