This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Mod: 搜索改进,临时签入

  • Loading branch information...
huacnlee committed Apr 14, 2011
1 parent 8dff887 commit e759da3f5bd65a8b564e5fbf16b8389831244484
Showing with 54 additions and 7 deletions.
  1. +4 −4 app/controllers/search_controller.rb
  2. +2 −2 app/models/ask.rb
  3. +16 −0 app/models/topic.rb
  4. +9 −1 app/models/user.rb
  5. +23 −0 public/javascripts/asks.js
@@ -14,13 +14,13 @@ def index
end end
def topics def topics
result = Search.query(params[:q],:type => "Topic",:limit => 10) result = Topic.search(params[:q], :limit => 10).items
if params[:format] == "json" if params[:format] == "json"
render :json => result.to_json render :json => result.to_json
else else
lines = [] lines = []
result.each do |item| result.each do |item|
lines << item['title'] lines << "#{item[:title]}#!##{item[:followers_count]}#!##{item[:cover_small]}"
end end
render :text => lines.join("\n") render :text => lines.join("\n")
end end
@@ -40,13 +40,13 @@ def asks
end end
def users def users
result = Search.query(params[:q],:type => "User",:limit => 10) result = User.search(params[:q],:limit => 10).items
if params[:format] == "json" if params[:format] == "json"
render :json => result.to_json render :json => result.to_json
else else
lines = [] lines = []
result.each do |item| result.each do |item|
lines << "#{item['title']}#!##{item['id']}#!##{item['tagline']}#!##{item['avatar_small']}#!##{item['slug']}" lines << "#{item[:name]}#!##{item[:id]}#!##{item[:tagline]}#!##{item[:avatar_small]}#!##{item[:slug]}"
end end
render :text => lines.join("\n") render :text => lines.join("\n")
end end
View
@@ -57,8 +57,8 @@ class Ask
scope :only_ids, lambda { |id_array| any_in("_id" => (id_array ||= [])) } scope :only_ids, lambda { |id_array| any_in("_id" => (id_array ||= [])) }
# FullText indexes # FullText indexes
search_index(:fields => [:title,:body, :topics], search_index(:fields => [:title,:topics],
:attributes => [], :attributes => [:title,:topics],
:options => {} ) :options => {} )
redis_search_index(:title_field => :title,:ext_fields => [:topics]) redis_search_index(:title_field => :title,:ext_fields => [:topics])
View
@@ -1,6 +1,7 @@
# coding: utf-8 # coding: utf-8
class Topic class Topic
include Mongoid::Document include Mongoid::Document
include Mongoid::Sphinx
include BaseModel include BaseModel
attr_accessor :current_user_id, :cover_changed attr_accessor :current_user_id, :cover_changed
@@ -20,6 +21,21 @@ class Topic
validates_presence_of :name validates_presence_of :name
validates_uniqueness_of :name, :case_insensitive => true validates_uniqueness_of :name, :case_insensitive => true
# 以下两个方法是给 redis search index 用
def followers_count
self.follower_ids.count
end
def cover_small
self.cover.small.url
end
# FullText indexes
search_index(:fields => [:name],
:attributes => [:name,:cover_small, :followers_count],
:attribute_types => {:cover_small => String, :followers_count => Integer},
:options => {} )
redis_search_index(:title_field => :name) redis_search_index(:title_field => :name)
# 敏感词验证 # 敏感词验证
View
@@ -3,6 +3,7 @@ class User
include Mongoid::Document include Mongoid::Document
include Mongoid::Timestamps include Mongoid::Timestamps
include Mongoid::Voter include Mongoid::Voter
include Mongoid::Sphinx
include BaseModel include BaseModel
devise :invitable, :database_authenticatable, :registerable, devise :invitable, :database_authenticatable, :registerable,
@@ -67,7 +68,14 @@ def avatar_small
def avatar_small_changed? def avatar_small_changed?
self.avatar_changed? self.avatar_changed?
end end
redis_search_index(:title_field => :name, :ext_fields => [:slug,:avatar_small,:tagline])
# FullText indexes
search_index(:fields => [:name,:slug],
:attributes => [:name,:slug,:avatar_small,:tagline],
:attribute_types => {:avatar_small => String, :tagline => String},
:options => {} )
redis_search_index(:title_field => :name,
:ext_fields => [:slug,:avatar_small,:tagline])
# 敏感词验证 # 敏感词验证
before_validation :check_spam_words before_validation :check_spam_words
View
@@ -144,6 +144,9 @@ var Asks = {
minChars: 1, minChars: 1,
width: 200, width: 200,
scroll : false, scroll : false,
formatItem : function(data, i, total){
return Asks.completeLineTopic(data,false);
}
}); });
}, },
@@ -199,6 +202,26 @@ var Asks = {
$.get("/asks/"+ask_id+"/invite_to_answer.js",{ user_id : user_id, drop : is_drop }); $.get("/asks/"+ask_id+"/invite_to_answer.js",{ user_id : user_id, drop : is_drop });
}, },
completeLineTopic : function(data,allow_link){
html = "";
cover = data[2];
if(/http:\/\//.test(cover) == false){
avatar = "/images/" + cover;
}
count = data[1];
html += '<img class="avatar" src="'+ cover +'" />';
html += '<div class="uinfo"><p>';
if(allow_link == true){
html += '<a href="/topics/'+data[0]+'">'+data[0]+'</a>';
}
else{
html += '<span class="name">'+data[0]+'</span>';
}
html += '</p>';
html += '<p class="count">'+count+' 个关注者</p></div>';
return html;
},
completeLineUser : function(data,allow_link){ completeLineUser : function(data,allow_link){
html = ""; html = "";
avatar = data[3]; avatar = data[3];

0 comments on commit e759da3

Please sign in to comment.