Search Engine improved performance

This commit is contained in:
Manson Wang 2014-03-19 14:05:54 +08:00
parent 4482fa50eb
commit 86eabcc0f1
3 changed files with 41 additions and 37 deletions

View File

@ -225,8 +225,8 @@
.search_result h5{ .search_result h5{
} }
.search_result strong{ .search_result b{
/*font-weight: bold;*/ font-weight: normal;
color: #B90000; color: #B90000;
} }

View File

@ -16,20 +16,19 @@ class SiteSearchController < ApplicationController
# Search Pages # Search Pages
Item.where(:app_frontend_url=>"page_contexts", :is_published=>true).each do |page| Item.where(:app_frontend_url=>"page_contexts", :is_published=>true).each do |page|
title = page.title title = page.title
context = PageContext.where(:page_id=>page.id).first.context context = PageContext.where(:page_id=>page.id).first.context rescue nil
context = ActionController::Base.helpers.strip_tags(context).gsub(/&nbsp;/i," ") rescue "" next if title.nil? and context.nil?
title_matches = title.scan(regex).size
context_matches = context.scan(regex).size rescue 0 context = context.gsub(/<\/?[^>]*>/, "").gsub(/&nbsp;/i," ") rescue ""
if title_matches > 0 or context_matches > 0 title_matches = title.match(regex)
context_matches = context.match(regex)
if title_matches or context_matches
tmp = {} tmp = {}
tmp[:id] = page.id tmp[:id] = page.id
tmp[:module] = "page" tmp[:module] = "page"
tmp[:url] = "/"+page.path tmp[:url] = "/"+page.path
tmp[:title] = hight_light(keywords, title) tmp[:title] = title
tmp[:content] = hight_light(keywords, context, true) tmp[:content] = context
tmp[:matches] = title_matches+context_matches
tmp[:matches] += title.scan(Regexp.new(".*"+key_string+".*", "i")).size > 0 ? 20 : 0 rescue 0
tmp[:matches] += context.scan(Regexp.new(".*"+key_string+".*", "i")).size > 0 ? 20 : 0 rescue 0
result.push(tmp) result.push(tmp)
end end
end end
@ -38,7 +37,7 @@ class SiteSearchController < ApplicationController
modules.each do |mod| modules.each do |mod|
query = mod[:fields].map{|f| {f.to_sym => regex} } query = mod[:fields].map{|f| {f.to_sym => regex} }
res = Kernel.const_get(mod[:model]).any_of(query) res = Kernel.const_get(mod[:model]).any_of(query)
res.all.each do |r| res.each do |r|
tmp = {} tmp = {}
tmp[:id] = r.id tmp[:id] = r.id
tmp[:module] = mod[:key] tmp[:module] = mod[:key]
@ -46,7 +45,7 @@ class SiteSearchController < ApplicationController
tmp[:content] = "" tmp[:content] = ""
tmp[:matches] = 0 tmp[:matches] = 0
mod[:fields].each do |f| mod[:fields].each do |f|
value = ActionController::Base.helpers.strip_tags(eval("r.#{f}")).gsub(/&nbsp;/i,"") value = eval("r.#{f}").gsub(/<\/?[^>]*>/, "").gsub(/&nbsp;/i,"")
if f=="title" or f=="name" if f=="title" or f=="name"
tmp[:title] = value tmp[:title] = value
@ -54,41 +53,40 @@ class SiteSearchController < ApplicationController
tmp[:content] << value tmp[:content] << value
end end
end end
tmp[:matches] = tmp[:content].scan(regex).size + tmp[:title].scan(regex).size
tmp[:matches] += tmp[:title].scan(Regexp.new(".*"+key_string+".*", "i")).size > 0 ? 20 : 0 rescue 0
tmp[:matches] += tmp[:content].scan(Regexp.new(".*"+key_string+".*", "i")).size > 0 ? 20 : 0 rescue 0
tmp[:title] = hight_light(keywords, tmp[:title])
tmp[:content] = hight_light(keywords, tmp[:content], true)
result.push(tmp) unless tmp[:matches]==0 result.push(tmp)
end end
end end
end end
result = result.sort_by { |k| k[:matches] }.reverse rescue []
render :json => { "results" => result, "time"=> ((Time.now-startTime)*1000).to_i , "keywords"=>keywords}
end
def hight_light(keywords, content, filter=false) result.each do |res|
matches = 0 res[:matches] = 0
keywords.each do |k|
matches += content.scan(/(#{k})/i).size
content.gsub!(/(#{k})/i, '<strong>\1</strong>') rescue ""
end
if filter res[:matches] += res[:title].match(/(#{key_string})/i) ? 100 : 0 rescue 0
index = content.index '<strong>' res[:matches] += res[:content].match(/(#{key_string})/i) ? 100 : 0 rescue 0
keywords.each do |k|
res[:matches] += res[:title].scan(/(#{k})/i).size + res[:content].scan(/(#{k})/i).size
res[:title].gsub!(/(#{k})/i, '<b>\1</b>') rescue ""
res[:content].gsub!(/(#{k})/i, '<b>\1</b>') rescue ""
end
result.delete(res) if res[:matches]==0
next if res[:matches]==0
index = res[:content].index '<b>'
unless index.nil? unless index.nil?
index = index>50 ? index-50 : 0 index = index>50 ? index-50 : 0
deadline = 150 deadline = 150
content = content[index, deadline] res[:content] = res[:content][index, deadline]
else else
content = content[0, 100] res[:content] = res[:content][0, 150]
end end
end end
return content result = result.sort_by { |k| k[:matches] }.reverse rescue []
render :json => { "results" => result, "time"=> ((Time.now-startTime)*1000).to_i , "keywords"=>keywords}
end end
end end

View File

@ -28,9 +28,15 @@
$("#search_close").click(function(){$("#search_container").fadeOut(300);}); $("#search_close").click(function(){$("#search_container").fadeOut(300);});
function processSearch(){ function processSearch(){
keyword_tmp = keyword;
processLock = true; processLock = true;
if(interval==0 && keyword!=""){ if(interval==0 && keyword!=""){
$.getJSON("/site_search?keywords="+keyword, function(data){ $.getJSON("/site_search?keywords="+keyword, function(data){
if(keyword_tmp != keyword) {
processLock = false;
processSearch();
return;
}
$("#search_results").scrollTop(0); $("#search_results").scrollTop(0);
$("#search_results").html(""); $("#search_results").html("");
$("#search_time").html("Found "+data.results.length+" Results, Search Time: "+data.time+"ms"); $("#search_time").html("Found "+data.results.length+" Results, Search Time: "+data.time+"ms");
@ -39,7 +45,7 @@
"<div class='search_result'>"+ "<div class='search_result'>"+
"<a href='"+res.url+"' target='_blank'>"+ "<a href='"+res.url+"' target='_blank'>"+
"<div class='seach_title'>"+res.title+"</div>"+ "<div class='seach_title'>"+res.title+"</div>"+
"</a><br/>"+ "</a>"+
"<span>"+res.content+"</span>"+ "<span>"+res.content+"</span>"+
"</div>"; "</div>";
$("#search_results").append(search_result); $("#search_results").append(search_result);