diff --git a/meilisearch-http/src/routes/document.rs b/meilisearch-http/src/routes/document.rs index 38e333aab..78d8345d6 100644 --- a/meilisearch-http/src/routes/document.rs +++ b/meilisearch-http/src/routes/document.rs @@ -159,6 +159,36 @@ struct UpdateDocumentsQuery { primary_key: Option, } +fn create_index(data: &Data, uid: &str) -> Result { + if !uid + .chars() + .all(|x| x.is_ascii_alphanumeric() || x == '-' || x == '_') + { + return Err(Error::InvalidIndexUid.into()); + } + + let created_index = data.db.create_index(&uid).map_err(|e| match e { + meilisearch_core::Error::IndexAlreadyExists => e.into(), + _ => ResponseError::from(Error::create_index(e)), + })?; + + data.db.main_write::<_, _, ResponseError>(|mut writer| { + created_index.main.put_name(&mut writer, uid)?; + + created_index + .main + .created_at(&writer)? + .ok_or(Error::internal("Impossible to read created at"))?; + + created_index + .main + .updated_at(&writer)? + .ok_or(Error::internal("Impossible to read updated at"))?; + Ok(()) + })?; + + Ok(created_index) +} async fn update_multiple_documents( data: web::Data, path: web::Path, @@ -169,7 +199,8 @@ async fn update_multiple_documents( let index = data .db .open_index(&path.index_uid) - .ok_or(Error::index_not_found(&path.index_uid))?; + .ok_or(Error::index_not_found(&path.index_uid)) + .or(create_index(&data, &path.index_uid))?; let reader = data.db.main_read_txn()?;