Change hashtag search to only return results that have trended in the past (#11448)
* Change hashtag search to only return results that have trended in the past A way to eliminate typos and other one-off "junk" results * Fix excluding exact matches that don't have a score * Fix tests
This commit is contained in:
parent
ff789a751a
commit
92de439c04
@ -76,9 +76,11 @@ class Tag < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def search_for(term, limit = 5, offset = 0)
|
def search_for(term, limit = 5, offset = 0)
|
||||||
pattern = sanitize_sql_like(normalize(term.strip)) + '%'
|
normalized_term = normalize(term.strip).mb_chars.downcase.to_s
|
||||||
|
pattern = sanitize_sql_like(normalized_term) + '%'
|
||||||
|
|
||||||
Tag.where(arel_table[:name].lower.matches(pattern.mb_chars.downcase.to_s))
|
Tag.where(arel_table[:name].lower.matches(pattern))
|
||||||
|
.where(arel_table[:score].gt(0).or(arel_table[:name].lower.eq(normalized_term)))
|
||||||
.order(Arel.sql('length(name) ASC, score DESC, name ASC'))
|
.order(Arel.sql('length(name) ASC, score DESC, name ASC'))
|
||||||
.limit(limit)
|
.limit(limit)
|
||||||
.offset(offset)
|
.offset(offset)
|
||||||
|
@ -136,8 +136,8 @@ RSpec.describe Tag, type: :model do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it 'finds the exact matching tag as the first item' do
|
it 'finds the exact matching tag as the first item' do
|
||||||
similar_tag = Fabricate(:tag, name: "matchlater")
|
similar_tag = Fabricate(:tag, name: "matchlater", score: 1)
|
||||||
tag = Fabricate(:tag, name: "match")
|
tag = Fabricate(:tag, name: "match", score: 1)
|
||||||
|
|
||||||
results = Tag.search_for("match")
|
results = Tag.search_for("match")
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user