Skip to content

Commit

Permalink
Fix the issue of search not working with spaces in them (#8052)
Browse files Browse the repository at this point in the history
  • Loading branch information
eth3lbert committed Feb 2, 2024
1 parent 179351a commit d53a93f
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/controllers/krate/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::auth::AuthCheck;
use diesel::dsl::*;
use diesel::sql_types::Array;
use diesel::sql_types::{Array, Text};
use diesel_full_text_search::*;
use indexmap::IndexMap;
use once_cell::sync::OnceCell;
Expand Down Expand Up @@ -98,10 +98,9 @@ pub async fn search(app: AppState, req: Parts) -> AppResult<Json<Value>> {
query = query.order(Crate::with_name(q_string).desc());

if sort == "relevance" {
let q = to_tsquery_with_search_config(
configuration::TsConfigurationByName("english"),
q_string,
);
let q = sql::<TsQuery>("plainto_tsquery('english', ")
.bind::<Text, _>(q_string)
.sql(")");
let rank = ts_rank_cd(crates::textsearchable_index_col, q);
query = query.then_order_by(rank.desc())
}
Expand Down Expand Up @@ -305,15 +304,13 @@ impl<'a> FilterParams<'a> {
req: &Parts,
conn: &mut PgConnection,
) -> AppResult<crates::BoxedQuery<'a, diesel::pg::Pg>> {
use diesel::sql_types::Text;
let mut query = crates::table.into_boxed();

if let Some(q_string) = self.q_string {
if !q_string.is_empty() {
let q = to_tsquery_with_search_config(
configuration::TsConfigurationByName("english"),
q_string,
);
let q = sql::<TsQuery>("plainto_tsquery('english', ")
.bind::<Text, _>(q_string)
.sql(")");
query = query.filter(
q.matches(crates::textsearchable_index_col)
.or(Crate::loosly_matches_name(q_string)),
Expand Down

0 comments on commit d53a93f

Please sign in to comment.