Skip to content

Commit

Permalink
First call to OpenSearch
Browse files Browse the repository at this point in the history
  • Loading branch information
sestrella committed Apr 22, 2024
1 parent 4aef8cf commit 79438e1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
2 changes: 2 additions & 0 deletions backend-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
53 changes: 47 additions & 6 deletions backend-rs/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
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 {
opensearch: OpenSearch,
pool: PgPool,
}

enum AppError {}

impl IntoResponse for AppError {
fn into_response(self) -> axum::response::Response {
todo!()
}
}

impl From<opensearch::Error> for AppError {
fn from(value: opensearch::Error) -> Self {
todo!()
}
}

#[tokio::main]
async fn main() {
// TODO: read PG and OS host names from env variables
Expand All @@ -36,9 +52,34 @@ async fn main() {
axum::serve(listener, app).await.unwrap();
}

async fn get_flake(State(state): State<Arc<AppState>>) -> &'static str {
let opensearch = &state.opensearch;
"Flake"
async fn get_flake(
State(state): State<Arc<AppState>>,
Query(params): Query<HashMap<String, String>>,
) -> 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 {
Expand Down

0 comments on commit 79438e1

Please sign in to comment.