This commit is contained in:
mpostma 2021-01-05 00:21:42 +01:00
parent b4d447b5cb
commit b07e21ab3c
8 changed files with 427 additions and 0 deletions

View File

@ -0,0 +1,43 @@
use actix_web::{web, HttpResponse, get};
use crate::error::{Error, ResponseError};
use crate::helpers::Authentication;
use crate::make_update_delete_routes;
use crate::Data;
#[get(
"/indexes/{index_uid}/settings/attributes-for-faceting",
wrap = "Authentication::Private"
)]
async fn get(
data: web::Data<Data>,
index_uid: web::Path<String>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.load()
.open_index(&index_uid.as_ref())
.ok_or(Error::index_not_found(&index_uid.as_ref()))?;
let attributes_for_faceting = data.db.load().main_read::<_, _, ResponseError>(|reader| {
let schema = index.main.schema(reader)?;
let attrs = index.main.attributes_for_faceting(reader)?;
let attr_names = match (&schema, &attrs) {
(Some(schema), Some(attrs)) => attrs
.iter()
.filter_map(|&id| schema.name(id))
.map(str::to_string)
.collect(),
_ => vec![],
};
Ok(attr_names)
})?;
Ok(HttpResponse::Ok().json(attributes_for_faceting))
}
make_update_delete_routes!(
"/indexes/{index_uid}/settings/attributes-for-faceting",
Vec<String>,
attributes_for_faceting
);

View File

@ -0,0 +1,25 @@
use std::collections::HashSet;
use actix_web::{web, HttpResponse, get};
use crate::error::{Error, ResponseError};
use crate::helpers::Authentication;
use crate::make_update_delete_routes;
use crate::Data;
#[get(
"/indexes/{index_uid}/settings/displayed-attributes",
wrap = "Authentication::Private"
)]
async fn get(
data: web::Data<Data>,
index_uid: web::Path<String>,
) -> Result<HttpResponse, ResponseError> {
todo!()
}
make_update_delete_routes!(
"/indexes/{index_uid}/settings/displayed-attributes",
HashSet<String>,
displayed_attributes
);

View File

@ -0,0 +1,36 @@
use crate::make_update_delete_routes;
use actix_web::{web, HttpResponse, get};
use crate::error::{Error, ResponseError};
use crate::helpers::Authentication;
use crate::Data;
#[get(
"/indexes/{index_uid}/settings/distinct-attribute",
wrap = "Authentication::Private"
)]
async fn get(
data: web::Data<Data>,
index_uid: web::Path<String>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.load()
.open_index(&index_uid.as_ref())
.ok_or(Error::index_not_found(&index_uid.as_ref()))?;
let reader = data.db.load().main_read_txn()?;
let distinct_attribute_id = index.main.distinct_attribute(&reader)?;
let schema = index.main.schema(&reader)?;
let distinct_attribute = match (schema, distinct_attribute_id) {
(Some(schema), Some(id)) => schema.name(id).map(str::to_string),
_ => None,
};
Ok(HttpResponse::Ok().json(distinct_attribute))
}
make_update_delete_routes!(
"/indexes/{index_uid}/settings/distinct-attribute",
String,
distinct_attribute
);

190
src/routes/settings/mod.rs Normal file
View File

