From 86eabcc0f13eeef7b433f7d189f8b694929f367e Mon Sep 17 00:00:00 2001 From: Manson Wang Date: Wed, 19 Mar 2014 14:05:54 +0800 Subject: [PATCH] Search Engine improved performance --- .../stylesheets/basic/orbit_bar.css.erb | 4 +- app/controllers/site_search_controller.rb | 66 +++++++++---------- app/views/layouts/_search.erb | 8 ++- 3 files changed, 41 insertions(+), 37 deletions(-) diff --git a/app/assets/stylesheets/basic/orbit_bar.css.erb b/app/assets/stylesheets/basic/orbit_bar.css.erb index 91076b8..173525c 100644 --- a/app/assets/stylesheets/basic/orbit_bar.css.erb +++ b/app/assets/stylesheets/basic/orbit_bar.css.erb @@ -225,8 +225,8 @@ .search_result h5{ } -.search_result strong{ - /*font-weight: bold;*/ +.search_result b{ + font-weight: normal; color: #B90000; } diff --git a/app/controllers/site_search_controller.rb b/app/controllers/site_search_controller.rb index 42a9482..a9ef51c 100644 --- a/app/controllers/site_search_controller.rb +++ b/app/controllers/site_search_controller.rb @@ -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(/ /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(/ /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(/ /i,"") + value = eval("r.#{f}").gsub(/<\/?[^>]*>/, "").gsub(/ /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, '\1') rescue "" - end + result.each do |res| + res[:matches] = 0 - if filter - index = content.index '' + 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, '\1') rescue "" + res[:content].gsub!(/(#{k})/i, '\1') rescue "" + end + + result.delete(res) if res[:matches]==0 + next if res[:matches]==0 + + index = res[:content].index '' 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 \ No newline at end of file diff --git a/app/views/layouts/_search.erb b/app/views/layouts/_search.erb index 1c4d93e..20340c5 100644 --- a/app/views/layouts/_search.erb +++ b/app/views/layouts/_search.erb @@ -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 @@ "
"+ ""+ "
"+res.title+"
"+ - "

"+ + ""+ ""+res.content+""+ "
"; $("#search_results").append(search_result);