From 79438e15acf777493eb544a76caa4121bfd2adae Mon Sep 17 00:00:00 2001 From: Sebastian Estrella <2049686+sestrella@users.noreply.github.com> Date: Sun, 21 Apr 2024 21:35:44 -0500 Subject: [PATCH] First call to OpenSearch --- backend-rs/Cargo.toml | 2 ++ backend-rs/src/main.rs | 53 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/backend-rs/Cargo.toml b/backend-rs/Cargo.toml index 60634e9..0f68e9a 100644 --- a/backend-rs/Cargo.toml +++ b/backend-rs/Cargo.toml @@ -8,5 +8,7 @@ edition = "2021" [dependencies] axum = "0.7" opensearch = "2.2" +serde = "1.0" +serde_json = "1.0" sqlx = { version = "0.7", features = ["runtime-tokio", "postgres"] } tokio = { version = "1.37", features = ["full"] } diff --git a/backend-rs/src/main.rs b/backend-rs/src/main.rs index 18cafd5..c140ddd 100644 --- a/backend-rs/src/main.rs +++ b/backend-rs/src/main.rs @@ -1,11 +1,13 @@ -use std::sync::Arc; +use std::{collections::HashMap, sync::Arc}; use axum::{ - extract::State, + extract::{Query, State}, + response::{ErrorResponse, IntoResponse}, routing::{get, post}, Router, }; -use opensearch::OpenSearch; +use opensearch::{OpenSearch, SearchParts}; +use serde_json::json; use sqlx::{postgres::PgPoolOptions, PgPool}; struct AppState { @@ -13,6 +15,20 @@ struct AppState { pool: PgPool, } +enum AppError {} + +impl IntoResponse for AppError { + fn into_response(self) -> axum::response::Response { + todo!() + } +} + +impl From for AppError { + fn from(value: opensearch::Error) -> Self { + todo!() + } +} + #[tokio::main] async fn main() { // TODO: read PG and OS host names from env variables @@ -36,9 +52,34 @@ async fn main() { axum::serve(listener, app).await.unwrap(); } -async fn get_flake(State(state): State>) -> &'static str { - let opensearch = &state.opensearch; - "Flake" +async fn get_flake( + State(state): State>, + Query(params): Query>, +) -> Result<(), AppError> { + if let Some(q) = params.get("q") { + let response = &state + .opensearch + .search(SearchParts::Index(&["opensearch_index"])) + .size(10) + .body(json!({ + "query": { + "multi_match": { + "query": q, + "fuzziness": "AUTO", + "fields": [ + "description^2", + "readme", + "outputs", + "repo^2", + "owner^2", + ], + } + } + })) + .send() + .await?; + } + Ok(()) } async fn post_publish() -> &'static str {