Skip to content

Commit

Permalink
Sort for search results
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Sep 25, 2011
1 parent c97a62e commit 8599e4b
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ gem "juggernaut"
# Diff 内容并输出 HTML 格式
gem "htmldiff", :git => "git://github.com/huacnlee/htmldiff.git"

gem "redis-search", "0.4"
gem "redis-search", "0.5"

group :development do
gem 'rails-dev-boost-beta','0.1.2', :require => 'rails_development_boost'
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ GEM
redis (2.1.1)
redis-namespace (1.0.3)
redis (< 3.0.0)
redis-search (0.4)
redis-search (0.5)
redis (>= 2.1.1)
rmmseg-cpp-huacnlee (~> 0.2.8)
resque (1.15.0)
Expand Down Expand Up @@ -243,7 +243,7 @@ DEPENDENCIES
rails (= 3.0.5)
rails-dev-boost-beta (= 0.1.2)
redis (= 2.1.1)
redis-search (= 0.4)
redis-search (= 0.5)
resque (= 1.15.0)
resque_mailer (= 1.0.1)
rmmseg-cpp-huacnlee (= 0.2.8)
Expand Down
8 changes: 7 additions & 1 deletion app/models/ask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ class Ask
# 问我的问题
scope :asked_to, lambda { |to_user_id| where(:to_user_id => to_user_id) }

redis_search_index(:title_field => :title,:ext_fields => [:topics])
redis_search_index(:title_field => :title,
:score_field => :score,
:ext_fields => [:topics])

before_save :fill_default_values
after_create :create_log, :inc_counter_cache, :send_mails
Expand All @@ -70,6 +72,10 @@ class Ask
def view!
self.inc(:views_count, 1)
end

def score
self.comments_count + (self.answers_count * 3) + (self.follower_ids.count * 2)
end

def send_mails
# 向某人提问
Expand Down
3 changes: 2 additions & 1 deletion app/models/topic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def cover_small_changed?

redis_search_index(:title_field => :name,
:prefix_index_enable => true,
:ext_fields => [:followers_count,:cover_small])
:score_field => :followers_count,
:ext_fields => [:followers_count,:asks_count,:cover_small])

# 敏感词验证
before_validation :check_spam_words
Expand Down
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def score_changed?

redis_search_index(:title_field => :name,
:prefix_index_enable => true,
:score_field => :score,
:ext_fields => [:id, :slug,:avatar_small,:tagline, :score])

# 敏感词验证
Expand Down

0 comments on commit 8599e4b

Please sign in to comment.