mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-11-23 05:14:27 +01:00
Change documents routes; fix #416
This commit is contained in:
parent
a5c5df0290
commit
f0590d3301
@ -23,7 +23,13 @@ pub fn value_to_string(value: &Value) -> Option<String> {
|
|||||||
Value::Null => None,
|
Value::Null => None,
|
||||||
Value::Bool(_) => None,
|
Value::Bool(_) => None,
|
||||||
Value::Number(value) => Some(value.to_string()),
|
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::Array(_) => None,
|
||||||
Value::Object(_) => None,
|
Value::Object(_) => None,
|
||||||
}
|
}
|
||||||
|
@ -55,7 +55,7 @@ impl fmt::Display for SerializerError {
|
|||||||
f.write_str("serialized document does not have an id according to the schema")
|
f.write_str("serialized document does not have an id according to the schema")
|
||||||
}
|
}
|
||||||
SerializerError::InvalidDocumentIdType => {
|
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::Zlmdb(e) => write!(f, "heed related error: {}", e),
|
||||||
SerializerError::SerdeJson(e) => write!(f, "serde json error: {}", e),
|
SerializerError::SerdeJson(e) => write!(f, "serde json error: {}", e),
|
||||||
|
@ -117,13 +117,14 @@ pub async fn get_all_documents(ctx: Context<Data>) -> SResult<Response> {
|
|||||||
Ok(tide::response::json(response_body))
|
Ok(tide::response::json(response_body))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn infered_schema(document: &IndexMap<String, Value>) -> Option<meilisearch_schema::Schema> {
|
fn infered_schema(document: &IndexMap<String, Value>, identifier: Option<String>) -> Option<meilisearch_schema::Schema> {
|
||||||
use meilisearch_schema::{SchemaBuilder, DISPLAYED, INDEXED};
|
use meilisearch_schema::{SchemaBuilder, DISPLAYED, INDEXED};
|
||||||
|
|
||||||
let mut identifier = None;
|
let mut identifier = identifier;
|
||||||
for key in document.keys() {
|
for key in document.keys() {
|
||||||
if identifier.is_none() && key.to_lowercase().contains("id") {
|
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<String, Value>) -> Option<meilisearch_sche
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Deserialize)]
|
||||||
|
#[serde(deny_unknown_fields)]
|
||||||
|
struct UpdateDocumentsQuery {
|
||||||
|
identifier: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
async fn update_multiple_documents(mut ctx: Context<Data>, is_partial: bool) -> SResult<Response> {
|
async fn update_multiple_documents(mut ctx: Context<Data>, is_partial: bool) -> SResult<Response> {
|
||||||
ctx.is_allowed(DocumentsWrite)?;
|
ctx.is_allowed(DocumentsWrite)?;
|
||||||
|
|
||||||
|
let index = ctx.index()?;
|
||||||
|
|
||||||
let data: Vec<IndexMap<String, Value>> =
|
let data: Vec<IndexMap<String, Value>> =
|
||||||
ctx.body_json().await.map_err(ResponseError::bad_request)?;
|
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 db = &ctx.state().db;
|
||||||
let reader = db.main_read_txn().map_err(ResponseError::internal)?;
|
let reader = db.main_read_txn().map_err(ResponseError::internal)?;
|
||||||
@ -155,7 +165,7 @@ async fn update_multiple_documents(mut ctx: Context<Data>, is_partial: bool) ->
|
|||||||
.schema(&reader)
|
.schema(&reader)
|
||||||
.map_err(ResponseError::internal)?;
|
.map_err(ResponseError::internal)?;
|
||||||
if current_schema.is_none() {
|
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) => {
|
Some(schema) => {
|
||||||
index
|
index
|
||||||
.schema_update(&mut update_writer, schema)
|
.schema_update(&mut update_writer, schema)
|
||||||
|
@ -72,7 +72,7 @@ pub fn load_routes(app: &mut tide::App<Data>) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
router
|
router
|
||||||
.at("/delete")
|
.at("/delete-batch")
|
||||||
.post(document::delete_multiple_documents);
|
.post(document::delete_multiple_documents);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user