Modify search functions to include distance for each result
This commit is contained in:
parent
9c1c8a6a1e
commit
4681c3ff22
@ -39,10 +39,11 @@ if #[cfg(feature = "ssr")] {
|
|||||||
/// # Returns
|
/// # Returns
|
||||||
/// A Result containing a vector of albums if the search was successful, or an error if the search failed
|
/// A Result containing a vector of albums if the search was successful, or an error if the search failed
|
||||||
#[server(endpoint = "search_albums")]
|
#[server(endpoint = "search_albums")]
|
||||||
pub async fn search_albums(query: String, limit: i64) -> Result<Vec<Album>, ServerFnError> {
|
pub async fn search_albums(query: String, limit: i64) -> Result<Vec<(Album, f32)>, ServerFnError> {
|
||||||
use crate::schema::albums::dsl::*;
|
use crate::schema::albums::dsl::*;
|
||||||
|
|
||||||
Ok(albums
|
Ok(albums
|
||||||
|
.select((albums::all_columns(), trgm_distance(title, query.clone())))
|
||||||
.filter(trgm_similar(title, query.clone()))
|
.filter(trgm_similar(title, query.clone()))
|
||||||
.order_by(trgm_distance(title, query))
|
.order_by(trgm_distance(title, query))
|
||||||
.limit(limit)
|
.limit(limit)
|
||||||
@ -58,10 +59,11 @@ pub async fn search_albums(query: String, limit: i64) -> Result<Vec<Album>, Serv
|
|||||||
/// # Returns
|
/// # Returns
|
||||||
/// A Result containing a vector of artists if the search was successful, or an error if the search failed
|
/// A Result containing a vector of artists if the search was successful, or an error if the search failed
|
||||||
#[server(endpoint = "search_artists")]
|
#[server(endpoint = "search_artists")]
|
||||||
pub async fn search_artists(query: String, limit: i64) -> Result<Vec<Artist>, ServerFnError> {
|
pub async fn search_artists(query: String, limit: i64) -> Result<Vec<(Artist, f32)>, ServerFnError> {
|
||||||
use crate::schema::artists::dsl::*;
|
use crate::schema::artists::dsl::*;
|
||||||
|
|
||||||
Ok(artists
|
Ok(artists
|
||||||
|
.select((artists::all_columns(), trgm_distance(name, query.clone())))
|
||||||
.filter(trgm_similar(name, query.clone()))
|
.filter(trgm_similar(name, query.clone()))
|
||||||
.order_by(trgm_distance(name, query))
|
.order_by(trgm_distance(name, query))
|
||||||
.limit(limit)
|
.limit(limit)
|
||||||
@ -77,10 +79,11 @@ pub async fn search_artists(query: String, limit: i64) -> Result<Vec<Artist>, Se
|
|||||||
/// # Returns
|
/// # Returns
|
||||||
/// A Result containing a vector of songs if the search was successful, or an error if the search failed
|
/// A Result containing a vector of songs if the search was successful, or an error if the search failed
|
||||||
#[server(endpoint = "search_songs")]
|
#[server(endpoint = "search_songs")]
|
||||||
pub async fn search_songs(query: String, limit: i64) -> Result<Vec<Song>, ServerFnError> {
|
pub async fn search_songs(query: String, limit: i64) -> Result<Vec<(Song, f32)>, ServerFnError> {
|
||||||
use crate::schema::songs::dsl::*;
|
use crate::schema::songs::dsl::*;
|
||||||
|
|
||||||
Ok(songs
|
Ok(songs
|
||||||
|
.select((songs::all_columns(), trgm_distance(title, query.clone())))
|
||||||
.filter(trgm_similar(title, query.clone()))
|
.filter(trgm_similar(title, query.clone()))
|
||||||
.order_by(trgm_distance(title, query))
|
.order_by(trgm_distance(title, query))
|
||||||
.limit(limit)
|
.limit(limit)
|
||||||
@ -95,9 +98,10 @@ pub async fn search_songs(query: String, limit: i64) -> Result<Vec<Song>, Server
|
|||||||
/// `limit` - The maximum number of results to return for each type
|
/// `limit` - The maximum number of results to return for each type
|
||||||
///
|
///
|
||||||
/// # Returns
|
/// # 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")]
|
#[server(endpoint = "search")]
|
||||||
pub async fn search(query: String, limit: i64) -> Result<(Vec<Album>, Vec<Artist>, Vec<Song>), 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 albums = search_albums(query.clone(), limit);
|
||||||
let artists = search_artists(query.clone(), limit);
|
let artists = search_artists(query.clone(), limit);
|
||||||
let songs = search_songs(query.clone(), limit);
|
let songs = search_songs(query.clone(), limit);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user