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 strong{
/*font-weight: bold;*/
.search_result b{
font-weight: normal;
color: #B90000;
}

View File

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

View File

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