MeiliSearch/meilisearch-http/src/routes/indexes/mod.rs

161 lines
4.9 KiB
Rust
Raw Normal View History

2021-10-13 20:56:28 +02:00
use actix_web::{web, HttpRequest, HttpResponse};
2021-05-10 20:24:14 +02:00
use chrono::{DateTime, Utc};
2021-06-23 12:18:34 +02:00
use log::debug;
use meilisearch_error::ResponseError;
use meilisearch_lib::index_controller::Update;
2021-09-28 22:22:59 +02:00
use meilisearch_lib::MeiliSearch;
2021-05-31 16:03:39 +02:00
use serde::{Deserialize, Serialize};
2021-10-12 14:46:35 +02:00
use serde_json::json;
2020-12-12 13:32:06 +01:00
2021-10-12 14:46:35 +02:00
use crate::analytics::Analytics;
2021-06-24 15:33:21 +02:00
use crate::extractors::authentication::{policies::*, GuardedData};
use crate::task::SummarizedTaskView;
2020-12-12 13:32:06 +01:00
2021-07-07 16:20:22 +02:00
pub mod documents;
pub mod search;
pub mod settings;
pub mod tasks;
2021-07-05 14:29:20 +02:00
pub fn configure(cfg: &mut web::ServiceConfig) {
2021-06-24 15:33:21 +02:00
cfg.service(
2021-07-05 14:29:20 +02:00
web::resource("")
2021-06-24 15:33:21 +02:00
.route(web::get().to(list_indexes))
2021-09-28 18:10:09 +02:00
.route(web::post().to(create_index)),
2021-06-24 15:33:21 +02:00
)
.service(
2021-07-05 14:29:20 +02:00
web::scope("/{index_uid}")
.service(
web::resource("")
.route(web::get().to(get_index))
.route(web::put().to(update_index))
2021-09-28 18:10:09 +02:00
.route(web::delete().to(delete_index)),
2021-07-05 14:29:20 +02:00
)
.service(web::resource("/stats").route(web::get().to(get_index_stats)))
.service(web::scope("/documents").configure(documents::configure))
.service(web::scope("/search").configure(search::configure))
.service(web::scope("/tasks").configure(tasks::configure))
2021-09-24 14:55:57 +02:00
.service(web::scope("/settings").configure(settings::configure)),
2021-06-24 15:33:21 +02:00
);
2020-12-12 13:32:06 +01:00
}
2021-09-28 22:22:59 +02:00
pub async fn list_indexes(
data: GuardedData<ActionPolicy<{ actions::INDEXES_GET }>, MeiliSearch>,
2021-09-28 22:22:59 +02:00
) -> Result<HttpResponse, ResponseError> {
let filters = data.filters();
let mut indexes = data.list_indexes().await?;
if let Some(indexes_filter) = filters.indexes.as_ref() {
indexes = indexes
.into_iter()
.filter(|i| indexes_filter.contains(&i.uid))
.collect();
}
2021-07-05 14:29:20 +02:00
debug!("returns: {:?}", indexes);
Ok(HttpResponse::Ok().json(indexes))
}
2020-12-12 13:32:06 +01:00
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
2021-07-07 16:20:22 +02:00
pub struct IndexCreateRequest {
2021-02-18 17:48:37 +01:00
uid: String,
2020-12-12 13:32:06 +01:00
primary_key: Option<String>,
}
2021-09-28 18:10:09 +02:00
pub async fn create_index(
meilisearch: GuardedData<ActionPolicy<{ actions::INDEXES_CREATE }>, MeiliSearch>,
2021-09-28 18:10:09 +02:00
body: web::Json<IndexCreateRequest>,
2021-10-13 20:56:28 +02:00
req: HttpRequest,
2021-10-29 16:10:58 +02:00
analytics: web::Data<dyn Analytics>,
2021-09-28 18:10:09 +02:00
) -> Result<HttpResponse, ResponseError> {
let IndexCreateRequest {
primary_key, uid, ..
} = body.into_inner();
2021-10-12 14:46:35 +02:00
analytics.publish(
"Index Created".to_string(),
json!({ "primary_key": primary_key }),
2021-10-13 20:56:28 +02:00
Some(&req),
2021-10-12 14:46:35 +02:00
);
let update = Update::CreateIndex { primary_key };
let task: SummarizedTaskView = meilisearch.register_update(uid, update).await?.into();
Ok(HttpResponse::Accepted().json(task))
2021-09-28 18:10:09 +02:00
}
2021-07-05 14:29:20 +02:00
2020-12-12 13:32:06 +01:00
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[allow(dead_code)]
2021-07-07 16:20:22 +02:00
pub struct UpdateIndexRequest {
2021-03-15 14:43:47 +01:00
uid: Option<String>,
2020-12-12 13:32:06 +01:00
primary_key: Option<String>,
}
2021-04-28 16:43:49 +02:00
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateIndexResponse {
name: String,
uid: String,
created_at: DateTime<Utc>,
updated_at: DateTime<Utc>,
primary_key: Option<String>,
}
2021-07-05 10:10:17 +02:00
2021-07-07 16:20:22 +02:00
pub async fn get_index(
meilisearch: GuardedData<ActionPolicy<{ actions::INDEXES_GET }>, MeiliSearch>,
path: web::Path<String>,
2021-06-24 15:33:21 +02:00
) -> Result<HttpResponse, ResponseError> {
let meta = meilisearch.get_index(path.into_inner()).await?;
2021-06-24 15:33:21 +02:00
debug!("returns: {:?}", meta);
Ok(HttpResponse::Ok().json(meta))
}
2021-07-07 16:20:22 +02:00
pub async fn update_index(
meilisearch: GuardedData<ActionPolicy<{ actions::INDEXES_UPDATE }>, MeiliSearch>,
path: web::Path<String>,
body: web::Json<UpdateIndexRequest>,
2021-10-13 20:56:28 +02:00
req: HttpRequest,
2021-10-29 16:10:58 +02:00
analytics: web::Data<dyn Analytics>,
2020-12-12 13:32:06 +01:00
) -> Result<HttpResponse, ResponseError> {
2021-06-23 12:18:34 +02:00
debug!("called with params: {:?}", body);
2021-03-15 16:52:05 +01:00
let body = body.into_inner();
2021-10-12 15:00:04 +02:00
analytics.publish(
"Index Updated".to_string(),
json!({ "primary_key": body.primary_key}),
2021-10-13 20:56:28 +02:00
Some(&req),
2021-10-12 15:00:04 +02:00
);
let update = Update::UpdateIndex {
primary_key: body.primary_key,
};
let task: SummarizedTaskView = meilisearch
.register_update(path.into_inner(), update)
.await?
.into();
debug!("returns: {:?}", task);
Ok(HttpResponse::Accepted().json(task))
2020-12-12 13:32:06 +01:00
}
2021-09-28 18:10:09 +02:00
pub async fn delete_index(
meilisearch: GuardedData<ActionPolicy<{ actions::INDEXES_DELETE }>, MeiliSearch>,
path: web::Path<String>,
2021-09-28 18:10:09 +02:00
) -> Result<HttpResponse, ResponseError> {
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-28 18:10:09 +02:00
}
2020-12-12 13:32:06 +01:00
2021-07-07 16:20:22 +02:00
pub async fn get_index_stats(
meilisearch: GuardedData<ActionPolicy<{ actions::STATS_GET }>, MeiliSearch>,
path: web::Path<String>,
2020-12-12 13:32:06 +01:00
) -> Result<HttpResponse, ResponseError> {
let response = meilisearch.get_index_stats(path.into_inner()).await?;
2021-07-05 14:29:20 +02:00
debug!("returns: {:?}", response);
Ok(HttpResponse::Ok().json(response))
2020-12-12 13:32:06 +01:00
}