@ -0,0 +1,190 @@
use actix_web::{web, HttpResponse, delete, get, post};
use log::error;
use crate::Data;
use crate::error::ResponseError;
use crate::updates::Settings;
use crate::helpers::Authentication;
#[macro_export]
macro_rules! make_setting_route {
($route:literal, $type:ty, $attr:ident) => {
mod $attr {
use actix_web::{web, HttpResponse};
use crate::data;
use crate::error::ResponseError;
use crate::helpers::Authentication;
use crate::updates::Settings;
#[actix_web::delete($route, wrap = "Authentication::Private")]
pub async fn delete(
data: web::Data<data::Data>,
index_uid: web::Path<String>,
) -> Result<HttpResponse, ResponseError> {
use crate::updates::Settings;
let settings = Settings {
$attr: Some(None),
..Default::default()
};
match data.update_settings(index_uid.as_ref(), settings).await {
Ok(update_status) => {
let json = serde_json::to_string(&update_status).unwrap();
Ok(HttpResponse::Ok().body(json))
}
Err(e) => {
log::error!("{}", e);
unimplemented!();
}
}
}
#[actix_web::post($route, wrap = "Authentication::Private")]
pub async fn update(
data: actix_web::web::Data<data::Data>,
index_uid: actix_web::web::Path<String>,
body: actix_web::web::Json<Option<$type>>,
) -> std::result::Result<HttpResponse, ResponseError> {
let settings = Settings {
$attr: Some(body.into_inner()),
..Default::default()
};
match data.update_settings(index_uid.as_ref(), settings).await {
Ok(update_status) => {
let json = serde_json::to_string(&update_status).unwrap();
Ok(HttpResponse::Ok().body(json))
}
Err(e) => {
log::error!("{}", e);
unimplemented!();
}
}
}
#[actix_web::get($route, wrap = "Authentication::Private")]
pub async fn get(
data: actix_web::web::Data<data::Data>,
index_uid: actix_web::web::Path<String>,
) -> std::result::Result<HttpResponse, ResponseError> {
match data.settings(index_uid.as_ref()) {
Ok(settings) => {
let setting = settings.$attr;
let json = serde_json::to_string(&setting).unwrap();
Ok(HttpResponse::Ok().body(json))
}
Err(e) => {
log::error!("{}", e);
unimplemented!();
}
}
}
}
};
}
make_setting_route!(
"/indexes/{index_uid}/settings/attributes-for-faceting",
std::collections::HashMap<String, String>,
faceted_attributes
);
make_setting_route!(
"/indexes/{index_uid}/settings/displayed-attributes",
Vec<String>,
displayed_attributes
);
make_setting_route!(
"/indexes/{index_uid}/settings/searchable-attributes",
Vec<String>,
searchable_attributes
);
//make_setting_route!(
//"/indexes/{index_uid}/settings/distinct-attribute",
//String,
//distinct_attribute
//);
//make_setting_route!(
//"/indexes/{index_uid}/settings/ranking-rules",
//Vec<String>,
//ranking_rules
//);
macro_rules! create_services {
($($mod:ident),*) => {
pub fn services(cfg: &mut web::ServiceConfig) {
cfg
.service(update_all)
.service(get_all)
.service(delete_all)
$(
.service($mod::get)
.service($mod::update)
.service($mod::delete)
)*;
}
};
}
create_services!(
faceted_attributes,
displayed_attributes,
searchable_attributes
);
#[post("/indexes/{index_uid}/settings", wrap = "Authentication::Private")]
async fn update_all(
data: web::Data<Data>,
index_uid: web::Path<String>,
body: web::Json<Settings>,
) -> Result<HttpResponse, ResponseError> {
match data.update_settings(index_uid.as_ref(), body.into_inner()).await {
Ok(update_result) => {
let json = serde_json::to_string(&update_result).unwrap();
Ok(HttpResponse::Ok().body(json))
}
Err(e) => {
error!("{}", e);
unimplemented!();
}
}
}
#[get("/indexes/{index_uid}/settings", wrap = "Authentication::Private")]
async fn get_all(
data: web::Data<Data>,
index_uid: web::Path<String>,
) -> Result<HttpResponse, ResponseError> {
match data.settings(index_uid.as_ref()) {
Ok(settings) => {
let json = serde_json::to_string(&settings).unwrap();
Ok(HttpResponse::Ok().body(json))
}
Err(e) => {
error!("{}", e);
unimplemented!();
}
}
}
#[delete("/indexes/{index_uid}/settings", wrap = "Authentication::Private")]
async fn delete_all(
data: web::Data<Data>,
index_uid: web::Path<String>,
) -> Result<HttpResponse, ResponseError> {
let settings = Settings::cleared();
match data.update_settings(index_uid.as_ref(), settings).await {
Ok(update_result) => {
let json = serde_json::to_string(&update_result).unwrap();
Ok(HttpResponse::Ok().body(json))
}
Err(e) => {
error!("{}", e);
unimplemented!();
}
}
}

