From 4681c3ff22b61c3cde61e94f0b59930e8936a51b Mon Sep 17 00:00:00 2001 From: ecco257 <72117210+ecco257@users.noreply.github.com> Date: Fri, 15 Mar 2024 17:01:28 -0400 Subject: [PATCH] Modify search functions to include distance for each result --- src/search.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/search.rs b/src/search.rs index 3b24f08..767675e 100644 --- a/src/search.rs +++ b/src/search.rs @@ -39,10 +39,11 @@ if #[cfg(feature = "ssr")] { /// # Returns /// A Result containing a vector of albums if the search was successful, or an error if the search failed #[server(endpoint = "search_albums")] -pub async fn search_albums(query: String, limit: i64) -> Result, ServerFnError> { +pub async fn search_albums(query: String, limit: i64) -> Result, ServerFnError> { use crate::schema::albums::dsl::*; Ok(albums + .select((albums::all_columns(), trgm_distance(title, query.clone()))) .filter(trgm_similar(title, query.clone())) .order_by(trgm_distance(title, query)) .limit(limit) @@ -58,10 +59,11 @@ pub async fn search_albums(query: String, limit: i64) -> Result, Serv /// # Returns /// A Result containing a vector of artists if the search was successful, or an error if the search failed #[server(endpoint = "search_artists")] -pub async fn search_artists(query: String, limit: i64) -> Result, ServerFnError> { +pub async fn search_artists(query: String, limit: i64) -> Result, ServerFnError> { use crate::schema::artists::dsl::*; Ok(artists + .select((artists::all_columns(), trgm_distance(name, query.clone()))) .filter(trgm_similar(name, query.clone())) .order_by(trgm_distance(name, query)) .limit(limit) @@ -77,10 +79,11 @@ pub async fn search_artists(query: String, limit: i64) -> Result, Se /// # Returns /// A Result containing a vector of songs if the search was successful, or an error if the search failed #[server(endpoint = "search_songs")] -pub async fn search_songs(query: String, limit: i64) -> Result, ServerFnError> { +pub async fn search_songs(query: String, limit: i64) -> Result, ServerFnError> { use crate::schema::songs::dsl::*; Ok(songs + .select((songs::all_columns(), trgm_distance(title, query.clone()))) .filter(trgm_similar(title, query.clone())) .order_by(trgm_distance(title, query)) .limit(limit) @@ -95,9 +98,10 @@ pub async fn search_songs(query: String, limit: i64) -> Result, Server /// `limit` - The maximum number of results to return for each type /// /// # Returns -/// A Result containing a tuple of vectors of albums, artists, and songs if the search was successful, +/// A Result containing a tuple of vectors of albums, artists, and songs, +/// along with respective similarity scores, if the search was successful. #[server(endpoint = "search")] -pub async fn search(query: String, limit: i64) -> Result<(Vec, Vec, Vec), ServerFnError> { +pub async fn search(query: String, limit: i64) -> Result<(Vec<(Album, f32)>, Vec<(Artist, f32)>, Vec<(Song, f32)>), ServerFnError> { let albums = search_albums(query.clone(), limit); let artists = search_artists(query.clone(), limit); let songs = search_songs(query.clone(), limit);