@@ -22,6 +22,8 @@ use axum::{
2222use base64:: { engine:: general_purpose:: STANDARD as b64, Engine } ;
2323use chrono:: { DateTime , Utc } ;
2424use futures_util:: stream:: TryStreamExt ;
25+ use once_cell:: sync:: Lazy ;
26+ use regex:: Regex ;
2527use serde:: { Deserialize , Serialize } ;
2628use sqlx:: Row ;
2729use std:: collections:: { BTreeMap , HashMap , HashSet } ;
@@ -156,7 +158,6 @@ async fn get_search_results(
156158 }
157159
158160 use crate :: utils:: APP_USER_AGENT ;
159- use once_cell:: sync:: Lazy ;
160161 use reqwest:: header:: { HeaderMap , HeaderValue , ACCEPT , USER_AGENT } ;
161162 use reqwest:: Client as HttpClient ;
162163
@@ -498,6 +499,13 @@ impl_axum_webpage! {
498499 status = |search| search. status,
499500}
500501
502+ fn retrive_sort_from_paginate ( query : & str ) -> String {
503+ static RE : Lazy < Regex > = Lazy :: new ( || Regex :: new ( r"[?&]sort=([^&]+)" ) . unwrap ( ) ) ;
504+ let cap = RE . captures ( query) . unwrap ( ) ;
505+ cap. get ( 1 )
506+ . map_or ( "relevance" . to_string ( ) , |v| v. as_str ( ) . to_string ( ) )
507+ }
508+
501509pub ( crate ) async fn search_handler (
502510 mut conn : DbConnection ,
503511 Extension ( pool) : Extension < Pool > ,
@@ -509,7 +517,7 @@ pub(crate) async fn search_handler(
509517 . get ( "query" )
510518 . map ( |q| q. to_string ( ) )
511519 . unwrap_or_else ( || "" . to_string ( ) ) ;
512- let sort_by = params
520+ let mut sort_by = params
513521 . get ( "sort" )
514522 . map ( |q| q. to_string ( ) )
515523 . unwrap_or_else ( || "relevance" . to_string ( ) ) ;
@@ -578,7 +586,7 @@ pub(crate) async fn search_handler(
578586 ) ;
579587 return Err ( AxumNope :: NoResults ) ;
580588 }
581-
589+ sort_by = retrive_sort_from_paginate ( & query_params ) ;
582590 get_search_results ( & mut conn, & config, & query_params) . await ?
583591 } else if !query. is_empty ( ) {
584592 let query_params: String = form_urlencoded:: Serializer :: new ( String :: new ( ) )
0 commit comments