Skip to content

Commit b5bb9c5

Browse files
committed
controllers/krate/search: Use provided plainto_tsquery_with_search_config() fn
Instead of hand-crafting SQL ourselves, this leverages the defined `plainto_tsquery_with_search_config()` function from the `diesel_full_text_search`.
1 parent e3367c7 commit b5bb9c5

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/controllers/krate/search.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ use axum::extract::FromRequestParts;
55
use axum_extra::extract::Query;
66
use axum_extra::json;
77
use axum_extra::response::ErasedJson;
8+
use configuration::TsConfigurationByName;
89
use derive_more::Deref;
9-
use diesel::dsl::{exists, sql, InnerJoinQuerySource, LeftJoinQuerySource};
10+
use diesel::dsl::{exists, InnerJoinQuerySource, LeftJoinQuerySource};
1011
use diesel::prelude::*;
11-
use diesel::sql_types::{Bool, Text};
12+
use diesel::sql_types::Bool;
1213
use diesel_async::{AsyncPgConnection, RunQueryDsl};
1314
use diesel_full_text_search::*;
1415
use http::request::Parts;
@@ -102,16 +103,15 @@ pub async fn list_crates(
102103
query = query.order(Crate::with_name(q_string).desc());
103104

104105
if sort == "relevance" {
105-
let q = sql::<TsQuery>("plainto_tsquery('english', ")
106-
.bind::<Text, _>(q_string)
107-
.sql(")");
106+
let q =
107+
plainto_tsquery_with_search_config(TsConfigurationByName("english"), q_string);
108108
let rank = ts_rank_cd(crates::textsearchable_index_col, q);
109109
query = query.select((
110110
ALL_COLUMNS,
111111
Crate::with_name(q_string),
112112
crate_downloads::downloads,
113113
recent_crate_downloads::downloads.nullable(),
114-
rank.clone(),
114+
rank,
115115
versions::num.nullable(),
116116
versions::yanked.nullable(),
117117
));
@@ -359,9 +359,10 @@ impl FilterParams {
359359

360360
if let Some(q_string) = &self.q_string {
361361
if !q_string.is_empty() {
362-
let q = sql::<TsQuery>("plainto_tsquery('english', ")
363-
.bind::<Text, _>(q_string.as_str())
364-
.sql(")");
362+
let q = plainto_tsquery_with_search_config(
363+
TsConfigurationByName("english"),
364+
q_string.as_str(),
365+
);
365366
query = query.filter(
366367
q.matches(crates::textsearchable_index_col)
367368
.or(Crate::loosly_matches_name(q_string.as_str())),
@@ -590,16 +591,17 @@ impl FilterParams {
590591
// ORDER BY exact_match DESC, rank DESC, name ASC
591592
// ```
592593
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(")");
594+
let q = plainto_tsquery_with_search_config(
595+
TsConfigurationByName("english"),
596+
q_string.as_str(),
597+
);
596598
let rank = ts_rank_cd(crates::textsearchable_index_col, q);
597599
let name_exact_match = Crate::with_name(q_string.as_str());
598600
vec![
599601
Box::new(
600602
name_exact_match
601603
.eq(exact)
602-
.and(rank.clone().eq(rank_in))
604+
.and(rank.eq(rank_in))
603605
.and(crates::name.nullable().gt(crate_name_by_id(id)))
604606
.nullable(),
605607
),

0 commit comments

Comments
 (0)