Skip to content

Commit cb875d1

Browse files
authored
controllers/krate/search: Use provided plainto_tsquery_with_search_config() fn (#10411)
Instead of hand-crafting SQL ourselves, this leverages the defined `plainto_tsquery_with_search_config()` function from the `diesel_full_text_search`.
1 parent 3ffba1e commit cb875d1

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

src/controllers/krate/search.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ use axum_extra::extract::Query;
66
use axum_extra::json;
77
use axum_extra::response::ErasedJson;
88
use derive_more::Deref;
9-
use diesel::dsl::{exists, sql, InnerJoinQuerySource, LeftJoinQuerySource};
9+
use diesel::dsl::{exists, InnerJoinQuerySource, LeftJoinQuerySource};
1010
use diesel::prelude::*;
11-
use diesel::sql_types::{Bool, Text};
11+
use diesel::sql_types::Bool;
1212
use diesel_async::{AsyncPgConnection, RunQueryDsl};
13-
use diesel_full_text_search::*;
13+
use diesel_full_text_search::{configuration::TsConfigurationByName, *};
1414
use http::request::Parts;
1515
use tracing::Instrument;
1616
use utoipa::IntoParams;
@@ -102,16 +102,15 @@ pub async fn list_crates(
102102
query = query.order(Crate::with_name(q_string).desc());
103103

104104
if sort == "relevance" {
105-
let q = sql::<TsQuery>("plainto_tsquery('english', ")
106-
.bind::<Text, _>(q_string)
107-
.sql(")");
105+
let q =
106+
plainto_tsquery_with_search_config(TsConfigurationByName("english"), q_string);
108107
let rank = ts_rank_cd(crates::textsearchable_index_col, q);
109108
query = query.select((
110109
ALL_COLUMNS,
111110
Crate::with_name(q_string),
112111
crate_downloads::downloads,
113112
recent_crate_downloads::downloads.nullable(),
114-
rank.clone(),
113+
rank,
115114
versions::num.nullable(),
116115
versions::yanked.nullable(),
117116
));
@@ -359,9 +358,10 @@ impl FilterParams {
359358

360359
if let Some(q_string) = &self.q_string {
361360
if !q_string.is_empty() {
362-
let q = sql::<TsQuery>("plainto_tsquery('english', ")
363-
.bind::<Text, _>(q_string.as_str())
364-
.sql(")");
361+
let q = plainto_tsquery_with_search_config(
362+
TsConfigurationByName("english"),
363+
q_string.as_str(),
364+
);
365365
query = query.filter(
366366
q.matches(crates::textsearchable_index_col)
367367
.or(Crate::loosly_matches_name(q_string.as_str())),
@@ -590,16 +590,17 @@ impl FilterParams {
590590
// ORDER BY exact_match DESC, rank DESC, name ASC
591591
// ```
592592
let q_string = self.q_string.as_ref().expect("q_string should not be None");
593-
let q = sql::<TsQuery>("plainto_tsquery('english', ")
594-
.bind::<Text, _>(q_string.as_str())
595-
.sql(")");
593+
let q = plainto_tsquery_with_search_config(
594+
TsConfigurationByName("english"),
595+
q_string.as_str(),
596+
);
596597
let rank = ts_rank_cd(crates::textsearchable_index_col, q);
597598
let name_exact_match = Crate::with_name(q_string.as_str());
598599
vec![
599600
Box::new(
600601
name_exact_match
601602
.eq(exact)
602-
.and(rank.clone().eq(rank_in))
603+
.and(rank.eq(rank_in))
603604
.and(crates::name.nullable().gt(crate_name_by_id(id)))
604605
.nullable(),
605606
),

0 commit comments

Comments
 (0)