2021-10-14 02:56:28 +08:00
|
|
|
use actix_web::{web, HttpRequest, HttpResponse};
|
2021-06-23 18:18:34 +08:00
|
|
|
use log::debug;
|
2021-12-02 23:03:26 +08:00
|
|
|
use meilisearch_error::ResponseError;
|
|
|
|
use meilisearch_lib::index_controller::Update;
|
2021-09-29 04:22:59 +08:00
|
|
|
use meilisearch_lib::MeiliSearch;
|
2021-05-31 22:03:39 +08:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-10-12 20:46:35 +08:00
|
|
|
use serde_json::json;
|
2022-02-14 22:32:41 +08:00
|
|
|
use time::OffsetDateTime;
|
2020-12-12 20:32:06 +08:00
|
|
|
|
2021-10-12 20:46:35 +08:00
|
|
|
use crate::analytics::Analytics;
|
2021-06-24 21:33:21 +08:00
|
|
|
use crate::extractors::authentication::{policies::*, GuardedData};
|
2022-03-05 03:12:44 +08:00
|
|
|
use crate::extractors::sequential_extractor::SeqHandler;
|
2021-12-02 23:03:26 +08:00
|
|
|
use crate::task::SummarizedTaskView;
|
2020-12-12 20:32:06 +08:00
|
|
|
|
2021-07-07 22:20:22 +08:00
|
|
|
pub mod documents;
|
|
|
|
pub mod search;
|
|
|
|
pub mod settings;
|
2021-07-05 20:29:20 +08:00
|
|
|
|
|
|
|
pub fn configure(cfg: &mut web::ServiceConfig) {
|
2021-06-24 21:33:21 +08:00
|
|
|
cfg.service(
|
2021-07-05 20:29:20 +08:00
|
|
|
web::resource("")
|
2021-06-24 21:33:21 +08:00
|
|
|
.route(web::get().to(list_indexes))
|
2022-03-05 03:12:44 +08:00
|
|
|
.route(web::post().to(SeqHandler(create_index))),
|
2021-06-24 21:33:21 +08:00
|
|
|
)
|
|
|
|
.service(
|
2021-07-05 20:29:20 +08:00
|
|
|
web::scope("/{index_uid}")
|
|
|
|
.service(
|
|
|
|
web::resource("")
|
2022-03-05 03:12:44 +08:00
|
|
|
.route(web::get().to(SeqHandler(get_index)))
|
|
|
|
.route(web::put().to(SeqHandler(update_index)))
|
|
|
|
.route(web::delete().to(SeqHandler(delete_index))),
|
2021-07-05 20:29:20 +08:00
|
|
|
)
|
2022-03-05 03:12:44 +08:00
|
|
|
.service(web::resource("/stats").route(web::get().to(SeqHandler(get_index_stats))))
|
2021-07-05 20:29:20 +08:00
|
|
|
.service(web::scope("/documents").configure(documents::configure))
|
|
|
|
.service(web::scope("/search").configure(search::configure))
|
2021-09-24 20:55:57 +08:00
|
|
|
.service(web::scope("/settings").configure(settings::configure)),
|
2021-06-24 21:33:21 +08:00
|
|
|
);
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
2022-05-24 17:29:03 +08:00
|
|
|
const PAGINATION_DEFAULT_LIMIT: fn() -> usize = || 20;
|
|
|
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
|
|
pub struct Paginate {
|
|
|
|
#[serde(default)]
|
|
|
|
offset: usize,
|
|
|
|
#[serde(default = "PAGINATION_DEFAULT_LIMIT")]
|
|
|
|
limit: usize,
|
|
|
|
}
|
|
|
|
|
2021-09-29 04:22:59 +08:00
|
|
|
pub async fn list_indexes(
|
2021-11-09 01:31:27 +08:00
|
|
|
data: GuardedData<ActionPolicy<{ actions::INDEXES_GET }>, MeiliSearch>,
|
2022-05-24 17:29:03 +08:00
|
|
|
paginate: web::Query<Paginate>,
|
2021-09-29 04:22:59 +08:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2022-01-12 22:35:33 +08:00
|
|
|
let search_rules = &data.filters().search_rules;
|
2022-05-24 17:29:03 +08:00
|
|
|
let indexes: Vec<_> = data.list_indexes().await?;
|
|
|
|
let nb_indexes = indexes.len();
|
|
|
|
let indexes: Vec<_> = indexes
|
2022-01-12 22:35:33 +08:00
|
|
|
.into_iter()
|
|
|
|
.filter(|i| search_rules.is_index_authorized(&i.uid))
|
2022-05-24 17:29:03 +08:00
|
|
|
.skip(paginate.offset)
|
|
|
|
.take(paginate.limit)
|
2022-01-12 22:35:33 +08:00
|
|
|
.collect();
|
2021-11-09 01:31:27 +08:00
|
|
|
|
2021-07-05 20:29:20 +08:00
|
|
|
debug!("returns: {:?}", indexes);
|
2022-05-24 17:29:03 +08:00
|
|
|
Ok(HttpResponse::Ok().json(json!({
|
|
|
|
"results": indexes,
|
|
|
|
"offset": paginate.offset,
|
|
|
|
"limit": paginate.limit,
|
|
|
|
"total": nb_indexes,
|
|
|
|
})))
|
2021-07-05 20:29:20 +08:00
|
|
|
}
|
|
|
|
|
2020-12-12 20:32:06 +08:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
2021-07-07 22:20:22 +08:00
|
|
|
pub struct IndexCreateRequest {
|
2021-02-19 00:48:37 +08:00
|
|
|
uid: String,
|
2020-12-12 20:32:06 +08:00
|
|
|
primary_key: Option<String>,
|
|
|
|
}
|
|
|
|
|
2021-09-29 00:10:09 +08:00
|
|
|
pub async fn create_index(
|
2021-12-06 22:45:41 +08:00
|
|
|
meilisearch: GuardedData<ActionPolicy<{ actions::INDEXES_CREATE }>, MeiliSearch>,
|
2021-09-29 00:10:09 +08:00
|
|
|
body: web::Json<IndexCreateRequest>,
|
2021-10-14 02:56:28 +08:00
|
|
|
req: HttpRequest,
|
2021-10-29 22:10:58 +08:00
|
|
|
analytics: web::Data<dyn Analytics>,
|
2021-09-29 00:10:09 +08:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-12-02 23:03:26 +08:00
|
|
|
let IndexCreateRequest {
|
|
|
|
primary_key, uid, ..
|
|
|
|
} = body.into_inner();
|
2021-10-12 20:46:35 +08:00
|
|
|
|
|
|
|
analytics.publish(
|
|
|
|
"Index Created".to_string(),
|
2021-12-02 23:03:26 +08:00
|
|
|
json!({ "primary_key": primary_key }),
|
2021-10-14 02:56:28 +08:00
|
|
|
Some(&req),
|
2021-10-12 20:46:35 +08:00
|
|
|
);
|
2021-12-02 23:03:26 +08:00
|
|
|
|
|
|
|
let update = Update::CreateIndex { primary_key };
|
|
|
|
let task: SummarizedTaskView = meilisearch.register_update(uid, update).await?.into();
|
|
|
|
|
|
|
|
Ok(HttpResponse::Accepted().json(task))
|
2021-09-29 00:10:09 +08:00
|
|
|
}
|
2021-07-05 20:29:20 +08:00
|
|
|
|
2020-12-12 20:32:06 +08:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
2021-12-02 23:03:26 +08:00
|
|
|
#[allow(dead_code)]
|
2021-07-07 22:20:22 +08:00
|
|
|
pub struct UpdateIndexRequest {
|
2021-03-15 21:43:47 +08:00
|
|
|
uid: Option<String>,
|
2020-12-12 20:32:06 +08:00
|
|
|
primary_key: Option<String>,
|
|
|
|
}
|
|
|
|
|
2021-04-28 22:43:49 +08:00
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct UpdateIndexResponse {
|
|
|
|
name: String,
|
|
|
|
uid: String,
|
2022-02-14 22:32:41 +08:00
|
|
|
#[serde(serialize_with = "time::serde::rfc3339::serialize")]
|
|
|
|
created_at: OffsetDateTime,
|
|
|
|
#[serde(serialize_with = "time::serde::rfc3339::serialize")]
|
|
|
|
updated_at: OffsetDateTime,
|
|
|
|
#[serde(serialize_with = "time::serde::rfc3339::serialize")]
|
|
|
|
primary_key: OffsetDateTime,
|
2021-04-28 22:43:49 +08:00
|
|
|
}
|
2021-07-05 16:10:17 +08:00
|
|
|
|
2021-07-07 22:20:22 +08:00
|
|
|
pub async fn get_index(
|
2021-11-09 01:31:27 +08:00
|
|
|
meilisearch: GuardedData<ActionPolicy<{ actions::INDEXES_GET }>, MeiliSearch>,
|
2021-12-02 23:03:26 +08:00
|
|
|
path: web::Path<String>,
|
2021-06-24 21:33:21 +08:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-12-02 23:03:26 +08:00
|
|
|
let meta = meilisearch.get_index(path.into_inner()).await?;
|
2021-06-24 21:33:21 +08:00
|
|
|
debug!("returns: {:?}", meta);
|
|
|
|
Ok(HttpResponse::Ok().json(meta))
|
|
|
|
}
|
|
|
|
|
2021-07-07 22:20:22 +08:00
|
|
|
pub async fn update_index(
|
2021-11-09 01:31:27 +08:00
|
|
|
meilisearch: GuardedData<ActionPolicy<{ actions::INDEXES_UPDATE }>, MeiliSearch>,
|
2021-12-02 23:03:26 +08:00
|
|
|
path: web::Path<String>,
|
2021-02-09 18:26:52 +08:00
|
|
|
body: web::Json<UpdateIndexRequest>,
|
2021-10-14 02:56:28 +08:00
|
|
|
req: HttpRequest,
|
2021-10-29 22:10:58 +08:00
|
|
|
analytics: web::Data<dyn Analytics>,
|
2020-12-12 20:32:06 +08:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-06-23 18:18:34 +08:00
|
|
|
debug!("called with params: {:?}", body);
|
2021-03-15 23:52:05 +08:00
|
|
|
let body = body.into_inner();
|
2021-10-12 21:00:04 +08:00
|
|
|
analytics.publish(
|
|
|
|
"Index Updated".to_string(),
|
2021-10-20 20:29:08 +08:00
|
|
|
json!({ "primary_key": body.primary_key}),
|
2021-10-14 02:56:28 +08:00
|
|
|
Some(&req),
|
2021-10-12 21:00:04 +08:00
|
|
|
);
|
2021-12-02 23:03:26 +08:00
|
|
|
|
|
|
|
let update = Update::UpdateIndex {
|
2021-09-21 19:23:22 +08:00
|
|
|
primary_key: body.primary_key,
|
|
|
|
};
|
2021-12-02 23:03:26 +08:00
|
|
|
|
|
|
|
let task: SummarizedTaskView = meilisearch
|
|
|
|
.register_update(path.into_inner(), update)
|
|
|
|
.await?
|
|
|
|
.into();
|
|
|
|
|
|
|
|
debug!("returns: {:?}", task);
|
|
|
|
Ok(HttpResponse::Accepted().json(task))
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
2021-09-29 00:10:09 +08:00
|
|
|
pub async fn delete_index(
|
2021-11-09 01:31:27 +08:00
|
|
|
meilisearch: GuardedData<ActionPolicy<{ actions::INDEXES_DELETE }>, MeiliSearch>,
|
2021-12-02 23:03:26 +08:00
|
|
|
path: web::Path<String>,
|
2021-09-29 00:10:09 +08:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-12-02 23:03:26 +08:00
|
|
|
let uid = path.into_inner();
|
|
|
|
let update = Update::DeleteIndex;
|
|
|
|
let task: SummarizedTaskView = meilisearch.register_update(uid, update).await?.into();
|
|
|
|
|
|
|
|
Ok(HttpResponse::Accepted().json(task))
|
2021-09-29 00:10:09 +08:00
|
|
|
}
|
2020-12-12 20:32:06 +08:00
|
|
|
|
2021-07-07 22:20:22 +08:00
|
|
|
pub async fn get_index_stats(
|
2021-11-09 01:31:27 +08:00
|
|
|
meilisearch: GuardedData<ActionPolicy<{ actions::STATS_GET }>, MeiliSearch>,
|
2021-12-02 23:03:26 +08:00
|
|
|
path: web::Path<String>,
|
2020-12-12 20:32:06 +08:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-12-02 23:03:26 +08:00
|
|
|
let response = meilisearch.get_index_stats(path.into_inner()).await?;
|
2021-04-30 01:31:58 +08:00
|
|
|
|
2021-07-05 20:29:20 +08:00
|
|
|
debug!("returns: {:?}", response);
|
|
|
|
Ok(HttpResponse::Ok().json(response))
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|