Merge pull request #321 from meilisearch/fix-create-index

Fix index creation
This commit is contained in:
Clément Renault 2019-11-22 14:10:05 +01:00 committed by GitHub
commit 840217b111
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 5 deletions

View File

@ -62,7 +62,7 @@ MeiliDB can serve multiple indexes, with different kinds of documents,
therefore, it is required to create the index before sending documents to it. therefore, it is required to create the index before sending documents to it.
```bash ```bash
curl -i -X POST 'http://127.0.0.1:8080/indexes/movies' curl -i -X POST 'http://127.0.0.1:8080/indexes' --data '{ "name": "Movies", "uid": "movies" }'
``` ```
Now that the server knows about our brand new index, we can send it data. Now that the server knows about our brand new index, we can send it data.

View File

@ -123,6 +123,7 @@ pub async fn get_index(ctx: Context<Data>) -> SResult<Response> {
#[serde(rename_all = "camelCase", deny_unknown_fields)] #[serde(rename_all = "camelCase", deny_unknown_fields)]
struct IndexCreateRequest { struct IndexCreateRequest {
name: String, name: String,
uid: Option<String>,
schema: Option<SchemaBody>, schema: Option<SchemaBody>,
} }
@ -146,11 +147,19 @@ pub async fn create_index(mut ctx: Context<Data>) -> SResult<Response> {
.await .await
.map_err(ResponseError::bad_request)?; .map_err(ResponseError::bad_request)?;
let generated_uid = generate_uid();
let db = &ctx.state().db; let db = &ctx.state().db;
let created_index = match db.create_index(&generated_uid) { let uid = match body.uid {
Some(uid) => uid,
None => loop {
let uid = generate_uid();
if db.open_index(&uid).is_none() {
break uid;
}
},
};
let created_index = match db.create_index(&uid) {
Ok(index) => index, Ok(index) => index,
Err(e) => return Err(ResponseError::create_index(e)), Err(e) => return Err(ResponseError::create_index(e)),
}; };
@ -184,7 +193,7 @@ pub async fn create_index(mut ctx: Context<Data>) -> SResult<Response> {
let response_body = IndexCreateResponse { let response_body = IndexCreateResponse {
name: body.name, name: body.name,
uid: generated_uid, uid,
schema: body.schema, schema: body.schema,
update_id: response_update_id, update_id: response_update_id,
created_at: Utc::now(), created_at: Utc::now(),