mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-11-26 23:04:26 +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::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,
|
||||
}
|
||||
|
@ -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),
|
||||
|
@ -117,13 +117,14 @@ pub async fn get_all_documents(ctx: Context<Data>) -> SResult<Response> {
|
||||
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};
|
||||
|
||||
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<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> {
|
||||
ctx.is_allowed(DocumentsWrite)?;
|
||||
|
||||
let index = ctx.index()?;
|
||||
|
||||
let data: Vec<IndexMap<String, Value>> =
|
||||
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<Data>, 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)
|
||||
|
@ -72,7 +72,7 @@ pub fn load_routes(app: &mut tide::App<Data>) {
|
||||
});
|
||||
|
||||
router
|
||||
.at("/delete")
|
||||
.at("/delete-batch")
|
||||
.post(document::delete_multiple_documents);
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user