0
0
Fork 0

Add hashtag score for better sorting of autosuggestions (#11427)

* Add hashtag score for better sorting of autosuggestions

* Do not use `~<~` operator with no text_pattern_ops index
This commit is contained in:
Eugen Rochko 2019-07-30 13:10:40 +02:00 committed by GitHub
parent b31b232edf
commit 648cdbc04a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 5 deletions

View file

@ -7,6 +7,7 @@
# name :string default(""), not null
# created_at :datetime not null
# updated_at :datetime not null
# score :integer
#
class Tag < ApplicationRecord
@ -78,7 +79,7 @@ class Tag < ApplicationRecord
pattern = sanitize_sql_like(normalize(term.strip)) + '%'
Tag.where(arel_table[:name].lower.matches(pattern.mb_chars.downcase.to_s))
.order(:name)
.order(Arel.sql('length(name) ASC, score DESC, name ASC'))
.limit(limit)
.offset(offset)
end

View file

@ -48,12 +48,17 @@ class TrendingTags
redis.zrem(key, tag_id.to_s)
else
score = ((observed - expected)**2) / expected
redis.zadd(key, score, tag_id.to_s)
added = redis.zadd(key, score, tag_id.to_s)
bump_tag_score!(tag_id) if added == 1
end
redis.expire(key, EXPIRE_TRENDS_AFTER)
end
def bump_tag_score!(tag_id)
Tag.where(id: tag_id).update_all('score = COALESCE(score, 0) + 1')
end
def disallowed_hashtags
return @disallowed_hashtags if defined?(@disallowed_hashtags)