mirror of
https://github.com/meilisearch/MeiliSearch
synced 2025-07-04 04:17:10 +02:00
review the internal schema to allow to create schema without identifier; fix #513
This commit is contained in:
parent
16a99aa95e
commit
86c3482cbd
11 changed files with 312 additions and 129 deletions
|
@ -1,7 +1,6 @@
|
|||
use std::collections::{BTreeSet, HashSet};
|
||||
|
||||
use indexmap::IndexMap;
|
||||
use meilisearch_schema::Schema;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use tide::{Request, Response};
|
||||
|
@ -134,10 +133,11 @@ async fn update_multiple_documents(mut ctx: Request<Data>, is_partial: bool) ->
|
|||
let query: UpdateDocumentsQuery = ctx.query().unwrap_or_default();
|
||||
|
||||
let db = &ctx.state().db;
|
||||
let reader = db.main_read_txn()?;
|
||||
|
||||
let current_schema = index.main.schema(&reader)?;
|
||||
if current_schema.is_none() {
|
||||
let reader = db.main_read_txn()?;
|
||||
let mut schema = index.main.schema(&reader)?.ok_or(ResponseError::internal("schema not found"))?;
|
||||
|
||||
if schema.identifier().is_none() {
|
||||
let id = match query.identifier {
|
||||
Some(id) => id,
|
||||
None => match data.first().and_then(|docs| find_identifier(docs)) {
|
||||
|
@ -145,9 +145,12 @@ async fn update_multiple_documents(mut ctx: Request<Data>, is_partial: bool) ->
|
|||
None => return Err(ResponseError::bad_request("Could not infer a schema")),
|
||||
},
|
||||
};
|
||||
let mut writer = db.main_write_txn()?;
|
||||
index.main.put_schema(&mut writer, &Schema::with_identifier(&id))?;
|
||||
writer.commit()?;
|
||||
|
||||
if schema.set_identifier(&id).is_ok() {
|
||||
let mut writer = db.main_write_txn()?;
|
||||
index.main.put_schema(&mut writer, &schema)?;
|
||||
writer.commit()?;
|
||||
}
|
||||
}
|
||||
|
||||
let mut document_addition = if is_partial {
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
use chrono::{DateTime, Utc};
|
||||
use log::error;
|
||||
use meilisearch_core::ProcessedUpdateResult;
|
||||
use meilisearch_schema::Schema;
|
||||
use rand::seq::SliceRandom;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
|
@ -41,7 +40,10 @@ pub async fn list_indexes(ctx: Request<Data>) -> SResult<Response> {
|
|||
let updated_at = index.main.updated_at(&reader)?.into_internal_error()?;
|
||||
|
||||
let identifier = match index.main.schema(&reader) {
|
||||
Ok(Some(schema)) => Some(schema.identifier().to_owned()),
|
||||
Ok(Some(schema)) => match schema.identifier() {
|
||||
Some(identifier) => Some(identifier.to_owned()),
|
||||
None => None
|
||||
},
|
||||
_ => None,
|
||||
};
|
||||
|
||||
|
@ -88,7 +90,10 @@ pub async fn get_index(ctx: Request<Data>) -> SResult<Response> {
|
|||
let updated_at = index.main.updated_at(&reader)?.into_internal_error()?;
|
||||
|
||||
let identifier = match index.main.schema(&reader) {
|
||||
Ok(Some(schema)) => Some(schema.identifier().to_owned()),
|
||||
Ok(Some(schema)) => match schema.identifier() {
|
||||
Some(identifier) => Some(identifier.to_owned()),
|
||||
None => None
|
||||
},
|
||||
_ => None,
|
||||
};
|
||||
|
||||
|
@ -171,9 +176,11 @@ pub async fn create_index(mut ctx: Request<Data>) -> SResult<Response> {
|
|||
.into_internal_error()?;
|
||||
|
||||
if let Some(id) = body.identifier.clone() {
|
||||
created_index
|
||||
.main
|
||||
.put_schema(&mut writer, &Schema::with_identifier(&id))?;
|
||||
if let Some(mut schema) = created_index.main.schema(&mut writer)? {
|
||||
if let Ok(_) = schema.set_identifier(&id) {
|
||||
created_index.main.put_schema(&mut writer, &schema)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
writer.commit()?;
|
||||
|
@ -224,15 +231,19 @@ pub async fn update_index(mut ctx: Request<Data>) -> SResult<Response> {
|
|||
index.main.put_name(&mut writer, &name)?;
|
||||
}
|
||||
|
||||
if let Some(identifier) = body.identifier {
|
||||
if let Ok(Some(_)) = index.main.schema(&writer) {
|
||||
return Err(ResponseError::bad_request(
|
||||
"The index identifier cannot be updated",
|
||||
));
|
||||
if let Some(id) = body.identifier.clone() {
|
||||
if let Some(mut schema) = index.main.schema(&mut writer)? {
|
||||
match schema.identifier() {
|
||||
Some(_) => {
|
||||
return Err(ResponseError::bad_request("The index identifier cannot be updated"));
|
||||
},
|
||||
None => {
|
||||
if let Ok(_) = schema.set_identifier(&id) {
|
||||
index.main.put_schema(&mut writer, &schema)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
index
|
||||
.main
|
||||
.put_schema(&mut writer, &Schema::with_identifier(&identifier))?;
|
||||
}
|
||||
|
||||
index.main.put_updated_at(&mut writer)?;
|
||||
|
@ -244,7 +255,15 @@ pub async fn update_index(mut ctx: Request<Data>) -> SResult<Response> {
|
|||
let updated_at = index.main.updated_at(&reader)?.into_internal_error()?;
|
||||
|
||||
let identifier = match index.main.schema(&reader) {
|
||||
Ok(Some(schema)) => Some(schema.identifier().to_owned()),
|
||||
Ok(Some(schema)) => {
|
||||
match schema.identifier() {
|
||||
Some(identifier) => {
|
||||
Some(identifier.to_owned())
|
||||
},
|
||||
None => None
|
||||
}
|
||||
|
||||
},
|
||||
_ => None,
|
||||
};
|
||||
|
||||
|
|
|
@ -50,11 +50,7 @@ pub async fn get_all(ctx: Request<Data>) -> SResult<Response> {
|
|||
.iter()
|
||||
.map(|s| (*s).to_string())
|
||||
.collect::<Vec<String>>();
|
||||
if attrs.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(attrs)
|
||||
}
|
||||
Some(attrs)
|
||||
});
|
||||
|
||||
let displayed_attributes = schema.clone().map(|s| {
|
||||
|
@ -63,11 +59,7 @@ pub async fn get_all(ctx: Request<Data>) -> SResult<Response> {
|
|||
.iter()
|
||||
.map(|s| (*s).to_string())
|
||||
.collect::<HashSet<String>>();
|
||||
if attrs.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(attrs)
|
||||
}
|
||||
Some(attrs)
|
||||
});
|
||||
let accept_new_fields = schema.map(|s| s.accept_new_fields());
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue