Permalink
Browse files

Merge remote-tracking branch 'kattrali/project-search-optimization' i…

…nto gems

Conflicts:
	plugins/project_search/lib/project_search/lucene_index.rb
	plugins/project_search/lib/project_search/stylesheets/style.css
  • Loading branch information...
2 parents b0d979e + 50e4f1f commit 2ee1ac8e9e8d5869fc5ae35233400590fc9b711b @danlucraft committed Aug 19, 2011
@@ -2,47 +2,47 @@
class ProjectSearch
class LuceneIndex
attr_accessor :last_updated, :lucene_index
-
+
def initialize(project)
@project = project
@has_content = false
load
end
-
+
def timestamp_file_path
File.join(@project.config_dir, 'lucene_last_updated')
end
-
+
def lucene_index_dir
File.join(@project.config_dir, "lucene")
end
-
+
def delete
FileUtils.rm(timestamp_file_path)
FileUtils.rm_r(lucene_index_dir)
load
end
-
+
def load
@last_updated = Time.at(0)
if File.exist?(timestamp_file_path)
@last_updated = Time.at(File.read(timestamp_file_path).chomp.to_i)
@has_content = true
end
end
-
+
def dump
File.open(timestamp_file_path, "w") do |fout|
fout.puts(last_updated.to_i.to_s)
end
end
-
+
def has_content?
@has_content
end
-
+
MAX_FILE_SIZE = 500 * 1024
-
+
def update
changed_files = @project.file_list.changed_since(last_updated)
@last_updated = Time.now
@@ -60,7 +60,7 @@ def update
pre_contents = File.new(fn).read(200)
unless !pre_contents or BinaryDataDetector.binary?(pre_contents)
contents = File.read(fn)
- adjusted_contents = contents
+ adjusted_contents = contents.gsub(/\.([^\s])/, '. \1')
@lucene_index << { :id => fn, :contents => adjusted_contents }
end
rescue => e
@@ -76,6 +76,6 @@ def update
end
dump
end
-
+
end
end
@@ -2,7 +2,8 @@
margin: 0;
padding: 0;
font-weight: normal;
- font-family: Helvetica Neue, Sawasdee, sans-serif;
+ font-size: 14px;
+ font-family: Myriad, Helvetica Neue, Sawasdee, Lucida Grande, sans-serif;
}
body {
@@ -167,7 +168,7 @@ body {
/* background-color: #ccc;*/
padding: 4px 4px 4px 12px;
font-weight: bold;
- font-family:Helvetica Neue, Sawasdee, sans-serif;
+ font-family: Myriad, Helvetica Neue, Sawasdee, Lucida Grande, sans-serif;
border-bottom: 1px dotted #aaa;
}
@@ -27,8 +27,8 @@
<% line_index += 1 %>
<tr class="result file_<%= file_num %> line_<%= this_line_num %> <%= (line_index % 2 == 0 ? 'even' : 'odd') %>" data-href='<%= CGI::escapeHTML(file) %>' data-line_num='<%= this_line_num %>'>
<td class='line_num'><%= this_line_num %></td>
- <% text = CGI.escapeHTML(context_line).gsub(@word_search.regex) { "<span>#{$&}</span>" } %>
- <td class='text'><pre><%= text.chomp + " " %></pre></td> <!-- extra space to ensure line-height is respected -->
+ <% text = CGI.escapeHTML(context_line.gsub(@word_search.regex) { "__span__#{$&}__span__" }) %>
+ <td class='text'><pre><%= text.gsub(/__span__(.*?)__span__/,"<span>\\1</span>").chomp + " " %></pre></td> <!-- extra space to ensure line-height is respected -->
</tr>
<% end %>
<% end %>
@@ -37,8 +37,8 @@
<% last_displayed_line_num = display_line_num %>
<tr class="result file_<%= file_num %> line_<%= display_line_num %> <%= (line_index % 2 == 0 ? 'even' : 'odd') %>" data-href='<%= CGI::escapeHTML(file) %>' data-line_num='<%= display_line_num %>'>
<td class='line_num'><%= display_line_num %></td>
- <% text = CGI.escapeHTML(hit.line).gsub(@word_search.regex) { "<span>#{$&}</span>" } %>
- <td class='text'><pre><%= text.chomp + " " %></pre></td> <!-- extra space to ensure line-height is respected -->
+ <% text = CGI.escapeHTML(hit.line.gsub(@word_search.regex) { "__span__#{$&}__span__" }) %>
+ <td class='text'><pre><%= text.gsub(/__span__(.*?)__span__/,"<span>\\1</span>").chomp + " " %></pre></td> <!-- extra space to ensure line-height is respected -->
</tr>
<% hit.post_context.each_with_index do |context_line, i| %>
@@ -48,8 +48,8 @@
<% line_index += 1 %>
<tr class="result file_<%= file_num %> line_<%= this_line_num %> <%= (line_index % 2 == 0 ? 'even' : 'odd') %>" data-href='<%= CGI::escapeHTML(file) %>' data-line_num='<%= this_line_num %>'>
<td class='line_num'><%= this_line_num %></td>
- <% text = CGI.escapeHTML(context_line).gsub(@word_search.regex) { "<span>#{$&}</span>" } %>
- <td class='text'><pre><%= text.chomp + " " %></pre></td> <!-- extra space to ensure line-height is respected -->
+ <% text = CGI.escapeHTML(context_line.gsub(@word_search.regex) { "__span__#{$&}__span__" }) %>
+ <td class='text'><pre><%= text.gsub(/__span__(.*?)__span__/,"<span>\\1</span>").chomp + " " %></pre></td> <!-- extra space to ensure line-height is respected -->
</tr>
<% end %>
<% end %>
@@ -147,7 +147,7 @@
$('.file_link').live('click',function() {
$('body').animate({
- scrollTop: $("#"+$(this).attr('id').replace("link_","")).offset().top - 120
+ scrollTop: $("#"+$(this).attr('id').replace("link_","")).offset().top - 100
}, 0);
});
});
@@ -13,19 +13,19 @@ def initialize(project, query_string, match_case, context_size)
@match_case = !!match_case
@context_size = context_size
end
-
+
def match_case?
@match_case
end
-
+
def context?
@context_size > 0
end
-
+
def matching_line?(line)
line =~ regex
end
-
+
def regex
@regex ||= begin
regexp_text = Regexp.escape(@query_string)
@@ -37,11 +37,11 @@ def regex
match_case? ? /#{regexp_text}/ : /#{regexp_text}/i
end
end
-
+
def on_file_results(&block)
@on_file_results_block = block
end
-
+
def generate_results
hits = []
doc_ids.each do |doc_id|
@@ -58,9 +58,9 @@ def generate_results
remove_hits << hit
end
end
-
+
hits_needing_post_context -= remove_hits
-
+
if matching_line?(line)
hit = Hit.new(doc_id, line_num, line, regex, pre_context.dup, [])
hits << hit
@@ -78,13 +78,13 @@ def generate_results
end
hits
end
-
+
def send_file_results(hits)
if @on_file_results_block
@on_file_results_block.call(hits)
end
end
-
+
def results
@results ||= generate_results
end
@@ -95,18 +95,32 @@ def formatted_query
"contents",
StandardAnalyzer.new(Version::LUCENE_29)
)
- parser.parse(query_string).to_s
+ begin
+ text = query_string.gsub(/^\W+/,"").gsub(/\(|\)/," ").strip
+ unless text.empty?
+ query = parser.parse(text)
+ return query.to_s.gsub("_*","*").gsub(/_|\./," ").strip
+ end
+ rescue => e
+ p e.message
+ p e.backtrace
+ end
+ return ""
end
def doc_ids
@doc_ids ||= begin
index = ProjectSearch.indexes[project.path].lucene_index
doc_ids = nil
- doc_ids = index.find(formatted_query).map {|doc| doc.id}
- doc_ids.reject {|doc_id| Redcar::Project::FileList.hide_file_path?(doc_id) }
+ if text = formatted_query and not text.empty?
+ doc_ids = index.find(text).map {|doc| doc.id}.uniq
+ doc_ids.reject {|doc_id| Redcar::Project::FileList.hide_file_path?(doc_id) }
+ else
+ []
+ end
end
end
-
+
def ignore_regexes
self.class.shared_storage['ignored_file_patterns']
end

0 comments on commit 2ee1ac8

Please sign in to comment.