添加对帖子的全文搜索
This commit is contained in:
parent
4c00312f45
commit
ef2ef7111c
|
@ -161,7 +161,7 @@ class WelcomeController < ApplicationController
|
|||
query: @name,
|
||||
type:"most_fields",
|
||||
operator: "or",
|
||||
fields: ['login', 'firstname','lastname','name','description^0.5','filename']
|
||||
fields: ['login', 'firstname','lastname','name','description^0.5','filename','subject','content^0.5']
|
||||
}
|
||||
},
|
||||
highlight: {
|
||||
|
@ -173,10 +173,12 @@ class WelcomeController < ApplicationController
|
|||
lastname: {},
|
||||
name:{},
|
||||
description:{},
|
||||
filename:{}
|
||||
filename:{},
|
||||
subject:{},
|
||||
content:{}
|
||||
}
|
||||
}
|
||||
},[User,Course,Attachment,Project] ).page(params[:page] || 1).per(20).results
|
||||
},[User,Course,Attachment,Project,Memo] ).page(params[:page] || 1).per(20).results
|
||||
when 'user'
|
||||
@users = User.search(@name).page(params[:page] || 1).per(20)
|
||||
when 'project'
|
||||
|
@ -185,6 +187,8 @@ class WelcomeController < ApplicationController
|
|||
@courses = Course.search(@name).page(params[:page] || 1).per(20).results
|
||||
when 'attachment'
|
||||
@attachments = Attachment.search(@name).page(params[:page] || 1).per(20).results
|
||||
when 'memo'
|
||||
@memos = Memo.search(@name).page(params[:page] || 1).per(20).results
|
||||
else
|
||||
@alls = Elasticsearch::Model.search({
|
||||
query: {
|
||||
|
@ -192,7 +196,7 @@ class WelcomeController < ApplicationController
|
|||
query: @name,
|
||||
type:"most_fields",
|
||||
operator: "or",
|
||||
fields: ['login', 'firstname','lastname','name','description^0.5','filename']
|
||||
fields: ['login', 'firstname','lastname','name','description^0.5','filename','subject','content^0.5']
|
||||
}
|
||||
},
|
||||
highlight: {
|
||||
|
@ -204,10 +208,12 @@ class WelcomeController < ApplicationController
|
|||
lastname: {},
|
||||
name:{},
|
||||
description:{},
|
||||
filename:{}
|
||||
filename:{},
|
||||
subject:{},
|
||||
content:{}
|
||||
}
|
||||
}
|
||||
},[User,Course,Attachment,Project] ).page(params[:page] || 1).per(20).results
|
||||
},[User,Course,Attachment,Project,Memo] ).page(params[:page] || 1).per(20).results
|
||||
|
||||
end
|
||||
|
||||
|
@ -216,13 +222,14 @@ class WelcomeController < ApplicationController
|
|||
@course_count = Course.search(@name).results.total
|
||||
@attach_count = Attachment.search(@name).results.total
|
||||
@project_count = Project.search(@name).results.total
|
||||
@memo_count = Memo.search(@name).results.total
|
||||
@total_count = Elasticsearch::Model.search({
|
||||
query: {
|
||||
multi_match: {
|
||||
query: @name,
|
||||
type:"most_fields",
|
||||
operator: "or",
|
||||
fields: ['login', 'firstname','lastname','name','description^0.5','filename']
|
||||
fields: ['login', 'firstname','lastname','name','description^0.5','filename','subject','content^0.5']
|
||||
}
|
||||
},
|
||||
highlight: {
|
||||
|
@ -234,10 +241,12 @@ class WelcomeController < ApplicationController
|
|||
lastname: {},
|
||||
name:{},
|
||||
description:{},
|
||||
filename:{}
|
||||
filename:{},
|
||||
subject:{},
|
||||
content:{}
|
||||
}
|
||||
}
|
||||
},[User,Course,Attachment,Project] ).results.total
|
||||
},[User,Course,Attachment,Project,Memo] ).results.total
|
||||
# search_type = params[:search_type].to_sym unless search_condition.blank?
|
||||
# search_by = params[:search_by]
|
||||
#
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
require 'elasticsearch/model'
|
||||
class Memo < ActiveRecord::Base
|
||||
include Redmine::SafeAttributes
|
||||
include UserScoreHelper
|
||||
include ApplicationHelper
|
||||
include Elasticsearch::Model
|
||||
belongs_to :forum
|
||||
has_many_kindeditor_assets :assets, :dependent => :destroy
|
||||
belongs_to :author, :class_name => "User", :foreign_key => 'author_id'
|
||||
|
@ -12,6 +14,18 @@ class Memo < ActiveRecord::Base
|
|||
validates_length_of :content, maximum: 30000
|
||||
validate :cannot_reply_to_locked_topic, :on => :create
|
||||
|
||||
|
||||
#elasticsearch kaminari init
|
||||
Kaminari::Hooks.init
|
||||
Elasticsearch::Model::Response::Response.__send__ :include, Elasticsearch::Model::Response::Pagination::Kaminari
|
||||
settings index: { number_of_shards: 5 } do
|
||||
mappings dynamic: 'false' do
|
||||
indexes :subject, analyzer: 'smartcn',index_options: 'offsets'
|
||||
indexes :content, analyzer: 'smartcn',index_options: 'offsets'
|
||||
indexes :updated_at, type:'date'
|
||||
end
|
||||
end
|
||||
|
||||
acts_as_tree :counter_cache => :replies_count, :order => "#{Memo.table_name}.created_at ASC"
|
||||
acts_as_attachable
|
||||
has_many :user_score_details, :class_name => 'UserScoreDetails',:as => :score_changeable_obj
|
||||
|
@ -47,9 +61,9 @@ class Memo < ActiveRecord::Base
|
|||
"parent_id",
|
||||
"replies_count"
|
||||
|
||||
after_create :add_author_as_watcher, :reset_counters!, :send_mail, :send_message
|
||||
# after_update :update_memos_forum
|
||||
after_destroy :reset_counters!,:delete_kindeditor_assets#,:down_user_score -- 公共区发帖暂不计入得分
|
||||
after_create :add_author_as_watcher, :reset_counters!, :send_mail, :send_message,:create_memo_ealasticsearch_index
|
||||
after_update :update_memo_ealasticsearch_index
|
||||
after_destroy :reset_counters!,:delete_kindeditor_assets,:delete_memo_ealasticsearch_index#,:down_user_score -- 公共区发帖暂不计入得分,
|
||||
# after_create :send_notification
|
||||
# after_save :plusParentAndForum
|
||||
# after_destroy :minusParentAndForum
|
||||
|
@ -57,6 +71,36 @@ class Memo < ActiveRecord::Base
|
|||
# scope :visible, lambda { |*args|
|
||||
# includes(:forum => ).where()
|
||||
# }
|
||||
scope :indexable,lambda {
|
||||
where('parent_id is null')
|
||||
}
|
||||
|
||||
def self.search(query)
|
||||
__elasticsearch__.search(
|
||||
{
|
||||
query: {
|
||||
multi_match: {
|
||||
query: query,
|
||||
type:"most_fields",
|
||||
operator: "or",
|
||||
fields: ['subject','content^0.5']
|
||||
}
|
||||
},
|
||||
sort: {
|
||||
_score:{order: "desc" },
|
||||
updated_at:{order: "desc" }
|
||||
},
|
||||
highlight: {
|
||||
pre_tags: ['<span class="c_red">'],
|
||||
post_tags: ['</span>'],
|
||||
fields: {
|
||||
subject: {},
|
||||
content: {}
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
def send_mail
|
||||
Mailer.run.forum_message_added(self) if Setting.notified_events.include?('forum_message_added')
|
||||
|
@ -205,4 +249,20 @@ class Memo < ActiveRecord::Base
|
|||
def delete_kindeditor_assets
|
||||
delete_kindeditor_assets_from_disk self.id,OwnerTypeHelper::MEMO
|
||||
end
|
||||
|
||||
def create_memo_ealasticsearch_index
|
||||
if self.parent_id.nil?
|
||||
self.__elasticsearch__.index_document
|
||||
end
|
||||
end
|
||||
def update_memo_ealasticsearch_index
|
||||
if self.parent_id.nil?
|
||||
self.__elasticsearch__.update_document
|
||||
end
|
||||
end
|
||||
def delete_memo_ealasticsearch_index
|
||||
if self.parent_id.nil?
|
||||
self.__elasticsearch__.delete_document
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -72,6 +72,22 @@
|
|||
</li>
|
||||
<div class="cl"></div>
|
||||
</ul>
|
||||
<% when 'memo'%>
|
||||
<ul class="searchContent">
|
||||
<li class="fl">
|
||||
</li>
|
||||
<li class="fl searchContentDes">
|
||||
<ul class="fl">
|
||||
<li class="f16 mb5"><a href="<%= forum_memo_path(:forum_id=>item.forum_id,:id=>item.id)%>" class="fontGrey3 fl"><%= item.try(:highlight).try(:subject) ? item.highlight.subject[0].html_safe : item.subject %></a>
|
||||
<div class="mt5 fl"><%= image_tag("search_icon_03.png", :width=>"8", :height=>"16" ,:class=>"fl") %><span class="searchTag">帖子</span></div>
|
||||
<div class="cl"></div>
|
||||
</li>
|
||||
<li class="fontGrey3 mb5"><%= item.try(:highlight).try(:content) ? item.highlight.content[0].html_safe : item.content.html_safe%></li>
|
||||
<li class="f12 fontGrey2"><span class="mr30">发帖人:<%= item.author_id ? User.find(item.author_id).login : '无' %></span><span class="mr30">创建时间:<%= format_date( item.created_at) %></span></li>
|
||||
</ul>
|
||||
</li>
|
||||
<div class="cl"></div>
|
||||
</ul>
|
||||
<%end %>
|
||||
<% end %>
|
||||
<div class="pageRoll">
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
<% unless memos.nil? || memos.empty?%>
|
||||
<% memos.each do |memo|%>
|
||||
<ul class="searchContent">
|
||||
<li class="fl">
|
||||
<!--<img src="images/homepageImage.jpg" alt="个人图片" width="75" height="75" class="searchCourseImage" />-->
|
||||
<%= link_to image_tag(url_to_avatar(User.find(memo.author_id)), :width => "75", :height => "75",:class=>'searchCourseImage'), forum_memo_path(:forum_id=>memo.forum_id,:id=>memo.id), :alt => "贴吧图片" %>
|
||||
</li>
|
||||
<li class="fl searchContentDes">
|
||||
<ul class="fl">
|
||||
<li class="f16 mb5"><a href="<%= forum_memo_path(:forum_id=>memo.forum_id,:id=>memo.id)%>" class="fontGrey3 fl"><%= memo.try(:highlight).try(:subject) ? memo.highlight.subject[0].html_safe : memo.subject %></a>
|
||||
<div class="mt5 fl"><%= image_tag("search_icon_03.png", :width=>"8", :height=>"16" ,:class=>"fl") %><span class="searchTag">帖子</span></div>
|
||||
<div class="cl"></div>
|
||||
</li>
|
||||
<li class="fontGrey3 mb5"><%= memo.try(:highlight).try(:content) ? memo.highlight.content[0].html_safe : memo.content.html_safe%></li>
|
||||
<li class="f12 fontGrey2"><span class="mr30">发帖人:<%= memo.author_id ? User.find(memo.author_id).login : '无' %></span><span class="mr30">创建时间:<%= format_date( memo.created_at) %></span></li>
|
||||
</ul>
|
||||
</li>
|
||||
<div class="cl"></div>
|
||||
</ul>
|
||||
<% end %>
|
||||
<div class="pageRoll">
|
||||
<%= paginate memos,:params => {:controller => 'welcome', :action => 'search',:search_type=>'memo'}%>
|
||||
</div>
|
||||
<% end %>
|
|
@ -3,7 +3,7 @@
|
|||
function g(o){return document.getElementById(o);}
|
||||
function HoverLi(n){
|
||||
//如果有N个标签,就将i<=N;
|
||||
for(var i=1;i<=5;i++){
|
||||
for(var i=1;i<=6;i++){
|
||||
g('searchBaner_'+i).className='searchBannerNormal';
|
||||
g('searchContent_'+i).className='undis';g('searchNum_'+i).className="numRed";
|
||||
g('searchType_'+i).className="fontGrey2 f14";
|
||||
|
@ -28,6 +28,8 @@
|
|||
search('attachment')
|
||||
}else if(n == 5){
|
||||
search('project')
|
||||
}else if(n == 6){
|
||||
search('memo')
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -56,6 +58,10 @@
|
|||
}else if('<%= @search_type%>' == 'project'){
|
||||
HoverLi(5)
|
||||
$("#searchContent_5").html('<%= escape_javascript(render :partial => 'search_project_results',:locals => {:projects=>@projects})%>');
|
||||
}else if('<%= @search_type%>' == 'memo')
|
||||
{
|
||||
HoverLi(6)
|
||||
$("#searchContent_6").html('<%= escape_javascript(render :partial => 'search_memo_results',:locals => {:memos=>@memos})%>');
|
||||
}
|
||||
})
|
||||
//如果要做成点击后再转到请将<li>中的onmouseover 改成 onclick;
|
||||
|
@ -76,6 +82,7 @@
|
|||
<li id="searchBaner_3" onclick="HoverLi(3);on_click_search(3);"><a href="javascript:void(0);" id="searchType_3" class="fontGrey2 f14">课程<span style="font-weight:normal;"><font id="searchNum_3" class="numRed">(<%=@course_count%>)</font></span></a></li>
|
||||
<li id="searchBaner_4" onclick="HoverLi(4);on_click_search(4);"><a href="javascript:void(0);" id="searchType_4" class="fontGrey2 f14">资源<span class="numRed" style="font-weight:normal;"><font id="searchNum_4" class="numRed">(<%= @attach_count%>)</font></span></a></li>
|
||||
<li id="searchBaner_5" onclick="HoverLi(5);on_click_search(5);"><a href="javascript:void(0);" id="searchType_5" class="fontGrey2 f14">项目<span class="numRed" style="font-weight:normal;"><font id="searchNum_5" class="numRed">(<%= @project_count%>)</font></span></a></li>
|
||||
<li id="searchBaner_6" onclick="HoverLi(6);on_click_search(6);"><a href="javascript:void(0);" id="searchType_6" class="fontGrey2 f14">帖子<span class="numRed" style="font-weight:normal;"><font id="searchNum_6" class="numRed">(<%= @memo_count%>)</font></span></a></li>
|
||||
<div class="cl"></div>
|
||||
</ul>
|
||||
<ul id="searchTips" style="display:none;">
|
||||
|
@ -94,6 +101,9 @@
|
|||
</div>
|
||||
<div id="searchContent_5" class="undis">
|
||||
|
||||
</div>
|
||||
<div id="searchContent_6" class="undis">
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -9,5 +9,7 @@ $("#searchContent_3").html('<%= escape_javascript(render :partial => 'search_cou
|
|||
$("#searchContent_5").html('<%= escape_javascript(render :partial => 'search_project_results',:locals => {:projects=>@projects})%>');
|
||||
<% when 'attachment'%>
|
||||
$("#searchContent_4").html('<%= escape_javascript(render :partial => 'search_attachment_results',:locals => {:attachments=>@attachments})%>');
|
||||
<% when 'memo'%>
|
||||
$("#searchContent_6").html('<%= escape_javascript(render :partial => 'search_memo_results',:locals => {:memos=>@memos})%>');
|
||||
<%else%>
|
||||
<%end %>
|
|
@ -28,4 +28,11 @@ namespace :importer do
|
|||
ENV['BATCH']='1000'
|
||||
Rake::Task["elasticsearch:import:model"].invoke
|
||||
end
|
||||
task :importmemo do
|
||||
ENV['CLASS']='Memo'
|
||||
ENV['SCOPE']='indexable'
|
||||
ENV['FORCE']='y'
|
||||
ENV['BATCH']='1000'
|
||||
Rake::Task["elasticsearch:import:model"].invoke
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue