search in admin also
This commit is contained in:
@@ -37,10 +37,13 @@ impl Entity {
|
||||
/// `f_unaccent`, so diacritics never matter. Results are ranked by full-text
|
||||
/// rank, then trigram closeness of the name, then recency. An empty/blank
|
||||
/// query returns nothing — callers fall back to the plain listing.
|
||||
/// `published_only` filters to the storefront-visible set; pass `false` for
|
||||
/// admin tools that also need to find drafts.
|
||||
pub async fn search<C: ConnectionTrait>(
|
||||
db: &C,
|
||||
query: &str,
|
||||
limit: u64,
|
||||
published_only: bool,
|
||||
) -> Result<Vec<Model>, DbErr> {
|
||||
let q = query.trim();
|
||||
if q.is_empty() {
|
||||
@@ -50,12 +53,13 @@ impl Entity {
|
||||
// Only the model's own columns are selected; the generated `search_vector`
|
||||
// is left out so the row maps cleanly back onto `Model`. `$1` is reused
|
||||
// for every occurrence of the query term; `$2` caps the result set.
|
||||
let sql = r#"
|
||||
let published_clause = if published_only { "p.published = TRUE AND" } else { "" };
|
||||
let sql = format!(
|
||||
r#"
|
||||
SELECT p.created_at, p.updated_at, p.id, p.name, p.slug, p.description,
|
||||
p.currency, p.view_count, p.published, p.published_at, p.category_id
|
||||
FROM products p
|
||||
WHERE p.published = TRUE
|
||||
AND (
|
||||
WHERE {published_clause} (
|
||||
p.search_vector @@ websearch_to_tsquery('sk_unaccent', $1)
|
||||
OR word_similarity(f_unaccent($1), f_unaccent(p.name)) > 0.3
|
||||
OR word_similarity(f_unaccent($1), f_unaccent(COALESCE(p.description, ''))) > 0.3
|
||||
@@ -65,12 +69,13 @@ impl Entity {
|
||||
word_similarity(f_unaccent($1), f_unaccent(p.name)) DESC,
|
||||
p.published_at DESC NULLS LAST
|
||||
LIMIT $2
|
||||
"#;
|
||||
"#
|
||||
);
|
||||
|
||||
Entity::find()
|
||||
.from_raw_sql(Statement::from_sql_and_values(
|
||||
db.get_database_backend(),
|
||||
sql,
|
||||
&sql,
|
||||
[q.into(), (limit as i64).into()],
|
||||
))
|
||||
.all(db)
|
||||
|
||||
Reference in New Issue
Block a user