-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from curieo-org/search_endpoint
added initial version of search APIs
- Loading branch information
Showing
17 changed files
with
508 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
create table search_history ( | ||
search_history_id uuid primary key default uuid_generate_v1mc(), | ||
user_id uuid not null references users(user_id), | ||
query text not null, | ||
result text not null, | ||
sources text[] not null, | ||
created_at timestamptz not null default now(), | ||
updated_at timestamptz not null default now() | ||
); | ||
|
||
-- And applying our `updated_at` trigger is as easy as this. | ||
SELECT trigger_updated_at('search_history'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
pub use models::*; | ||
pub use routes::*; | ||
pub use services::*; | ||
|
||
pub mod models; | ||
pub mod routes; | ||
pub mod services; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use sqlx::types::time; | ||
use sqlx::FromRow; | ||
use std::fmt::Debug; | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct TopSearchRequest { | ||
pub limit: Option<i64>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct RAGTokenResponse { | ||
pub access_token: String, | ||
pub token_type: String, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct SearchQueryRequest { | ||
pub query: String, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct SearchHistoryRequest { | ||
pub limit: Option<u8>, | ||
pub offset: Option<u8>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct SearchResponse { | ||
pub result: String, | ||
pub sources: Vec<String>, | ||
} | ||
|
||
#[derive(FromRow, Serialize, Deserialize, Clone, Debug)] | ||
pub struct SearchHistory { | ||
pub search_history_id: uuid::Uuid, | ||
pub user_id: uuid::Uuid, | ||
pub query: String, | ||
pub result: String, | ||
pub sources: Vec<String>, | ||
|
||
pub created_at: time::OffsetDateTime, | ||
pub updated_at: time::OffsetDateTime, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use crate::err::AppError; | ||
use crate::search::services; | ||
use crate::search::{SearchHistoryRequest, SearchQueryRequest, TopSearchRequest}; | ||
use crate::startup::AppState; | ||
use crate::users::User; | ||
use axum::extract::{Query, State}; | ||
use axum::http::StatusCode; | ||
use axum::response::IntoResponse; | ||
use axum::routing::get; | ||
use axum::{Json, Router}; | ||
use redis::{AsyncCommands, Client as RedisClient}; | ||
use sqlx::PgPool; | ||
|
||
#[tracing::instrument(level = "debug", skip_all, ret, err(Debug))] | ||
async fn get_search_handler( | ||
State(pool): State<PgPool>, | ||
State(cache): State<RedisClient>, | ||
user: User, | ||
Query(search_query): Query<SearchQueryRequest>, | ||
) -> crate::Result<impl IntoResponse> { | ||
let user_id = user.user_id; | ||
|
||
let mut connection = cache | ||
.get_multiplexed_async_connection() | ||
.await | ||
.map_err(|e| AppError::from(e))?; | ||
|
||
let search_response = services::search(&mut connection, &search_query).await?; | ||
services::insert_search_history( | ||
&pool, | ||
&mut connection, | ||
&user_id, | ||
&search_query, | ||
&search_response, | ||
) | ||
.await?; | ||
|
||
connection | ||
.zincr("search_queries", &search_query.query, 1) | ||
.await | ||
.map_err(|e| AppError::from(e))?; | ||
|
||
Ok((StatusCode::OK, Json(search_response))) | ||
} | ||
|
||
#[tracing::instrument(level = "debug", skip_all, ret, err(Debug))] | ||
async fn get_search_history_handler( | ||
State(pool): State<PgPool>, | ||
user: User, | ||
Query(search_history_request): Query<SearchHistoryRequest>, | ||
) -> crate::Result<impl IntoResponse> { | ||
let user_id = user.user_id; | ||
|
||
let search_history = | ||
services::get_search_history(&pool, &user_id, &search_history_request).await?; | ||
|
||
Ok((StatusCode::OK, Json(search_history))) | ||
} | ||
|
||
#[tracing::instrument(level = "debug", skip_all, ret, err(Debug))] | ||
async fn get_top_searches_handler( | ||
State(cache): State<RedisClient>, | ||
Query(query): Query<TopSearchRequest>, | ||
) -> crate::Result<impl IntoResponse> { | ||
let mut connection = cache | ||
.get_multiplexed_async_connection() | ||
.await | ||
.map_err(|e| AppError::from(e))?; | ||
|
||
let top_searches = services::get_top_searches(&mut connection, &query).await?; | ||
|
||
Ok((StatusCode::OK, Json(top_searches))) | ||
} | ||
|
||
pub fn routes() -> Router<AppState> { | ||
Router::new() | ||
.route("/", get(get_search_handler)) | ||
.route("/history", get(get_search_history_handler)) | ||
.route("/top", get(get_top_searches_handler)) | ||
} |
Oops, something went wrong.