2020-04-10 19:05:05 +02:00
|
|
|
use actix_web as aweb;
|
|
|
|
use actix_web::{delete, get, post, web, HttpResponse};
|
2020-02-26 18:24:19 +01:00
|
|
|
use meilisearch_core::settings::{Settings, SettingsUpdate, UpdateState, DEFAULT_RANKING_RULES};
|
2020-01-23 11:30:18 +01:00
|
|
|
use std::collections::{BTreeMap, BTreeSet, HashSet};
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-04-10 19:05:05 +02:00
|
|
|
use crate::error::ResponseError;
|
|
|
|
use crate::routes::{IndexParam, IndexUpdateResponse};
|
2019-10-31 15:00:36 +01:00
|
|
|
use crate::Data;
|
2020-04-09 11:11:48 +02:00
|
|
|
|
2020-04-10 10:13:08 +02:00
|
|
|
#[post("/indexes/{index_uid}/settings")]
|
|
|
|
pub async fn update_all(
|
2020-04-09 11:11:48 +02:00
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
2020-04-16 11:09:47 +02:00
|
|
|
body: web::Json<Settings>,
|
|
|
|
) -> aweb::Result<HttpResponse> {
|
|
|
|
let index = data
|
|
|
|
.db
|
|
|
|
.open_index(&path.index_uid)
|
|
|
|
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
|
|
|
|
|
|
|
|
let mut writer = data
|
|
|
|
.db
|
|
|
|
.update_write_txn()
|
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
|
|
|
let settings = body
|
|
|
|
.into_inner()
|
|
|
|
.into_update()
|
|
|
|
.map_err(|e| ResponseError::BadRequest(e.to_string()))?;
|
|
|
|
let update_id = index
|
|
|
|
.settings_update(&mut writer, settings)
|
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
|
|
|
writer
|
|
|
|
.commit()
|
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
|
|
|
|
|
|
|
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/indexes/{index_uid}/settings")]
|
|
|
|
pub async fn get_all(
|
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
2020-04-09 11:11:48 +02:00
|
|
|
) -> aweb::Result<HttpResponse> {
|
2020-04-10 19:05:05 +02:00
|
|
|
let index = data
|
|
|
|
.db
|
|
|
|
.open_index(&path.index_uid)
|
2020-04-09 11:11:48 +02:00
|
|
|
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
|
|
|
|
|
2020-04-10 19:05:05 +02:00
|
|
|
let reader = data
|
|
|
|
.db
|
|
|
|
.main_read_txn()
|
2020-04-09 11:11:48 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
|
|
|
|
2020-04-10 19:05:05 +02:00
|
|
|
let stop_words_fst = index
|
|
|
|
.main
|
|
|
|
.stop_words_fst(&reader)
|
2020-04-09 11:11:48 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
2020-04-10 19:05:05 +02:00
|
|
|
let stop_words = stop_words_fst
|
|
|
|
.unwrap_or_default()
|
|
|
|
.stream()
|
|
|
|
.into_strs()
|
2020-04-09 11:11:48 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
2020-01-16 19:19:44 +01:00
|
|
|
let stop_words: BTreeSet<String> = stop_words.into_iter().collect();
|
|
|
|
|
2020-04-10 19:05:05 +02:00
|
|
|
let synonyms_fst = index
|
|
|
|
.main
|
|
|
|
.synonyms_fst(&reader)
|
2020-04-09 11:11:48 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?
|
|
|
|
.unwrap_or_default();
|
2020-04-10 19:05:05 +02:00
|
|
|
let synonyms_list = synonyms_fst
|
|
|
|
.stream()
|
|
|
|
.into_strs()
|
2020-04-09 11:11:48 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
2020-01-16 19:19:44 +01:00
|
|
|
|
|
|
|
let mut synonyms = BTreeMap::new();
|
|
|
|
let index_synonyms = &index.synonyms;
|
|
|
|
for synonym in synonyms_list {
|
2020-04-10 19:05:05 +02:00
|
|
|
let alternative_list = index_synonyms
|
|
|
|
.synonyms(&reader, synonym.as_bytes())
|
2020-04-09 11:11:48 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
2020-01-16 19:19:44 +01:00
|
|
|
if let Some(list) = alternative_list {
|
2020-04-10 19:05:05 +02:00
|
|
|
let list = list
|
|
|
|
.stream()
|
|
|
|
.into_strs()
|
2020-04-09 11:11:48 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
2020-01-16 19:19:44 +01:00
|
|
|
synonyms.insert(synonym, list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 18:49:17 +01:00
|
|
|
let ranking_rules = index
|
|
|
|
.main
|
2020-04-09 11:11:48 +02:00
|
|
|
.ranking_rules(&reader)
|
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?
|
2020-02-26 18:24:19 +01:00
|
|
|
.unwrap_or(DEFAULT_RANKING_RULES.to_vec())
|
|
|
|
.into_iter()
|
|
|
|
.map(|r| r.to_string())
|
|
|
|
.collect();
|
|
|
|
|
2020-04-10 19:05:05 +02:00
|
|
|
let distinct_attribute = index
|
|
|
|
.main
|
|
|
|
.distinct_attribute(&reader)
|
2020-04-09 11:11:48 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
2020-01-16 19:19:44 +01:00
|
|
|
|
2020-04-10 19:05:05 +02:00
|
|
|
let schema = index
|
|
|
|
.main
|
|
|
|
.schema(&reader)
|
2020-04-09 11:11:48 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
2020-01-16 19:19:44 +01:00
|
|
|
|
2020-02-12 17:00:14 +01:00
|
|
|
let searchable_attributes = schema.clone().map(|s| {
|
2020-03-10 15:35:19 +01:00
|
|
|
s.indexed_name()
|
2020-02-12 17:00:14 +01:00
|
|
|
.iter()
|
|
|
|
.map(|s| (*s).to_string())
|
2020-03-10 15:35:19 +01:00
|
|
|
.collect::<Vec<String>>()
|
2020-02-12 17:00:14 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
let displayed_attributes = schema.clone().map(|s| {
|
2020-03-10 15:35:19 +01:00
|
|
|
s.displayed_name()
|
2020-02-12 17:00:14 +01:00
|
|
|
.iter()
|
|
|
|
.map(|s| (*s).to_string())
|
2020-03-10 15:35:19 +01:00
|
|
|
.collect::<HashSet<String>>()
|
2020-02-12 17:00:14 +01:00
|
|
|
});
|
2020-03-10 15:35:19 +01:00
|
|
|
|
2020-02-25 15:51:37 +01:00
|
|
|
let accept_new_fields = schema.map(|s| s.accept_new_fields());
|
2020-01-16 19:19:44 +01:00
|
|
|
|
|
|
|
let settings = Settings {
|
2020-02-26 18:24:19 +01:00
|
|
|
ranking_rules: Some(Some(ranking_rules)),
|
2020-02-25 16:10:34 +01:00
|
|
|
distinct_attribute: Some(distinct_attribute),
|
2020-03-10 15:35:19 +01:00
|
|
|
searchable_attributes: Some(searchable_attributes),
|
|
|
|
displayed_attributes: Some(displayed_attributes),
|
2020-03-05 14:17:15 +01:00
|
|
|
stop_words: Some(Some(stop_words)),
|
|
|
|
synonyms: Some(Some(synonyms)),
|
2020-02-25 15:51:37 +01:00
|
|
|
accept_new_fields: Some(accept_new_fields),
|
2020-01-16 19:19:44 +01:00
|
|
|
};
|
|
|
|
|
2020-04-09 11:11:48 +02:00
|
|
|
Ok(HttpResponse::Ok().json(settings))
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|
2020-01-27 18:33:40 +01:00
|
|
|
|
2020-04-10 10:13:08 +02:00
|
|
|
#[delete("/indexes/{index_uid}/settings")]
|
|
|
|
pub async fn delete_all(
|
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
|
|
|
) -> aweb::Result<HttpResponse> {
|
2020-04-10 19:05:05 +02:00
|
|
|
let index = data
|
|
|
|
.db
|
|
|
|
.open_index(&path.index_uid)
|
2020-04-10 10:13:08 +02:00
|
|
|
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
|
2020-04-10 19:05:05 +02:00
|
|
|
let mut writer = data
|
|
|
|
.db
|
|
|
|
.update_write_txn()
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
|
|
|
|
|
|
|
let settings = SettingsUpdate {
|
|
|
|
ranking_rules: UpdateState::Clear,
|
|
|
|
distinct_attribute: UpdateState::Clear,
|
|
|
|
primary_key: UpdateState::Clear,
|
|
|
|
searchable_attributes: UpdateState::Clear,
|
|
|
|
displayed_attributes: UpdateState::Clear,
|
|
|
|
stop_words: UpdateState::Clear,
|
|
|
|
synonyms: UpdateState::Clear,
|
|
|
|
accept_new_fields: UpdateState::Clear,
|
|
|
|
};
|
|
|
|
|
2020-04-10 19:05:05 +02:00
|
|
|
let update_id = index
|
|
|
|
.settings_update(&mut writer, settings)
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
2020-04-10 19:05:05 +02:00
|
|
|
writer
|
|
|
|
.commit()
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
|
|
|
|
|
|
|
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/indexes/{index_uid}/settings/ranking-rules")]
|
|
|
|
pub async fn get_rules(
|
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
|
|
|
) -> aweb::Result<HttpResponse> {
|
2020-04-10 19:05:05 +02:00
|
|
|
let index = data
|
|
|
|
.db
|
|
|
|
.open_index(&path.index_uid)
|
2020-04-10 10:13:08 +02:00
|
|
|
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
|
2020-04-10 19:05:05 +02:00
|
|
|
let reader = data
|
|
|
|
.db
|
|
|
|
.main_read_txn()
|
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
2020-04-10 10:13:08 +02:00
|
|
|
|
|
|
|
let ranking_rules = index
|
|
|
|
.main
|
|
|
|
.ranking_rules(&reader)
|
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?
|
|
|
|
.unwrap_or(DEFAULT_RANKING_RULES.to_vec())
|
|
|
|
.into_iter()
|
|
|
|
.map(|r| r.to_string())
|
|
|
|
.collect::<Vec<String>>();
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(ranking_rules))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/indexes/{index_uid}/settings/ranking-rules")]
|
|
|
|
pub async fn update_rules(
|
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
|
|
|
body: web::Json<Option<Vec<String>>>,
|
|
|
|
) -> aweb::Result<HttpResponse> {
|
2020-04-10 19:05:05 +02:00
|
|
|
let index = data
|
|
|
|
.db
|
|
|
|
.open_index(&path.index_uid)
|
2020-04-10 10:13:08 +02:00
|
|
|
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
|
|
|
|
|
|
|
|
let settings = Settings {
|
|
|
|
ranking_rules: Some(body.into_inner()),
|
|
|
|
..Settings::default()
|
|
|
|
};
|
|
|
|
|
2020-04-10 19:05:05 +02:00
|
|
|
let mut writer = data
|
|
|
|
.db
|
|
|
|
.update_write_txn()
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
2020-04-10 19:05:05 +02:00
|
|
|
let settings = settings
|
|
|
|
.into_update()
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|e| ResponseError::BadRequest(e.to_string()))?;
|
2020-04-10 19:05:05 +02:00
|
|
|
let update_id = index
|
|
|
|
.settings_update(&mut writer, settings)
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
2020-04-10 19:05:05 +02:00
|
|
|
writer
|
|
|
|
.commit()
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
|
|
|
|
|
|
|
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[delete("/indexes/{index_uid}/settings/ranking-rules")]
|
|
|
|
pub async fn delete_rules(
|
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
|
|
|
) -> aweb::Result<HttpResponse> {
|
2020-04-10 19:05:05 +02:00
|
|
|
let index = data
|
|
|
|
.db
|
|
|
|
.open_index(&path.index_uid)
|
2020-04-10 10:13:08 +02:00
|
|
|
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
|
2020-04-10 19:05:05 +02:00
|
|
|
let mut writer = data
|
|
|
|
.db
|
|
|
|
.update_write_txn()
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
|
|
|
|
|
|
|
let settings = SettingsUpdate {
|
|
|
|
ranking_rules: UpdateState::Clear,
|
|
|
|
..SettingsUpdate::default()
|
|
|
|
};
|
|
|
|
|
2020-04-10 19:05:05 +02:00
|
|
|
let update_id = index
|
|
|
|
.settings_update(&mut writer, settings)
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
|
|
|
|
2020-04-10 19:05:05 +02:00
|
|
|
writer
|
|
|
|
.commit()
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
|
|
|
|
|
|
|
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/indexes/{index_uid}/settings/distinct-attribute")]
|
|
|
|
pub async fn get_distinct(
|
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
|
|
|
) -> aweb::Result<HttpResponse> {
|
2020-04-10 19:05:05 +02:00
|
|
|
let index = data
|
|
|
|
.db
|
|
|
|
.open_index(&path.index_uid)
|
2020-04-10 10:13:08 +02:00
|
|
|
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
|
2020-04-10 19:05:05 +02:00
|
|
|
let reader = data
|
|
|
|
.db
|
|
|
|
.main_read_txn()
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
2020-04-10 19:05:05 +02:00
|
|
|
let distinct_attribute = index
|
|
|
|
.main
|
|
|
|
.distinct_attribute(&reader)
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(distinct_attribute))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/indexes/{index_uid}/settings/distinct-attribute")]
|
|
|
|
pub async fn update_distinct(
|
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
|
|
|
body: web::Json<Option<String>>,
|
|
|
|
) -> aweb::Result<HttpResponse> {
|
2020-04-10 19:05:05 +02:00
|
|
|
let index = data
|
|
|
|
.db
|
|
|
|
.open_index(&path.index_uid)
|
2020-04-10 10:13:08 +02:00
|
|
|
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
|
|
|
|
|
|
|
|
let settings = Settings {
|
|
|
|
distinct_attribute: Some(body.into_inner()),
|
|
|
|
..Settings::default()
|
|
|
|
};
|
|
|
|
|
2020-04-10 19:05:05 +02:00
|
|
|
let mut writer = data
|
|
|
|
.db
|
|
|
|
.update_write_txn()
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
2020-04-10 19:05:05 +02:00
|
|
|
let settings = settings
|
|
|
|
.into_update()
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|e| ResponseError::BadRequest(e.to_string()))?;
|
2020-04-10 19:05:05 +02:00
|
|
|
let update_id = index
|
|
|
|
.settings_update(&mut writer, settings)
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
2020-04-10 19:05:05 +02:00
|
|
|
writer
|
|
|
|
.commit()
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
|
|
|
|
|
|
|
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[delete("/indexes/{index_uid}/settings/distinct-attribute")]
|
|
|
|
pub async fn delete_distinct(
|
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
|
|
|
) -> aweb::Result<HttpResponse> {
|
2020-04-10 19:05:05 +02:00
|
|
|
let index = data
|
|
|
|
.db
|
|
|
|
.open_index(&path.index_uid)
|
2020-04-10 10:13:08 +02:00
|
|
|
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
|
2020-04-10 19:05:05 +02:00
|
|
|
let mut writer = data
|
|
|
|
.db
|
|
|
|
.update_write_txn()
|
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
2020-04-10 10:13:08 +02:00
|
|
|
|
|
|
|
let settings = SettingsUpdate {
|
|
|
|
distinct_attribute: UpdateState::Clear,
|
|
|
|
..SettingsUpdate::default()
|
|
|
|
};
|
|
|
|
|
2020-04-10 19:05:05 +02:00
|
|
|
let update_id = index
|
|
|
|
.settings_update(&mut writer, settings)
|
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
2020-04-10 10:13:08 +02:00
|
|
|
|
2020-04-10 19:05:05 +02:00
|
|
|
writer
|
|
|
|
.commit()
|
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
2020-04-10 10:13:08 +02:00
|
|
|
|
|
|
|
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/indexes/{index_uid}/settings/searchable-attributes")]
|
|
|
|
pub async fn get_searchable(
|
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
|
|
|
) -> aweb::Result<HttpResponse> {
|
2020-04-10 19:05:05 +02:00
|
|
|
let index = data
|
|
|
|
.db
|
|
|
|
.open_index(&path.index_uid)
|
2020-04-10 10:13:08 +02:00
|
|
|
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
|
2020-04-10 19:05:05 +02:00
|
|
|
let reader = data
|
|
|
|
.db
|
|
|
|
.main_read_txn()
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
2020-04-10 19:05:05 +02:00
|
|
|
let schema = index
|
|
|
|
.main
|
|
|
|
.schema(&reader)
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
|
|
|
let searchable_attributes: Option<Vec<String>> =
|
|
|
|
schema.map(|s| s.indexed_name().iter().map(|i| (*i).to_string()).collect());
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(searchable_attributes))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/indexes/{index_uid}/settings/searchable-attributes")]
|
|
|
|
pub async fn update_searchable(
|
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
|
|
|
body: web::Json<Option<Vec<String>>>,
|
|
|
|
) -> aweb::Result<HttpResponse> {
|
2020-04-10 19:05:05 +02:00
|
|
|
let index = data
|
|
|
|
.db
|
|
|
|
.open_index(&path.index_uid)
|
2020-04-10 10:13:08 +02:00
|
|
|
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
|
|
|
|
|
|
|
|
let settings = Settings {
|
|
|
|
searchable_attributes: Some(body.into_inner()),
|
|
|
|
..Settings::default()
|
|
|
|
};
|
|
|
|
|
2020-04-10 19:05:05 +02:00
|
|
|
let mut writer = data
|
|
|
|
.db
|
|
|
|
.update_write_txn()
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
2020-04-10 19:05:05 +02:00
|
|
|
let settings = settings
|
|
|
|
.into_update()
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|e| ResponseError::BadRequest(e.to_string()))?;
|
2020-04-10 19:05:05 +02:00
|
|
|
let update_id = index
|
|
|
|
.settings_update(&mut writer, settings)
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
2020-04-10 19:05:05 +02:00
|
|
|
writer
|
|
|
|
.commit()
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
|
|
|
|
|
|
|
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[delete("/indexes/{index_uid}/settings/searchable-attributes")]
|
|
|
|
pub async fn delete_searchable(
|
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
|
|
|
) -> aweb::Result<HttpResponse> {
|
2020-04-10 19:05:05 +02:00
|
|
|
let index = data
|
|
|
|
.db
|
|
|
|
.open_index(&path.index_uid)
|
2020-04-10 10:13:08 +02:00
|
|
|
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
|
|
|
|
|
|
|
|
let settings = SettingsUpdate {
|
|
|
|
searchable_attributes: UpdateState::Clear,
|
|
|
|
..SettingsUpdate::default()
|
|
|
|
};
|
|
|
|
|
2020-04-10 19:05:05 +02:00
|
|
|
let mut writer = data
|
|
|
|
.db
|
|
|
|
.update_write_txn()
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
2020-04-10 19:05:05 +02:00
|
|
|
let update_id = index
|
|
|
|
.settings_update(&mut writer, settings)
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
2020-04-10 19:05:05 +02:00
|
|
|
writer
|
|
|
|
.commit()
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
|
|
|
|
|
|
|
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/indexes/{index_uid}/settings/displayed-attributes")]
|
|
|
|
pub async fn get_displayed(
|
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
|
|
|
) -> aweb::Result<HttpResponse> {
|
2020-04-10 19:05:05 +02:00
|
|
|
let index = data
|
|
|
|
.db
|
|
|
|
.open_index(&path.index_uid)
|
2020-04-10 10:13:08 +02:00
|
|
|
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
|
2020-04-10 19:05:05 +02:00
|
|
|
let reader = data
|
|
|
|
.db
|
|
|
|
.main_read_txn()
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
|
|
|
|
2020-04-10 19:05:05 +02:00
|
|
|
let schema = index
|
|
|
|
.main
|
|
|
|
.schema(&reader)
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
|
|
|
|
|
|
|
let displayed_attributes: Option<HashSet<String>> = schema.map(|s| {
|
|
|
|
s.displayed_name()
|
|
|
|
.iter()
|
|
|
|
.map(|i| (*i).to_string())
|
|
|
|
.collect()
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(displayed_attributes))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/indexes/{index_uid}/settings/displayed-attributes")]
|
|
|
|
pub async fn update_displayed(
|
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
|
|
|
body: web::Json<Option<HashSet<String>>>,
|
|
|
|
) -> aweb::Result<HttpResponse> {
|
2020-04-10 19:05:05 +02:00
|
|
|
let index = data
|
|
|
|
.db
|
|
|
|
.open_index(&path.index_uid)
|
2020-04-10 10:13:08 +02:00
|
|
|
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
|
|
|
|
|
|
|
|
let settings = Settings {
|
|
|
|
displayed_attributes: Some(body.into_inner()),
|
|
|
|
..Settings::default()
|
|
|
|
};
|
|
|
|
|
2020-04-10 19:05:05 +02:00
|
|
|
let mut writer = data
|
|
|
|
.db
|
|
|
|
.update_write_txn()
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
2020-04-10 19:05:05 +02:00
|
|
|
let settings = settings
|
|
|
|
.into_update()
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|e| ResponseError::BadRequest(e.to_string()))?;
|
2020-04-10 19:05:05 +02:00
|
|
|
let update_id = index
|
|
|
|
.settings_update(&mut writer, settings)
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
2020-04-10 19:05:05 +02:00
|
|
|
writer
|
|
|
|
.commit()
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
|
|
|
|
|
|
|
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[delete("/indexes/{index_uid}/settings/displayed-attributes")]
|
|
|
|
pub async fn delete_displayed(
|
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
|
|
|
) -> aweb::Result<HttpResponse> {
|
2020-04-10 19:05:05 +02:00
|
|
|
let index = data
|
|
|
|
.db
|
|
|
|
.open_index(&path.index_uid)
|
2020-04-10 10:13:08 +02:00
|
|
|
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
|
|
|
|
|
|
|
|
let settings = SettingsUpdate {
|
|
|
|
displayed_attributes: UpdateState::Clear,
|
|
|
|
..SettingsUpdate::default()
|
|
|
|
};
|
|
|
|
|
2020-04-10 19:05:05 +02:00
|
|
|
let mut writer = data
|
|
|
|
.db
|
|
|
|
.update_write_txn()
|
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
|
|
|
let update_id = index
|
|
|
|
.settings_update(&mut writer, settings)
|
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
|
|
|
writer
|
|
|
|
.commit()
|
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
2020-04-10 10:13:08 +02:00
|
|
|
|
|
|
|
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/indexes/{index_uid}/settings/accept-new-fields")]
|
|
|
|
pub async fn get_accept_new_fields(
|
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
|
|
|
) -> aweb::Result<HttpResponse> {
|
2020-04-10 19:05:05 +02:00
|
|
|
let index = data
|
|
|
|
.db
|
|
|
|
.open_index(&path.index_uid)
|
2020-04-10 10:13:08 +02:00
|
|
|
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
|
2020-04-10 19:05:05 +02:00
|
|
|
let reader = data
|
|
|
|
.db
|
|
|
|
.main_read_txn()
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
|
|
|
|
2020-04-10 19:05:05 +02:00
|
|
|
let schema = index
|
|
|
|
.main
|
|
|
|
.schema(&reader)
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
|
|
|
|
|
|
|
let accept_new_fields = schema.map(|s| s.accept_new_fields());
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(accept_new_fields))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/indexes/{index_uid}/settings/accept-new-fields")]
|
|
|
|
pub async fn update_accept_new_fields(
|
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
|
|
|
body: web::Json<Option<bool>>,
|
|
|
|
) -> aweb::Result<HttpResponse> {
|
2020-04-10 19:05:05 +02:00
|
|
|
let index = data
|
|
|
|
.db
|
|
|
|
.open_index(&path.index_uid)
|
2020-04-10 10:13:08 +02:00
|
|
|
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
|
|
|
|
|
|
|
|
let settings = Settings {
|
|
|
|
accept_new_fields: Some(body.into_inner()),
|
|
|
|
..Settings::default()
|
|
|
|
};
|
|
|
|
|
2020-04-10 19:05:05 +02:00
|
|
|
let mut writer = data
|
|
|
|
.db
|
|
|
|
.update_write_txn()
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
2020-04-10 19:05:05 +02:00
|
|
|
let settings = settings
|
|
|
|
.into_update()
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|e| ResponseError::BadRequest(e.to_string()))?;
|
2020-04-10 19:05:05 +02:00
|
|
|
let update_id = index
|
|
|
|
.settings_update(&mut writer, settings)
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
2020-04-10 19:05:05 +02:00
|
|
|
writer
|
|
|
|
.commit()
|
2020-04-10 10:13:08 +02:00
|
|
|
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
|
|
|
|
|
|
|
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
|
|
|
|
}
|