Search changes

This commit is contained in:
StNicolay 2024-08-04 10:03:35 +03:00
parent b6c71ee35b
commit bac5584b46
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
3 changed files with 19 additions and 9 deletions

View File

@ -1,9 +1,10 @@
SELECT SELECT
user_id, username, email user_id, username, email,
FROM
users
ORDER BY
GREATEST ( GREATEST (
similarity (email, $1), similarity (email, $1),
similarity (username, $1) similarity (username, $1)
) DESC ) as "similarity!"
FROM
users
ORDER BY
"similarity!" DESC

View File

@ -95,9 +95,17 @@ pub async fn get_hash(search_string: &str, pool: &Pool) -> sqlx::Result<Option<(
Ok(record.map(|record| (record.user_id, record.hashed_password))) Ok(record.map(|record| (record.user_id, record.hashed_password)))
} }
#[derive(Serialize, Debug)]
pub struct UserSearch {
pub user_id: i32,
pub username: String,
pub email: String,
pub similarity: f32,
}
pub fn search_for_user<'a>( pub fn search_for_user<'a>(
search_string: &str, search_string: &str,
pool: &'a Pool, pool: &'a Pool,
) -> BoxStream<'a, sqlx::Result<UserInfo>> { ) -> BoxStream<'a, sqlx::Result<UserSearch>> {
sqlx::query_file_as!(UserInfo, "sql/search_for_user.sql", search_string).fetch(pool) sqlx::query_file_as!(UserSearch, "sql/search_for_user.sql", search_string).fetch(pool)
} }

View File

@ -1,4 +1,4 @@
use futures::TryStreamExt; use futures::{future, TryStreamExt};
use crate::prelude::*; use crate::prelude::*;
@ -10,9 +10,10 @@ pub struct Params {
pub async fn search( pub async fn search(
State(pool): State<Pool>, State(pool): State<Pool>,
Query(params): Query<Params>, Query(params): Query<Params>,
) -> sqlx::Result<Json<Vec<db::users::UserInfo>>, StatusCode> { ) -> sqlx::Result<Json<Vec<db::users::UserSearch>>, StatusCode> {
let users = db::users::search_for_user(&params.search_string, &pool) let users = db::users::search_for_user(&params.search_string, &pool)
.take(20) .take(20)
.try_filter(|user| future::ready(user.similarity > 0.1))
.try_collect() .try_collect()
.await .await
.handle_internal()?; .handle_internal()?;