move identifier from settings to index; fix #470

This commit is contained in:
qdequele 2020-02-12 17:00:14 +01:00
parent dc9ca2ebc9
commit 4986adc186
No known key found for this signature in database
GPG key ID: B3F0A000EBF11745
8 changed files with 193 additions and 69 deletions

View file

@ -1,7 +1,7 @@
use std::collections::{BTreeSet, HashSet};
use indexmap::IndexMap;
use meilisearch_core::settings::Settings;
use meilisearch_core::settings::{SettingsUpdate, UpdateState};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tide::{Request, Response};
@ -145,11 +145,11 @@ async fn update_multiple_documents(mut ctx: Request<Data>, is_partial: bool) ->
None => return Err(ResponseError::bad_request("Could not infer a schema")),
},
};
let settings = Settings {
identifier: Some(Some(id)),
..Settings::default()
let settings_update = SettingsUpdate{
identifier: UpdateState::Update(id),
..SettingsUpdate::default()
};
index.settings_update(&mut update_writer, settings.into_update()?)?;
index.settings_update(&mut update_writer, settings_update)?;
}
let mut document_addition = if is_partial {

View file

@ -40,11 +40,17 @@ pub async fn list_indexes(ctx: Request<Data>) -> SResult<Response> {
let created_at = index.main.created_at(&reader)?.into_internal_error()?;
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()),
_ => None
};
let index_response = IndexResponse {
name,
uid: index_uid,
created_at,
updated_at,
identifier,
};
response_body.push(index_response);
}
@ -65,6 +71,7 @@ struct IndexResponse {
uid: String,
created_at: DateTime<Utc>,
updated_at: DateTime<Utc>,
identifier: Option<String>,
}
pub async fn get_index(ctx: Request<Data>) -> SResult<Response> {
@ -80,11 +87,17 @@ pub async fn get_index(ctx: Request<Data>) -> SResult<Response> {
let created_at = index.main.created_at(&reader)?.into_internal_error()?;
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()),
_ => None
};
let response_body = IndexResponse {
name,
uid,
created_at,
updated_at,
identifier
};
Ok(tide::Response::new(200).body_json(&response_body)?)
@ -105,6 +118,7 @@ struct IndexCreateResponse {
uid: String,
created_at: DateTime<Utc>,
updated_at: DateTime<Utc>,
identifier: Option<String>,
}
pub async fn create_index(mut ctx: Request<Data>) -> SResult<Response> {
@ -150,7 +164,7 @@ pub async fn create_index(mut ctx: Request<Data>) -> SResult<Response> {
.updated_at(&writer)?
.into_internal_error()?;
if let Some(id) = body.identifier {
if let Some(id) = body.identifier.clone() {
created_index
.main
.put_schema(&mut writer, &Schema::with_identifier(&id))?;
@ -163,6 +177,7 @@ pub async fn create_index(mut ctx: Request<Data>) -> SResult<Response> {
uid,
created_at,
updated_at,
identifier: body.identifier,
};
Ok(tide::Response::new(201).body_json(&response_body)?)
@ -171,7 +186,8 @@ pub async fn create_index(mut ctx: Request<Data>) -> SResult<Response> {
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct UpdateIndexRequest {
name: String,
name: Option<String>,
identifier: Option<String>,
}
#[derive(Debug, Serialize)]
@ -181,6 +197,7 @@ struct UpdateIndexResponse {
uid: String,
created_at: DateTime<Utc>,
updated_at: DateTime<Utc>,
identifier: Option<String>,
}
pub async fn update_index(mut ctx: Request<Data>) -> SResult<Response> {
@ -197,21 +214,36 @@ pub async fn update_index(mut ctx: Request<Data>) -> SResult<Response> {
let db = &ctx.state().db;
let mut writer = db.main_write_txn()?;
index.main.put_name(&mut writer, &body.name)?;
if let Some(name) = body.name {
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"));
}
index.main.put_schema(&mut writer, &Schema::with_identifier(&identifier))?;
}
index.main.put_updated_at(&mut writer)?;
writer.commit()?;
let reader = db.main_read_txn()?;
let reader = db.main_read_txn()?;
let name = index.main.name(&reader)?.into_internal_error()?;
let created_at = index.main.created_at(&reader)?.into_internal_error()?;
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()),
_ => None
};
let response_body = UpdateIndexResponse {
name: body.name,
name,
uid: index_uid,
created_at,
updated_at,
identifier
};
Ok(tide::Response::new(200).body_json(&response_body)?)

View file

@ -54,21 +54,36 @@ pub async fn get_all(ctx: Request<Data>) -> SResult<Response> {
let schema = index.main.schema(&reader)?;
let identifier = schema.clone().map(|s| s.identifier().to_owned());
let searchable_attributes = schema
.clone()
.map(|s| s.indexed_name().iter().map(|s| (*s).to_string()).collect());
let displayed_attributes = schema
.clone()
.map(|s| s.displayed_name().iter().map(|s| (*s).to_string()).collect());
let searchable_attributes = schema.clone().map(|s| {
let attrs = s.indexed_name()
.iter()
.map(|s| (*s).to_string())
.collect::<Vec<String>>();
if attrs.is_empty() {
None
} else {
Some(attrs)
}
});
let displayed_attributes = schema.clone().map(|s| {
let attrs = s.displayed_name()
.iter()
.map(|s| (*s).to_string())
.collect::<HashSet<String>>();
if attrs.is_empty() {
None
} else {
Some(attrs)
}
});
let index_new_fields = schema.map(|s| s.index_new_fields());
let settings = Settings {
ranking_rules: Some(ranking_rules),
ranking_distinct: Some(ranking_distinct),
identifier: Some(identifier),
searchable_attributes: Some(searchable_attributes),
displayed_attributes: Some(displayed_attributes),
searchable_attributes: searchable_attributes,
displayed_attributes: displayed_attributes,
stop_words: Some(stop_words),
synonyms: Some(synonyms),
index_new_fields: Some(index_new_fields),
@ -100,7 +115,6 @@ pub async fn update_all(mut ctx: Request<Data>) -> SResult<Response> {
let settings = Settings {
ranking_rules: Some(settings_update.ranking_rules),
ranking_distinct: Some(settings_update.ranking_distinct),
identifier: Some(settings_update.identifier),
searchable_attributes: Some(settings_update.searchable_attributes),
displayed_attributes: Some(settings_update.displayed_attributes),
stop_words: Some(settings_update.stop_words),
@ -321,8 +335,12 @@ pub async fn displayed(ctx: Request<Data>) -> SResult<Response> {
let schema = index.main.schema(&reader)?;
let displayed_attributes: Option<HashSet<String>> =
schema.map(|s| s.displayed_name().iter().map(|i| (*i).to_string()).collect());
let displayed_attributes: Option<HashSet<String>> = schema.map(|s| {
s.displayed_name()
.iter()
.map(|i| (*i).to_string())
.collect()
});
Ok(tide::Response::new(200)
.body_json(&displayed_attributes)