mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-11-22 12:54:26 +01:00
change param tuples by struct
add settings endpoint; wip
This commit is contained in:
parent
5ec130e6dc
commit
b08f6737ac
@ -7,25 +7,31 @@ use actix_web as aweb;
|
||||
|
||||
use crate::error::ResponseError;
|
||||
use crate::Data;
|
||||
use crate::routes::IndexUpdateResponse;
|
||||
use crate::routes::{IndexUpdateResponse, IndexParam};
|
||||
|
||||
type Document = IndexMap<String, Value>;
|
||||
|
||||
#[derive(Default, Deserialize)]
|
||||
pub struct DocumentParam {
|
||||
index_uid: String,
|
||||
document_id: String
|
||||
}
|
||||
|
||||
#[get("/indexes/{index_uid}/documents/{document_id}")]
|
||||
pub async fn get_document(
|
||||
data: web::Data<Data>,
|
||||
path: web::Path<(String, String)>,
|
||||
path: web::Path<DocumentParam>,
|
||||
) -> aweb::Result<web::Json<Document>> {
|
||||
let index = data.db.open_index(&path.0)
|
||||
.ok_or(ResponseError::IndexNotFound(path.0.clone()))?;
|
||||
let document_id = meilisearch_core::serde::compute_document_id(path.1.clone());
|
||||
let index = data.db.open_index(&path.index_uid)
|
||||
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
|
||||
let document_id = meilisearch_core::serde::compute_document_id(path.document_id.clone());
|
||||
|
||||
let reader = data.db.main_read_txn()
|
||||
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
||||
|
||||
let response = index.document::<Document, String>(&reader, None, document_id)
|
||||
.map_err(|_| ResponseError::DocumentNotFound(path.1.clone()))?
|
||||
.ok_or(ResponseError::DocumentNotFound(path.1.clone()))?;
|
||||
.map_err(|_| ResponseError::DocumentNotFound(path.document_id.clone()))?
|
||||
.ok_or(ResponseError::DocumentNotFound(path.document_id.clone()))?;
|
||||
|
||||
Ok(web::Json(response))
|
||||
}
|
||||
@ -33,11 +39,11 @@ pub async fn get_document(
|
||||
#[delete("/indexes/{index_uid}/documents/{document_id}")]
|
||||
pub async fn delete_document(
|
||||
data: web::Data<Data>,
|
||||
path: web::Path<(String, String)>,
|
||||
path: web::Path<DocumentParam>,
|
||||
) -> aweb::Result<HttpResponse> {
|
||||
let index = data.db.open_index(&path.0)
|
||||
.ok_or(ResponseError::IndexNotFound(path.0.clone()))?;
|
||||
let document_id = meilisearch_core::serde::compute_document_id(path.1.clone());
|
||||
let index = data.db.open_index(&path.index_uid)
|
||||
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
|
||||
let document_id = meilisearch_core::serde::compute_document_id(path.document_id.clone());
|
||||
|
||||
let mut update_writer = data.db.update_write_txn()
|
||||
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
||||
@ -66,12 +72,12 @@ pub struct BrowseQuery {
|
||||
#[get("/indexes/{index_uid}/documents")]
|
||||
pub async fn get_all_documents(
|
||||
data: web::Data<Data>,
|
||||
path: web::Path<String>,
|
||||
path: web::Path<IndexParam>,
|
||||
params: web::Query<BrowseQuery>,
|
||||
) -> aweb::Result<web::Json<Vec<Document>>> {
|
||||
|
||||
let index = data.db.open_index(path.clone())
|
||||
.ok_or(ResponseError::IndexNotFound(path.clone()))?;
|
||||
let index = data.db.open_index(&path.index_uid)
|
||||
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
|
||||
|
||||
let offset = params.offset.unwrap_or(0);
|
||||
let limit = params.limit.unwrap_or(20);
|
||||
@ -82,7 +88,7 @@ pub async fn get_all_documents(
|
||||
let documents_ids: Result<BTreeSet<_>, _> = index
|
||||
.documents_fields_counts
|
||||
.documents_ids(&reader)
|
||||
.map_err(|_| ResponseError::Internal(path.clone()))?
|
||||
.map_err(|_| ResponseError::Internal(path.index_uid.clone()))?
|
||||
.skip(offset)
|
||||
.take(limit)
|
||||
.collect();
|
||||
@ -120,14 +126,14 @@ pub struct UpdateDocumentsQuery {
|
||||
|
||||
async fn update_multiple_documents(
|
||||
data: web::Data<Data>,
|
||||
path: web::Path<String>,
|
||||
path: web::Path<IndexParam>,
|
||||
params: web::Query<UpdateDocumentsQuery>,
|
||||
body: web::Json<Vec<Document>>,
|
||||
is_partial: bool
|
||||
) -> aweb::Result<HttpResponse> {
|
||||
|
||||
let index = data.db.open_index(path.clone())
|
||||
.ok_or(ResponseError::IndexNotFound(path.clone()))?;
|
||||
let index = data.db.open_index(path.index_uid.clone())
|
||||
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
|
||||
|
||||
let reader = data.db.main_read_txn()
|
||||
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
||||
@ -182,7 +188,7 @@ async fn update_multiple_documents(
|
||||
#[post("/indexes/{index_uid}/documents")]
|
||||
pub async fn add_documents(
|
||||
data: web::Data<Data>,
|
||||
path: web::Path<String>,
|
||||
path: web::Path<IndexParam>,
|
||||
params: web::Query<UpdateDocumentsQuery>,
|
||||
body: web::Json<Vec<Document>>
|
||||
) -> aweb::Result<HttpResponse> {
|
||||
@ -192,7 +198,7 @@ pub async fn add_documents(
|
||||
#[put("/indexes/{index_uid}/documents")]
|
||||
pub async fn update_documents(
|
||||
data: web::Data<Data>,
|
||||
path: web::Path<String>,
|
||||
path: web::Path<IndexParam>,
|
||||
params: web::Query<UpdateDocumentsQuery>,
|
||||
body: web::Json<Vec<Document>>
|
||||
) -> aweb::Result<HttpResponse> {
|
||||
@ -202,12 +208,12 @@ pub async fn update_documents(
|
||||
#[post("/indexes/{index_uid}/documents/delete-batch")]
|
||||
pub async fn delete_documents(
|
||||
data: web::Data<Data>,
|
||||
path: web::Path<String>,
|
||||
path: web::Path<IndexParam>,
|
||||
body: web::Json<Vec<Value>>
|
||||
) -> aweb::Result<HttpResponse> {
|
||||
|
||||
let index = data.db.open_index(path.clone())
|
||||
.ok_or(ResponseError::IndexNotFound(path.clone()))?;
|
||||
let index = data.db.open_index(path.index_uid.clone())
|
||||
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
|
||||
|
||||
let mut writer = data.db.update_write_txn()
|
||||
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
||||
@ -233,11 +239,11 @@ pub async fn delete_documents(
|
||||
#[delete("/indexes/{index_uid}/documents")]
|
||||
pub async fn clear_all_documents(
|
||||
data: web::Data<Data>,
|
||||
path: web::Path<String>,
|
||||
path: web::Path<IndexParam>,
|
||||
) -> aweb::Result<HttpResponse> {
|
||||
|
||||
let index = data.db.open_index(path.clone())
|
||||
.ok_or(ResponseError::IndexNotFound(path.clone()))?;
|
||||
let index = data.db.open_index(path.index_uid.clone())
|
||||
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
|
||||
|
||||
let mut writer = data.db.update_write_txn()
|
||||
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
||||
|
@ -8,6 +8,7 @@ use meilisearch_core::UpdateStatus;
|
||||
|
||||
use crate::error::ResponseError;
|
||||
use crate::Data;
|
||||
use crate::routes::IndexParam;
|
||||
|
||||
fn generate_uid() -> String {
|
||||
let mut rng = rand::thread_rng();
|
||||
@ -83,11 +84,11 @@ pub async fn list_indexes(
|
||||
#[get("/indexes/{index_uid}")]
|
||||
pub async fn get_index(
|
||||
data: web::Data<Data>,
|
||||
path: web::Path<String>,
|
||||
path: web::Path<IndexParam>,
|
||||
) -> aweb::Result<web::Json<IndexResponse>> {
|
||||
|
||||
let index = data.db.open_index(path.clone())
|
||||
.ok_or(ResponseError::IndexNotFound(path.clone()))?;
|
||||
let index = data.db.open_index(path.index_uid.clone())
|
||||
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
|
||||
|
||||
let reader = data.db.main_read_txn()
|
||||
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
||||
@ -112,7 +113,7 @@ pub async fn get_index(
|
||||
|
||||
Ok(web::Json(IndexResponse {
|
||||
name,
|
||||
uid: path.to_string(),
|
||||
uid: path.index_uid.clone(),
|
||||
created_at,
|
||||
updated_at,
|
||||
primary_key,
|
||||
@ -220,12 +221,12 @@ pub struct UpdateIndexResponse {
|
||||
#[post("/indexes/{index_uid}")]
|
||||
pub async fn update_index(
|
||||
data: web::Data<Data>,
|
||||
path: web::Path<String>,
|
||||
path: web::Path<IndexParam>,
|
||||
body: web::Json<IndexCreateRequest>
|
||||
) -> aweb::Result<web::Json<IndexResponse>> {
|
||||
|
||||
let index = data.db.open_index(path.clone())
|
||||
.ok_or(ResponseError::IndexNotFound(path.clone()))?;
|
||||
let index = data.db.open_index(path.index_uid.clone())
|
||||
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
|
||||
|
||||
let mut writer = data.db.main_write_txn()
|
||||
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
||||
@ -281,7 +282,7 @@ pub async fn update_index(
|
||||
|
||||
Ok(web::Json(IndexResponse {
|
||||
name,
|
||||
uid: path.clone(),
|
||||
uid: path.index_uid.clone(),
|
||||
created_at,
|
||||
updated_at,
|
||||
primary_key,
|
||||
@ -291,45 +292,50 @@ pub async fn update_index(
|
||||
#[delete("/indexes/{index_uid}")]
|
||||
pub async fn delete_index(
|
||||
data: web::Data<Data>,
|
||||
path: web::Path<String>,
|
||||
path: web::Path<IndexParam>,
|
||||
) -> aweb::Result<HttpResponse> {
|
||||
|
||||
data.db.delete_index(&path.to_string())
|
||||
data.db.delete_index(&path.index_uid)
|
||||
.map_err(|e| ResponseError::Internal(e.to_string()))?;
|
||||
|
||||
HttpResponse::NoContent().await
|
||||
}
|
||||
|
||||
#[derive(Default, Deserialize)]
|
||||
pub struct UpdateParam {
|
||||
index_uid: String,
|
||||
update_id: u64
|
||||
}
|
||||
|
||||
#[get("/indexes/{index_uid}/updates/{update_id}")]
|
||||
pub async fn get_update_status(
|
||||
data: web::Data<Data>,
|
||||
path: web::Path<(String, u64)>,
|
||||
path: web::Path<UpdateParam>,
|
||||
) -> aweb::Result<web::Json<UpdateStatus>> {
|
||||
|
||||
let index = data.db.open_index(path.0.clone())
|
||||
.ok_or(ResponseError::IndexNotFound(path.0.clone()))?;
|
||||
let index = data.db.open_index(path.index_uid.clone())
|
||||
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
|
||||
|
||||
let reader = data.db.update_read_txn()
|
||||
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
||||
|
||||
let status = index.update_status(&reader, path.1)
|
||||
let status = index.update_status(&reader, path.update_id)
|
||||
.map_err(|e| ResponseError::Internal(e.to_string()))?;
|
||||
|
||||
match status {
|
||||
Some(status) => Ok(web::Json(status)),
|
||||
None => Err(ResponseError::NotFound(format!("Update {} not found", path.1)).into())
|
||||
None => Err(ResponseError::NotFound(format!("Update {} not found", path.update_id)).into())
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/indexes/{index_uid}/updates")]
|
||||
pub async fn get_all_updates_status(
|
||||
data: web::Data<Data>,
|
||||
path: web::Path<String>,
|
||||
path: web::Path<IndexParam>,
|
||||
) -> aweb::Result<web::Json<Vec<UpdateStatus>>> {
|
||||
|
||||
let index = data.db.open_index(path.clone())
|
||||
.ok_or(ResponseError::IndexNotFound(path.clone()))?;
|
||||
let index = data.db.open_index(path.index_uid.clone())
|
||||
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
|
||||
|
||||
let reader = data.db.update_read_txn()
|
||||
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
||||
|
@ -1,5 +1,5 @@
|
||||
use actix_web::{get, HttpResponse};
|
||||
use serde::Serialize;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use log::error;
|
||||
use meilisearch_core::ProcessedUpdateResult;
|
||||
|
||||
@ -11,10 +11,15 @@ pub mod index;
|
||||
pub mod key;
|
||||
pub mod search;
|
||||
pub mod stats;
|
||||
// pub mod setting;
|
||||
pub mod setting;
|
||||
// pub mod stop_words;
|
||||
// pub mod synonym;
|
||||
|
||||
#[derive(Default, Deserialize)]
|
||||
pub struct IndexParam {
|
||||
index_uid: String
|
||||
}
|
||||
|
||||
#[derive(Default, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct IndexUpdateResponse {
|
||||
|
@ -12,6 +12,7 @@ use actix_web as aweb;
|
||||
use crate::error::ResponseError;
|
||||
use crate::helpers::meilisearch::{Error, IndexSearchExt, SearchHit, SearchResult};
|
||||
use crate::Data;
|
||||
use crate::routes::IndexParam;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
@ -31,12 +32,12 @@ pub struct SearchQuery {
|
||||
#[get("/indexes/{index_uid}/search")]
|
||||
pub async fn search_with_url_query(
|
||||
data: web::Data<Data>,
|
||||
path: web::Path<String>,
|
||||
path: web::Path<IndexParam>,
|
||||
params: web::Query<SearchQuery>,
|
||||
) -> aweb::Result<web::Json<SearchResult>> {
|
||||
|
||||
let index = data.db.open_index(path.clone())
|
||||
.ok_or(ResponseError::IndexNotFound(path.clone()))?;
|
||||
let index = data.db.open_index(path.index_uid.clone())
|
||||
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
|
||||
|
||||
let reader = data.db.main_read_txn()
|
||||
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
||||
|
@ -1,47 +1,61 @@
|
||||
use meilisearch_core::settings::{Settings, SettingsUpdate, UpdateState, DEFAULT_RANKING_RULES};
|
||||
use std::collections::{BTreeMap, BTreeSet, HashSet};
|
||||
use tide::{Request, Response};
|
||||
use actix_web::{web, get, post, put, delete, HttpResponse};
|
||||
use actix_web as aweb;
|
||||
|
||||
use crate::error::{ResponseError, SResult};
|
||||
use crate::helpers::tide::RequestExt;
|
||||
use crate::helpers::tide::ACL::*;
|
||||
use crate::routes::document::IndexUpdateResponse;
|
||||
use crate::error::{ResponseError};
|
||||
use crate::Data;
|
||||
use crate::routes::{IndexUpdateResponse, IndexParam};
|
||||
|
||||
pub async fn get_all(ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(Private)?;
|
||||
let index = ctx.index()?;
|
||||
let db = &ctx.state().db;
|
||||
let reader = db.main_read_txn()?;
|
||||
#[get("/indexes/{index_uid}/settings")]
|
||||
pub async fn get_all(
|
||||
data: web::Data<Data>,
|
||||
path: web::Path<IndexParam>,
|
||||
) -> aweb::Result<HttpResponse> {
|
||||
let index = data.db.open_index(&path.index_uid)
|
||||
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
|
||||
|
||||
let stop_words_fst = index.main.stop_words_fst(&reader)?;
|
||||
let stop_words = stop_words_fst.unwrap_or_default().stream().into_strs()?;
|
||||
let reader = data.db.main_read_txn()
|
||||
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
||||
|
||||
let stop_words_fst = index.main.stop_words_fst(&reader)
|
||||
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
||||
let stop_words = stop_words_fst.unwrap_or_default().stream().into_strs()
|
||||
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
||||
let stop_words: BTreeSet<String> = stop_words.into_iter().collect();
|
||||
|
||||
let synonyms_fst = index.main.synonyms_fst(&reader)?.unwrap_or_default();
|
||||
let synonyms_list = synonyms_fst.stream().into_strs()?;
|
||||
let synonyms_fst = index.main.synonyms_fst(&reader)
|
||||
.map_err(|err| ResponseError::Internal(err.to_string()))?
|
||||
.unwrap_or_default();
|
||||
let synonyms_list = synonyms_fst.stream().into_strs()
|
||||
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
||||
|
||||
let mut synonyms = BTreeMap::new();
|
||||
let index_synonyms = &index.synonyms;
|
||||
for synonym in synonyms_list {
|
||||
let alternative_list = index_synonyms.synonyms(&reader, synonym.as_bytes())?;
|
||||
let alternative_list = index_synonyms.synonyms(&reader, synonym.as_bytes())
|
||||
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
||||
if let Some(list) = alternative_list {
|
||||
let list = list.stream().into_strs()?;
|
||||
let list = list.stream().into_strs()
|
||||
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
||||
synonyms.insert(synonym, list);
|
||||
}
|
||||
}
|
||||
|
||||
let ranking_rules = index
|
||||
.main
|
||||
.ranking_rules(&reader)?
|
||||
.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();
|
||||
|
||||
let distinct_attribute = index.main.distinct_attribute(&reader)?;
|
||||
let distinct_attribute = index.main.distinct_attribute(&reader)
|
||||
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
||||
|
||||
let schema = index.main.schema(&reader)?;
|
||||
let schema = index.main.schema(&reader)
|
||||
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
||||
|
||||
let searchable_attributes = schema.clone().map(|s| {
|
||||
s.indexed_name()
|
||||
@ -69,306 +83,306 @@ pub async fn get_all(ctx: Request<Data>) -> SResult<Response> {
|
||||
accept_new_fields: Some(accept_new_fields),
|
||||
};
|
||||
|
||||
Ok(tide::Response::new(200).body_json(&settings).unwrap())
|
||||
Ok(HttpResponse::Ok().json(settings))
|
||||
}
|
||||
|
||||
pub async fn update_all(mut ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(Private)?;
|
||||
let index = ctx.index()?;
|
||||
let settings: Settings =
|
||||
ctx.body_json().await.map_err(ResponseError::bad_request)?;
|
||||
let db = &ctx.state().db;
|
||||
// pub async fn update_all(mut ctx: Request<Data>) -> SResult<Response> {
|
||||
// ctx.is_allowed(Private)?;
|
||||
// let index = ctx.index()?;
|
||||
// let settings: Settings =
|
||||
// ctx.body_json().await.map_err(ResponseError::bad_request)?;
|
||||
// let db = &ctx.state().db;
|
||||
|
||||
let mut writer = db.update_write_txn()?;
|
||||
let settings = settings.into_update().map_err(ResponseError::bad_request)?;
|
||||
let update_id = index.settings_update(&mut writer, settings)?;
|
||||
writer.commit()?;
|
||||
// let mut writer = db.update_write_txn()?;
|
||||
// let settings = settings.into_update().map_err(ResponseError::bad_request)?;
|
||||
// let update_id = index.settings_update(&mut writer, settings)?;
|
||||
// writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
Ok(tide::Response::new(202).body_json(&response_body)?)
|
||||
}
|
||||
// let response_body = IndexUpdateResponse { update_id };
|
||||
// Ok(tide::Response::new(202).body_json(&response_body)?)
|
||||
// }
|
||||
|
||||
pub async fn delete_all(ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(Private)?;
|
||||
let index = ctx.index()?;
|
||||
let db = &ctx.state().db;
|
||||
let mut writer = db.update_write_txn()?;
|
||||
// pub async fn delete_all(ctx: Request<Data>) -> SResult<Response> {
|
||||
// ctx.is_allowed(Private)?;
|
||||
// let index = ctx.index()?;
|
||||
// let db = &ctx.state().db;
|
||||
// let mut writer = db.update_write_txn()?;
|
||||
|
||||
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,
|
||||
};
|
||||
// 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,
|
||||
// };
|
||||
|
||||
let update_id = index.settings_update(&mut writer, settings)?;
|
||||
// let update_id = index.settings_update(&mut writer, settings)?;
|
||||
|
||||
writer.commit()?;
|
||||
// writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
Ok(tide::Response::new(202).body_json(&response_body)?)
|
||||
}
|
||||
// let response_body = IndexUpdateResponse { update_id };
|
||||
// Ok(tide::Response::new(202).body_json(&response_body)?)
|
||||
// }
|
||||
|
||||
pub async fn get_rules(ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(Private)?;
|
||||
let index = ctx.index()?;
|
||||
let db = &ctx.state().db;
|
||||
let reader = db.main_read_txn()?;
|
||||
// pub async fn get_rules(ctx: Request<Data>) -> SResult<Response> {
|
||||
// ctx.is_allowed(Private)?;
|
||||
// let index = ctx.index()?;
|
||||
// let db = &ctx.state().db;
|
||||
// let reader = db.main_read_txn()?;
|
||||
|
||||
let ranking_rules = index
|
||||
.main
|
||||
.ranking_rules(&reader)?
|
||||
.unwrap_or(DEFAULT_RANKING_RULES.to_vec())
|
||||
.into_iter()
|
||||
.map(|r| r.to_string())
|
||||
.collect::<Vec<String>>();
|
||||
// let ranking_rules = index
|
||||
// .main
|
||||
// .ranking_rules(&reader)?
|
||||
// .unwrap_or(DEFAULT_RANKING_RULES.to_vec())
|
||||
// .into_iter()
|
||||
// .map(|r| r.to_string())
|
||||
// .collect::<Vec<String>>();
|
||||
|
||||
Ok(tide::Response::new(200).body_json(&ranking_rules).unwrap())
|
||||
}
|
||||
// Ok(tide::Response::new(200).body_json(&ranking_rules).unwrap())
|
||||
// }
|
||||
|
||||
pub async fn update_rules(mut ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(Private)?;
|
||||
let index = ctx.index()?;
|
||||
let ranking_rules: Option<Vec<String>> =
|
||||
ctx.body_json().await.map_err(ResponseError::bad_request)?;
|
||||
let db = &ctx.state().db;
|
||||
// pub async fn update_rules(mut ctx: Request<Data>) -> SResult<Response> {
|
||||
// ctx.is_allowed(Private)?;
|
||||
// let index = ctx.index()?;
|
||||
// let ranking_rules: Option<Vec<String>> =
|
||||
// ctx.body_json().await.map_err(ResponseError::bad_request)?;
|
||||
// let db = &ctx.state().db;
|
||||
|
||||
let settings = Settings {
|
||||
ranking_rules: Some(ranking_rules),
|
||||
..Settings::default()
|
||||
};
|
||||
// let settings = Settings {
|
||||
// ranking_rules: Some(ranking_rules),
|
||||
// ..Settings::default()
|
||||
// };
|
||||
|
||||
let mut writer = db.update_write_txn()?;
|
||||
let settings = settings.into_update().map_err(ResponseError::bad_request)?;
|
||||
let update_id = index.settings_update(&mut writer, settings)?;
|
||||
writer.commit()?;
|
||||
// let mut writer = db.update_write_txn()?;
|
||||
// let settings = settings.into_update().map_err(ResponseError::bad_request)?;
|
||||
// let update_id = index.settings_update(&mut writer, settings)?;
|
||||
// writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
Ok(tide::Response::new(202).body_json(&response_body)?)
|
||||
}
|
||||
// let response_body = IndexUpdateResponse { update_id };
|
||||
// Ok(tide::Response::new(202).body_json(&response_body)?)
|
||||
// }
|
||||
|
||||
pub async fn delete_rules(ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(Private)?;
|
||||
let index = ctx.index()?;
|
||||
let db = &ctx.state().db;
|
||||
let mut writer = db.update_write_txn()?;
|
||||
// pub async fn delete_rules(ctx: Request<Data>) -> SResult<Response> {
|
||||
// ctx.is_allowed(Private)?;
|
||||
// let index = ctx.index()?;
|
||||
// let db = &ctx.state().db;
|
||||
// let mut writer = db.update_write_txn()?;
|
||||
|
||||
let settings = SettingsUpdate {
|
||||
ranking_rules: UpdateState::Clear,
|
||||
..SettingsUpdate::default()
|
||||
};
|
||||
// let settings = SettingsUpdate {
|
||||
// ranking_rules: UpdateState::Clear,
|
||||
// ..SettingsUpdate::default()
|
||||
// };
|
||||
|
||||
let update_id = index.settings_update(&mut writer, settings)?;
|
||||
// let update_id = index.settings_update(&mut writer, settings)?;
|
||||
|
||||
writer.commit()?;
|
||||
// writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
Ok(tide::Response::new(202).body_json(&response_body)?)
|
||||
}
|
||||
// let response_body = IndexUpdateResponse { update_id };
|
||||
// Ok(tide::Response::new(202).body_json(&response_body)?)
|
||||
// }
|
||||
|
||||
pub async fn get_distinct(ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(Private)?;
|
||||
let index = ctx.index()?;
|
||||
let db = &ctx.state().db;
|
||||
let reader = db.main_read_txn()?;
|
||||
// pub async fn get_distinct(ctx: Request<Data>) -> SResult<Response> {
|
||||
// ctx.is_allowed(Private)?;
|
||||
// let index = ctx.index()?;
|
||||
// let db = &ctx.state().db;
|
||||
// let reader = db.main_read_txn()?;
|
||||
|
||||
let distinct_attribute = index.main.distinct_attribute(&reader)?;
|
||||
// let distinct_attribute = index.main.distinct_attribute(&reader)?;
|
||||
|
||||
Ok(tide::Response::new(200)
|
||||
.body_json(&distinct_attribute)
|
||||
.unwrap())
|
||||
}
|
||||
// Ok(tide::Response::new(200)
|
||||
// .body_json(&distinct_attribute)
|
||||
// .unwrap())
|
||||
// }
|
||||
|
||||
pub async fn update_distinct(mut ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(Private)?;
|
||||
let index = ctx.index()?;
|
||||
let distinct_attribute: Option<String> =
|
||||
ctx.body_json().await.map_err(ResponseError::bad_request)?;
|
||||
let db = &ctx.state().db;
|
||||
// pub async fn update_distinct(mut ctx: Request<Data>) -> SResult<Response> {
|
||||
// ctx.is_allowed(Private)?;
|
||||
// let index = ctx.index()?;
|
||||
// let distinct_attribute: Option<String> =
|
||||
// ctx.body_json().await.map_err(ResponseError::bad_request)?;
|
||||
// let db = &ctx.state().db;
|
||||
|
||||
let settings = Settings {
|
||||
distinct_attribute: Some(distinct_attribute),
|
||||
..Settings::default()
|
||||
};
|
||||
// let settings = Settings {
|
||||
// distinct_attribute: Some(distinct_attribute),
|
||||
// ..Settings::default()
|
||||
// };
|
||||
|
||||
let mut writer = db.update_write_txn()?;
|
||||
let settings = settings.into_update().map_err(ResponseError::bad_request)?;
|
||||
let update_id = index.settings_update(&mut writer, settings)?;
|
||||
writer.commit()?;
|
||||
// let mut writer = db.update_write_txn()?;
|
||||
// let settings = settings.into_update().map_err(ResponseError::bad_request)?;
|
||||
// let update_id = index.settings_update(&mut writer, settings)?;
|
||||
// writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
Ok(tide::Response::new(202).body_json(&response_body)?)
|
||||
}
|
||||
// let response_body = IndexUpdateResponse { update_id };
|
||||
// Ok(tide::Response::new(202).body_json(&response_body)?)
|
||||
// }
|
||||
|
||||
pub async fn delete_distinct(ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(Private)?;
|
||||
let index = ctx.index()?;
|
||||
let db = &ctx.state().db;
|
||||
let mut writer = db.update_write_txn()?;
|
||||
// pub async fn delete_distinct(ctx: Request<Data>) -> SResult<Response> {
|
||||
// ctx.is_allowed(Private)?;
|
||||
// let index = ctx.index()?;
|
||||
// let db = &ctx.state().db;
|
||||
// let mut writer = db.update_write_txn()?;
|
||||
|
||||
let settings = SettingsUpdate {
|
||||
distinct_attribute: UpdateState::Clear,
|
||||
..SettingsUpdate::default()
|
||||
};
|
||||
// let settings = SettingsUpdate {
|
||||
// distinct_attribute: UpdateState::Clear,
|
||||
// ..SettingsUpdate::default()
|
||||
// };
|
||||
|
||||
let update_id = index.settings_update(&mut writer, settings)?;
|
||||
// let update_id = index.settings_update(&mut writer, settings)?;
|
||||
|
||||
writer.commit()?;
|
||||
// writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
Ok(tide::Response::new(202).body_json(&response_body)?)
|
||||
}
|
||||
// let response_body = IndexUpdateResponse { update_id };
|
||||
// Ok(tide::Response::new(202).body_json(&response_body)?)
|
||||
// }
|
||||
|
||||
pub async fn get_searchable(ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(Private)?;
|
||||
let index = ctx.index()?;
|
||||
let db = &ctx.state().db;
|
||||
let reader = db.main_read_txn()?;
|
||||
// pub async fn get_searchable(ctx: Request<Data>) -> SResult<Response> {
|
||||
// ctx.is_allowed(Private)?;
|
||||
// let index = ctx.index()?;
|
||||
// let db = &ctx.state().db;
|
||||
// let reader = db.main_read_txn()?;
|
||||
|
||||
let schema = index.main.schema(&reader)?;
|
||||
// let schema = index.main.schema(&reader)?;
|
||||
|
||||
let searchable_attributes: Option<Vec<String>> =
|
||||
schema.map(|s| s.indexed_name().iter().map(|i| (*i).to_string()).collect());
|
||||
// let searchable_attributes: Option<Vec<String>> =
|
||||
// schema.map(|s| s.indexed_name().iter().map(|i| (*i).to_string()).collect());
|
||||
|
||||
Ok(tide::Response::new(200)
|
||||
.body_json(&searchable_attributes)
|
||||
.unwrap())
|
||||
}
|
||||
// Ok(tide::Response::new(200)
|
||||
// .body_json(&searchable_attributes)
|
||||
// .unwrap())
|
||||
// }
|
||||
|
||||
pub async fn update_searchable(mut ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(Private)?;
|
||||
let index = ctx.index()?;
|
||||
let searchable_attributes: Option<Vec<String>> =
|
||||
ctx.body_json().await.map_err(ResponseError::bad_request)?;
|
||||
let db = &ctx.state().db;
|
||||
// pub async fn update_searchable(mut ctx: Request<Data>) -> SResult<Response> {
|
||||
// ctx.is_allowed(Private)?;
|
||||
// let index = ctx.index()?;
|
||||
// let searchable_attributes: Option<Vec<String>> =
|
||||
// ctx.body_json().await.map_err(ResponseError::bad_request)?;
|
||||
// let db = &ctx.state().db;
|
||||
|
||||
let settings = Settings {
|
||||
searchable_attributes: Some(searchable_attributes),
|
||||
..Settings::default()
|
||||
};
|
||||
// let settings = Settings {
|
||||
// searchable_attributes: Some(searchable_attributes),
|
||||
// ..Settings::default()
|
||||
// };
|
||||
|
||||
let mut writer = db.update_write_txn()?;
|
||||
let settings = settings.into_update().map_err(ResponseError::bad_request)?;
|
||||
let update_id = index.settings_update(&mut writer, settings)?;
|
||||
writer.commit()?;
|
||||
// let mut writer = db.update_write_txn()?;
|
||||
// let settings = settings.into_update().map_err(ResponseError::bad_request)?;
|
||||
// let update_id = index.settings_update(&mut writer, settings)?;
|
||||
// writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
Ok(tide::Response::new(202).body_json(&response_body)?)
|
||||
}
|
||||
// let response_body = IndexUpdateResponse { update_id };
|
||||
// Ok(tide::Response::new(202).body_json(&response_body)?)
|
||||
// }
|
||||
|
||||
pub async fn delete_searchable(ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(Private)?;
|
||||
let index = ctx.index()?;
|
||||
let db = &ctx.state().db;
|
||||
// pub async fn delete_searchable(ctx: Request<Data>) -> SResult<Response> {
|
||||
// ctx.is_allowed(Private)?;
|
||||
// let index = ctx.index()?;
|
||||
// let db = &ctx.state().db;
|
||||
|
||||
let settings = SettingsUpdate {
|
||||
searchable_attributes: UpdateState::Clear,
|
||||
..SettingsUpdate::default()
|
||||
};
|
||||
// let settings = SettingsUpdate {
|
||||
// searchable_attributes: UpdateState::Clear,
|
||||
// ..SettingsUpdate::default()
|
||||
// };
|
||||
|
||||
let mut writer = db.update_write_txn()?;
|
||||
let update_id = index.settings_update(&mut writer, settings)?;
|
||||
writer.commit()?;
|
||||
// let mut writer = db.update_write_txn()?;
|
||||
// let update_id = index.settings_update(&mut writer, settings)?;
|
||||
// writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
Ok(tide::Response::new(202).body_json(&response_body)?)
|
||||
}
|
||||
// let response_body = IndexUpdateResponse { update_id };
|
||||
// Ok(tide::Response::new(202).body_json(&response_body)?)
|
||||
// }
|
||||
|
||||
pub async fn displayed(ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(Private)?;
|
||||
let index = ctx.index()?;
|
||||
let db = &ctx.state().db;
|
||||
let reader = db.main_read_txn()?;
|
||||
// pub async fn displayed(ctx: Request<Data>) -> SResult<Response> {
|
||||
// ctx.is_allowed(Private)?;
|
||||
// let index = ctx.index()?;
|
||||
// let db = &ctx.state().db;
|
||||
// let reader = db.main_read_txn()?;
|
||||
|
||||
let schema = index.main.schema(&reader)?;
|
||||
// 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)
|
||||
.unwrap())
|
||||
}
|
||||
// Ok(tide::Response::new(200)
|
||||
// .body_json(&displayed_attributes)
|
||||
// .unwrap())
|
||||
// }
|
||||
|
||||
pub async fn update_displayed(mut ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(Private)?;
|
||||
let index = ctx.index()?;
|
||||
let displayed_attributes: Option<HashSet<String>> =
|
||||
ctx.body_json().await.map_err(ResponseError::bad_request)?;
|
||||
let db = &ctx.state().db;
|
||||
// pub async fn update_displayed(mut ctx: Request<Data>) -> SResult<Response> {
|
||||
// ctx.is_allowed(Private)?;
|
||||
// let index = ctx.index()?;
|
||||
// let displayed_attributes: Option<HashSet<String>> =
|
||||
// ctx.body_json().await.map_err(ResponseError::bad_request)?;
|
||||
// let db = &ctx.state().db;
|
||||
|
||||
let settings = Settings {
|
||||
displayed_attributes: Some(displayed_attributes),
|
||||
..Settings::default()
|
||||
};
|
||||
// let settings = Settings {
|
||||
// displayed_attributes: Some(displayed_attributes),
|
||||
// ..Settings::default()
|
||||
// };
|
||||
|
||||
let mut writer = db.update_write_txn()?;
|
||||
let settings = settings.into_update().map_err(ResponseError::bad_request)?;
|
||||
let update_id = index.settings_update(&mut writer, settings)?;
|
||||
writer.commit()?;
|
||||
// let mut writer = db.update_write_txn()?;
|
||||
// let settings = settings.into_update().map_err(ResponseError::bad_request)?;
|
||||
// let update_id = index.settings_update(&mut writer, settings)?;
|
||||
// writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
Ok(tide::Response::new(202).body_json(&response_body)?)
|
||||
}
|
||||
// let response_body = IndexUpdateResponse { update_id };
|
||||
// Ok(tide::Response::new(202).body_json(&response_body)?)
|
||||
// }
|
||||
|
||||
pub async fn delete_displayed(ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(Private)?;
|
||||
let index = ctx.index()?;
|
||||
let db = &ctx.state().db;
|
||||
// pub async fn delete_displayed(ctx: Request<Data>) -> SResult<Response> {
|
||||
// ctx.is_allowed(Private)?;
|
||||
// let index = ctx.index()?;
|
||||
// let db = &ctx.state().db;
|
||||
|
||||
let settings = SettingsUpdate {
|
||||
displayed_attributes: UpdateState::Clear,
|
||||
..SettingsUpdate::default()
|
||||
};
|
||||
// let settings = SettingsUpdate {
|
||||
// displayed_attributes: UpdateState::Clear,
|
||||
// ..SettingsUpdate::default()
|
||||
// };
|
||||
|
||||
let mut writer = db.update_write_txn()?;
|
||||
let update_id = index.settings_update(&mut writer, settings)?;
|
||||
writer.commit()?;
|
||||
// let mut writer = db.update_write_txn()?;
|
||||
// let update_id = index.settings_update(&mut writer, settings)?;
|
||||
// writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
Ok(tide::Response::new(202).body_json(&response_body)?)
|
||||
}
|
||||
// let response_body = IndexUpdateResponse { update_id };
|
||||
// Ok(tide::Response::new(202).body_json(&response_body)?)
|
||||
// }
|
||||
|
||||
pub async fn get_accept_new_fields(ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(Private)?;
|
||||
let index = ctx.index()?;
|
||||
let db = &ctx.state().db;
|
||||
let reader = db.main_read_txn()?;
|
||||
// pub async fn get_accept_new_fields(ctx: Request<Data>) -> SResult<Response> {
|
||||
// ctx.is_allowed(Private)?;
|
||||
// let index = ctx.index()?;
|
||||
// let db = &ctx.state().db;
|
||||
// let reader = db.main_read_txn()?;
|
||||
|
||||
let schema = index.main.schema(&reader)?;
|
||||
// let schema = index.main.schema(&reader)?;
|
||||
|
||||
let accept_new_fields = schema.map(|s| s.accept_new_fields());
|
||||
// let accept_new_fields = schema.map(|s| s.accept_new_fields());
|
||||
|
||||
Ok(tide::Response::new(200)
|
||||
.body_json(&accept_new_fields)
|
||||
.unwrap())
|
||||
}
|
||||
// Ok(tide::Response::new(200)
|
||||
// .body_json(&accept_new_fields)
|
||||
// .unwrap())
|
||||
// }
|
||||
|
||||
pub async fn update_accept_new_fields(mut ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(Private)?;
|
||||
let index = ctx.index()?;
|
||||
let accept_new_fields: Option<bool> =
|
||||
ctx.body_json().await.map_err(ResponseError::bad_request)?;
|
||||
let db = &ctx.state().db;
|
||||
// pub async fn update_accept_new_fields(mut ctx: Request<Data>) -> SResult<Response> {
|
||||
// ctx.is_allowed(Private)?;
|
||||
// let index = ctx.index()?;
|
||||
// let accept_new_fields: Option<bool> =
|
||||
// ctx.body_json().await.map_err(ResponseError::bad_request)?;
|
||||
// let db = &ctx.state().db;
|
||||
|
||||
let settings = Settings {
|
||||
accept_new_fields: Some(accept_new_fields),
|
||||
..Settings::default()
|
||||
};
|
||||
// let settings = Settings {
|
||||
// accept_new_fields: Some(accept_new_fields),
|
||||
// ..Settings::default()
|
||||
// };
|
||||
|
||||
let mut writer = db.update_write_txn()?;
|
||||
let settings = settings.into_update().map_err(ResponseError::bad_request)?;
|
||||
let update_id = index.settings_update(&mut writer, settings)?;
|
||||
writer.commit()?;
|
||||
// let mut writer = db.update_write_txn()?;
|
||||
// let settings = settings.into_update().map_err(ResponseError::bad_request)?;
|
||||
// let update_id = index.settings_update(&mut writer, settings)?;
|
||||
// writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
Ok(tide::Response::new(202).body_json(&response_body)?)
|
||||
}
|
||||
// let response_body = IndexUpdateResponse { update_id };
|
||||
// Ok(tide::Response::new(202).body_json(&response_body)?)
|
||||
// }
|
||||
|
@ -11,6 +11,7 @@ use walkdir::WalkDir;
|
||||
|
||||
use crate::Data;
|
||||
use crate::error::ResponseError;
|
||||
use crate::routes::IndexParam;
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
@ -23,10 +24,10 @@ pub struct IndexStatsResponse {
|
||||
#[get("/indexes/{index_uid}/stats")]
|
||||
pub async fn index_stats(
|
||||
data: web::Data<Data>,
|
||||
path: web::Path<String>,
|
||||
path: web::Path<IndexParam>,
|
||||
) -> aweb::Result<web::Json<IndexStatsResponse>> {
|
||||
let index = data.db.open_index(path.clone())
|
||||
.ok_or(ResponseError::IndexNotFound(path.clone()))?;
|
||||
let index = data.db.open_index(path.index_uid.clone())
|
||||
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
|
||||
|
||||
let reader = data.db.main_read_txn()
|
||||
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
||||
@ -42,7 +43,7 @@ pub async fn index_stats(
|
||||
.map_err(|err| ResponseError::Internal(err.to_string()))?;
|
||||
|
||||
let is_indexing = data
|
||||
.is_indexing(&update_reader, &path)
|
||||
.is_indexing(&update_reader, &path.index_uid)
|
||||
.map_err(|err| ResponseError::Internal(err.to_string()))?
|
||||
.unwrap_or_default();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user