]> cat aescling's git repositories - mastodon.git/commitdiff
Change hashtag search to only return results that have trended in the past (#11448)
authorEugen Rochko <eugen@zeonfederated.com>
Tue, 30 Jul 2019 18:29:50 +0000 (20:29 +0200)
committerGitHub <noreply@github.com>
Tue, 30 Jul 2019 18:29:50 +0000 (20:29 +0200)
* 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

app/models/tag.rb
spec/models/tag_spec.rb

index a2d6078f4bb635f2d9e716225e344d99c2ed1158..c7f0af86df15b2e95b48d678fd641b80857e8066 100644 (file)
@@ -76,9 +76,11 @@ class Tag < ApplicationRecord
     end
 
     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'))
          .limit(limit)
          .offset(offset)
index 5f07fd618e5a6d1444d36e2c4dc76fce68dc2da6..9d700849bb872258b38b98437ab56719a2753535 100644 (file)
@@ -136,8 +136,8 @@ RSpec.describe Tag, type: :model do
     end
 
     it 'finds the exact matching tag as the first item' do
-      similar_tag = Fabricate(:tag, name: "matchlater")
-      tag = Fabricate(:tag, name: "match")
+      similar_tag = Fabricate(:tag, name: "matchlater", score: 1)
+      tag = Fabricate(:tag, name: "match", score: 1)
 
       results = Tag.search_for("match")