feat(api): GET /api/admin/search endpoint + regenerated client types
Expose full-text search over catalogue objects via a new admin endpoint backed by the Meilisearch SearchClient. Validates visibility filter values, short-circuits on empty queries, clamps pagination, and returns 503 when search is not configured. Registered in OpenAPI; schema.d.ts regenerated. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
//! Admin full-text search over catalogue objects. Read capability: `ViewInternal`
|
||||
//! (admins search across all visibility levels). Backed by the Meilisearch index.
|
||||
|
||||
use auth::{Authorized, ViewInternal};
|
||||
use axum::{
|
||||
Json, Router,
|
||||
extract::{Query, State},
|
||||
http::StatusCode,
|
||||
routing::get,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use utoipa::ToSchema;
|
||||
|
||||
use crate::AppState;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub(crate) struct SearchParams {
|
||||
#[serde(default)]
|
||||
q: String,
|
||||
visibility: Option<String>,
|
||||
offset: Option<i64>,
|
||||
limit: Option<i64>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, ToSchema)]
|
||||
pub(crate) struct SearchHitView {
|
||||
pub id: String,
|
||||
pub object_number: String,
|
||||
pub object_name: String,
|
||||
pub brief_description: Option<String>,
|
||||
pub visibility: String,
|
||||
pub snippet: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, ToSchema)]
|
||||
pub(crate) struct SearchResultsView {
|
||||
pub hits: Vec<SearchHitView>,
|
||||
/// Meilisearch's estimate of the total number of matches.
|
||||
pub estimated_total: usize,
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
get, path = "/api/admin/search",
|
||||
params(
|
||||
("q" = String, Query, description = "Search query text"),
|
||||
("visibility" = Option<String>, Query, description = "Filter: draft|internal|public"),
|
||||
("offset" = Option<i64>, Query, description = "default 0"),
|
||||
("limit" = Option<i64>, Query, description = "1..=50, default 20")
|
||||
),
|
||||
responses(
|
||||
(status = 200, body = SearchResultsView),
|
||||
(status = 400, description = "Invalid visibility value"),
|
||||
(status = 401),
|
||||
(status = 403),
|
||||
(status = 503, description = "Search is not configured")
|
||||
)
|
||||
)]
|
||||
pub(crate) async fn search_objects(
|
||||
_auth: Authorized<ViewInternal>,
|
||||
State(state): State<AppState>,
|
||||
Query(params): Query<SearchParams>,
|
||||
) -> Result<Json<SearchResultsView>, StatusCode> {
|
||||
let Some(search) = &state.search else {
|
||||
return Err(StatusCode::SERVICE_UNAVAILABLE);
|
||||
};
|
||||
|
||||
let visibility = match params.visibility.as_deref() {
|
||||
None | Some("") => None,
|
||||
Some(v @ ("draft" | "internal" | "public")) => Some(v),
|
||||
Some(_) => return Err(StatusCode::BAD_REQUEST),
|
||||
};
|
||||
|
||||
let q = params.q.trim();
|
||||
|
||||
if q.is_empty() {
|
||||
return Ok(Json(SearchResultsView {
|
||||
hits: Vec::new(),
|
||||
estimated_total: 0,
|
||||
}));
|
||||
}
|
||||
|
||||
let offset = params.offset.unwrap_or(0).max(0) as usize;
|
||||
let limit = params.limit.unwrap_or(20).clamp(1, 50) as usize;
|
||||
|
||||
let results = search
|
||||
.search_objects(q, visibility, offset, limit)
|
||||
.await
|
||||
.map_err(|err| {
|
||||
tracing::error!(?err, "search query failed");
|
||||
StatusCode::INTERNAL_SERVER_ERROR
|
||||
})?;
|
||||
|
||||
Ok(Json(SearchResultsView {
|
||||
hits: results
|
||||
.hits
|
||||
.into_iter()
|
||||
.map(|h| SearchHitView {
|
||||
id: h.id,
|
||||
object_number: h.object_number,
|
||||
object_name: h.object_name,
|
||||
brief_description: h.brief_description,
|
||||
visibility: h.visibility,
|
||||
snippet: h.snippet,
|
||||
})
|
||||
.collect(),
|
||||
estimated_total: results.estimated_total,
|
||||
}))
|
||||
}
|
||||
|
||||
pub(crate) fn routes() -> Router<AppState> {
|
||||
Router::new().route("/api/admin/search", get(search_objects))
|
||||
}
|
||||
Reference in New Issue
Block a user