View File

@ -0,0 +1,23 @@
use crate::make_update_delete_routes;
use actix_web::{web, HttpResponse, get};
use crate::error::{Error, ResponseError};
use crate::helpers::Authentication;
use crate::Data;
#[get(
"/indexes/{index_uid}/settings/ranking-rules",
wrap = "Authentication::Private"
)]
async fn get(
data: web::Data<Data>,
index_uid: web::Path<String>,
) -> Result<HttpResponse, ResponseError> {
todo!()
}
make_update_delete_routes!(
"/indexes/{index_uid}/settings/ranking-rules",
Vec<String>,
ranking_rules
);

View File

@ -0,0 +1,34 @@
use actix_web::{web, HttpResponse, get};
use crate::data::get_indexed_attributes;
use crate::error::{Error, ResponseError};
use crate::helpers::Authentication;
use crate::make_update_delete_routes;
use crate::Data;
#[get(
"/indexes/{index_uid}/settings/searchable-attributes",
wrap = "Authentication::Private"
)]
async fn get(
data: web::Data<Data>,
index_uid: web::Path<String>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.load()
.open_index(&index_uid.as_ref())
.ok_or(Error::index_not_found(&index_uid.as_ref()))?;
let reader = data.db.load().main_read_txn()?;
let schema = index.main.schema(&reader)?;
let searchable_attributes: Option<Vec<String>> = schema.as_ref().map(get_indexed_attributes);
Ok(HttpResponse::Ok().json(searchable_attributes))
}
make_update_delete_routes!(
"/indexes/{index_uid}/settings/searchable-attributes",
Vec<String>,
searchable_attributes
);

View File

@ -0,0 +1,33 @@
use std::collections::BTreeSet;
use crate::make_update_delete_routes;
use actix_web::{web, HttpResponse, get};
use crate::error::{Error, ResponseError};
use crate::helpers::Authentication;
use crate::Data;
#[get(
"/indexes/{index_uid}/settings/stop-words",
wrap = "Authentication::Private"
)]
async fn get(
data: web::Data<Data>,
index_uid: web::Path<String>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.load()
.open_index(&index_uid.as_ref())
.ok_or(Error::index_not_found(&index_uid.as_ref()))?;
let reader = data.db.load().main_read_txn()?;
let stop_words = index.main.stop_words(&reader)?;
Ok(HttpResponse::Ok().json(stop_words))
}
make_update_delete_routes!(
"/indexes/{index_uid}/settings/stop-words",
BTreeSet<String>,
stop_words
);

View File

@ -0,0 +1,43 @@
use std::collections::BTreeMap;
use actix_web::{web, HttpResponse, get};
use indexmap::IndexMap;
use crate::error::{Error, ResponseError};
use crate::helpers::Authentication;
use crate::make_update_delete_routes;
use crate::Data;
#[get(
"/indexes/{index_uid}/settings/synonyms",
wrap = "Authentication::Private"
)]
async fn get(
data: web::Data<Data>,
index_uid: web::Path<String>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.load()
.open_index(&index_uid.as_ref())
.ok_or(Error::index_not_found(&index_uid.as_ref()))?;
let reader = data.db.load().main_read_txn()?;
let synonyms_list = index.main.synonyms(&reader)?;
let mut synonyms = IndexMap::new();
let index_synonyms = &index.synonyms;
for synonym in synonyms_list {
let list = index_synonyms.synonyms(&reader, synonym.as_bytes())?;
synonyms.insert(synonym, list);
}
Ok(HttpResponse::Ok().json(synonyms))
}
make_update_delete_routes!(
"/indexes/{index_uid}/settings/synonyms",
BTreeMap<String, Vec<String>>,
synonyms
);