fix(search): support partial search, flexible tokens, and relevance ranking (issue #28)
This commit is contained in:
parent
0d9c48c6b5
commit
fd5e86b56f
|
|
@ -673,9 +673,105 @@ class SearchRepository:
|
||||||
|
|
||||||
score_expr = "0.0"
|
score_expr = "0.0"
|
||||||
if query:
|
if query:
|
||||||
params["search_query"] = query
|
clean_query = query.strip()
|
||||||
where.append("csd.search_vector @@ websearch_to_tsquery('simple', :search_query)")
|
params["raw_query"] = clean_query
|
||||||
score_expr = "ts_rank_cd(csd.search_vector, websearch_to_tsquery('simple', :search_query))"
|
params["like_query"] = f"%{clean_query}%"
|
||||||
|
params["prefix_like"] = f"{clean_query}%"
|
||||||
|
|
||||||
|
tokens = re.findall(r"\w+", clean_query)
|
||||||
|
if tokens:
|
||||||
|
all_tokens = list(dict.fromkeys(tokens))
|
||||||
|
stem_tokens = set(all_tokens)
|
||||||
|
for t in all_tokens:
|
||||||
|
root = re.sub(r"(ies|y|ical|ic|s|ed|ing)$", "", t, flags=re.IGNORECASE)
|
||||||
|
if len(root) >= 3:
|
||||||
|
stem_tokens.add(root)
|
||||||
|
|
||||||
|
and_prefix = " & ".join(f"{t}:*" for t in all_tokens)
|
||||||
|
or_prefix = " | ".join(f"{t}:*" for t in all_tokens)
|
||||||
|
params["and_prefix_query"] = and_prefix
|
||||||
|
params["or_prefix_query"] = or_prefix
|
||||||
|
|
||||||
|
tok_conditions: list[str] = []
|
||||||
|
tok_score_parts: list[str] = []
|
||||||
|
for idx, tok in enumerate(list(stem_tokens)[:8]):
|
||||||
|
p_name = f"tok_{idx}"
|
||||||
|
params[p_name] = f"%{tok}%"
|
||||||
|
tok_conditions.append(
|
||||||
|
f"(ci.title ilike :{p_name} or csd.search_text ilike :{p_name} or exists ("
|
||||||
|
f"select 1 from {self.schema}.module_contents mc_tok "
|
||||||
|
f"join {self.schema}.modules m_tok on m_tok.id = mc_tok.module_id "
|
||||||
|
f"where mc_tok.content_id = ci.id and m_tok.is_active = true and ("
|
||||||
|
f"m_tok.name ilike :{p_name} or m_tok.code ilike :{p_name})"
|
||||||
|
f"))"
|
||||||
|
)
|
||||||
|
tok_score_parts.append(
|
||||||
|
f"(case when ci.title ilike :{p_name} then 6.0 else 0.0 end) + "
|
||||||
|
f"(case when exists (select 1 from {self.schema}.module_contents mc_s "
|
||||||
|
f"join {self.schema}.modules m_s on m_s.id = mc_s.module_id "
|
||||||
|
f"where mc_s.content_id = ci.id and m_s.is_active = true and ("
|
||||||
|
f"m_s.name ilike :{p_name} or m_s.code ilike :{p_name})) then 4.0 else 0.0 end)"
|
||||||
|
)
|
||||||
|
|
||||||
|
combined_tok_cond = " or ".join(tok_conditions)
|
||||||
|
tok_score_sql = " + ".join(tok_score_parts) if tok_score_parts else "0.0"
|
||||||
|
|
||||||
|
where.append(
|
||||||
|
f"""(
|
||||||
|
csd.search_vector @@ to_tsquery('simple', :and_prefix_query)
|
||||||
|
or csd.search_vector @@ to_tsquery('simple', :or_prefix_query)
|
||||||
|
or csd.search_vector @@ websearch_to_tsquery('simple', :raw_query)
|
||||||
|
or csd.search_text ilike :like_query
|
||||||
|
or ci.title ilike :like_query
|
||||||
|
or exists (
|
||||||
|
select 1
|
||||||
|
from {self.schema}.module_contents mc_mod
|
||||||
|
join {self.schema}.modules m_mod on m_mod.id = mc_mod.module_id
|
||||||
|
where mc_mod.content_id = ci.id
|
||||||
|
and m_mod.is_active = true
|
||||||
|
and (m_mod.name ilike :like_query or m_mod.code ilike :like_query)
|
||||||
|
)
|
||||||
|
or ({combined_tok_cond})
|
||||||
|
)"""
|
||||||
|
)
|
||||||
|
score_expr = f"""(
|
||||||
|
(case when ci.title ilike :raw_query then 100.0
|
||||||
|
when ci.title ilike :prefix_like then 50.0
|
||||||
|
when ci.title ilike :like_query then 25.0
|
||||||
|
else 0.0 end) +
|
||||||
|
{tok_score_sql} +
|
||||||
|
coalesce(ts_rank_cd(csd.search_vector, to_tsquery('simple', :and_prefix_query)), 0.0) * 10.0 +
|
||||||
|
coalesce(ts_rank_cd(csd.search_vector, to_tsquery('simple', :or_prefix_query)), 0.0) * 2.0 +
|
||||||
|
(case when exists (
|
||||||
|
select 1
|
||||||
|
from {self.schema}.module_contents mc_mod
|
||||||
|
join {self.schema}.modules m_mod on m_mod.id = mc_mod.module_id
|
||||||
|
where mc_mod.content_id = ci.id
|
||||||
|
and m_mod.is_active = true
|
||||||
|
and (m_mod.name ilike :like_query or m_mod.code ilike :like_query)
|
||||||
|
) then 15.0 else 0.0 end)
|
||||||
|
)"""
|
||||||
|
else:
|
||||||
|
where.append(
|
||||||
|
f"""(
|
||||||
|
csd.search_text ilike :like_query
|
||||||
|
or ci.title ilike :like_query
|
||||||
|
or exists (
|
||||||
|
select 1
|
||||||
|
from {self.schema}.module_contents mc_mod
|
||||||
|
join {self.schema}.modules m_mod on m_mod.id = mc_mod.module_id
|
||||||
|
where mc_mod.content_id = ci.id
|
||||||
|
and m_mod.is_active = true
|
||||||
|
and (m_mod.name ilike :like_query or m_mod.code ilike :like_query)
|
||||||
|
)
|
||||||
|
)"""
|
||||||
|
)
|
||||||
|
score_expr = f"""(
|
||||||
|
case when ci.title ilike :raw_query then 100.0
|
||||||
|
when ci.title ilike :prefix_like then 50.0
|
||||||
|
when ci.title ilike :like_query then 25.0
|
||||||
|
else 1.0 end
|
||||||
|
)"""
|
||||||
|
|
||||||
direction = "asc" if sort_direction == "asc" else "desc"
|
direction = "asc" if sort_direction == "asc" else "desc"
|
||||||
if sort_field == "title":
|
if sort_field == "title":
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue