From f0590d330127beca2871826e8c9b582a73ece45c Mon Sep 17 00:00:00 2001 From: qdequele Date: Thu, 2 Jan 2020 12:24:29 +0100 Subject: [PATCH] Change documents routes; fix #416 --- .../src/serde/extract_document_id.rs | 8 +++++++- meilisearch-core/src/serde/mod.rs | 2 +- meilisearch-http/src/routes/document.rs | 20 ++++++++++++++----- meilisearch-http/src/routes/mod.rs | 2 +- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/meilisearch-core/src/serde/extract_document_id.rs b/meilisearch-core/src/serde/extract_document_id.rs index 5c2a81d2e..01427c5da 100644 --- a/meilisearch-core/src/serde/extract_document_id.rs +++ b/meilisearch-core/src/serde/extract_document_id.rs @@ -23,7 +23,13 @@ pub fn value_to_string(value: &Value) -> Option { Value::Null => None, Value::Bool(_) => None, Value::Number(value) => Some(value.to_string()), - Value::String(value) => Some(value.to_string()), + Value::String(value) => { + if value.chars().all(|x| x.is_ascii_alphanumeric() || x == '-' || x == '_') { + Some(value.to_string()) + } else { + None + } + }, Value::Array(_) => None, Value::Object(_) => None, } diff --git a/meilisearch-core/src/serde/mod.rs b/meilisearch-core/src/serde/mod.rs index c25c4b158..0469f352d 100644 --- a/meilisearch-core/src/serde/mod.rs +++ b/meilisearch-core/src/serde/mod.rs @@ -55,7 +55,7 @@ impl fmt::Display for SerializerError { f.write_str("serialized document does not have an id according to the schema") } SerializerError::InvalidDocumentIdType => { - f.write_str("document identifier can only be of type string or number") + f.write_str("document identifier can only be of type number or string (A-Z, a-z, 0-9, -_)") } SerializerError::Zlmdb(e) => write!(f, "heed related error: {}", e), SerializerError::SerdeJson(e) => write!(f, "serde json error: {}", e), diff --git a/meilisearch-http/src/routes/document.rs b/meilisearch-http/src/routes/document.rs index 7c91219b8..ebb3851be 100644 --- a/meilisearch-http/src/routes/document.rs +++ b/meilisearch-http/src/routes/document.rs @@ -117,13 +117,14 @@ pub async fn get_all_documents(ctx: Context) -> SResult { Ok(tide::response::json(response_body)) } -fn infered_schema(document: &IndexMap) -> Option { +fn infered_schema(document: &IndexMap, identifier: Option) -> Option { use meilisearch_schema::{SchemaBuilder, DISPLAYED, INDEXED}; - let mut identifier = None; + let mut identifier = identifier; for key in document.keys() { if identifier.is_none() && key.to_lowercase().contains("id") { - identifier = Some(key); + identifier = Some(key.to_string()); + break; } } @@ -139,12 +140,21 @@ fn infered_schema(document: &IndexMap) -> Option, +} + async fn update_multiple_documents(mut ctx: Context, is_partial: bool) -> SResult { ctx.is_allowed(DocumentsWrite)?; + let index = ctx.index()?; + let data: Vec> = ctx.body_json().await.map_err(ResponseError::bad_request)?; - let index = ctx.index()?; + let query: UpdateDocumentsQuery = ctx + .url_query().unwrap_or_default(); let db = &ctx.state().db; let reader = db.main_read_txn().map_err(ResponseError::internal)?; @@ -155,7 +165,7 @@ async fn update_multiple_documents(mut ctx: Context, is_partial: bool) -> .schema(&reader) .map_err(ResponseError::internal)?; if current_schema.is_none() { - match data.first().and_then(infered_schema) { + match data.first().and_then(|docs| infered_schema(docs, query.identifier)) { Some(schema) => { index .schema_update(&mut update_writer, schema) diff --git a/meilisearch-http/src/routes/mod.rs b/meilisearch-http/src/routes/mod.rs index ee6d75e47..e27d2887b 100644 --- a/meilisearch-http/src/routes/mod.rs +++ b/meilisearch-http/src/routes/mod.rs @@ -72,7 +72,7 @@ pub fn load_routes(app: &mut tide::App) { }); router - .at("/delete") + .at("/delete-batch") .post(document::delete_multiple_documents); });