Commit 7540e120 authored by Valery Sizov's avatar Valery Sizov

Fix Elasticsearch

parent acb8e33d
......@@ -32,59 +32,12 @@ module SearchHelper
def parse_search_result(result)
if result.is_a?(String)
parse_search_result_from_grep(result)
Gitlab::ProjectSearchResults.parse_search_result(result)
else
parse_search_result_from_elastic(result)
Gitlab::Elastic::SearchResults.parse_search_result(result)
end
end
def parse_search_result_from_elastic(result)
ref = result["_source"]["blob"]["commit_sha"]
filename = result["_source"]["blob"]["path"]
extname = File.extname(filename)
basename = filename.sub(/#{extname}$/, '')
content = result["_source"]["blob"]["content"]
total_lines = content.lines.size
highlighted_content = result["highlight"]["blob.content"]
term = highlighted_content && highlighted_content[0].match(/gitlabelasticsearch→(.*?)←gitlabelasticsearch/)[1]
found_line_number = 0
content.each_line.each_with_index do |line, index|
if term && line.include?(term)
found_line_number = index
break
end
end
from = if found_line_number >= 2
found_line_number - 2
else
found_line_number
end
to = if (total_lines - found_line_number) > 3
found_line_number + 2
else
found_line_number
end
data = content.lines[from..to]
OpenStruct.new(
filename: filename,
basename: basename,
ref: ref,
startline: from + 1,
data: data.join
)
end
def parse_search_result_from_grep(result)
Gitlab::ProjectSearchResults.parse_search_result(result)
end
def find_project_for_blob(blob)
Project.find(blob['_parent'])
end
......
- file_name, blob = blob
- project = @project || find_project_for_blob(blob)
- ref = @search_results.repository_ref
- if blob.is_a?(Array)
- file_name, blob = blob
- else
- blob = parse_search_result(blob)
- file_name = blob.filename
- ref = blob.ref
- blob_link = namespace_project_blob_path(project.namespace, project, tree_join(ref, file_name))
.blob-result
.file-holder
......
......@@ -57,6 +57,49 @@ module Gitlab
@milestones_count ||= milestones.total_count
end
def self.parse_search_result(result)
ref = result["_source"]["blob"]["commit_sha"]
filename = result["_source"]["blob"]["path"]
extname = File.extname(filename)
basename = filename.sub(/#{extname}$/, '')
content = result["_source"]["blob"]["content"]
total_lines = content.lines.size
highlighted_content = result["highlight"]["blob.content"]
term = highlighted_content && highlighted_content[0].match(/gitlabelasticsearch→(.*?)←gitlabelasticsearch/)[1]
found_line_number = 0
content.each_line.each_with_index do |line, index|
if term && line.include?(term)
found_line_number = index
break
end
end
from = if found_line_number >= 2
found_line_number - 2
else
found_line_number
end
to = if (total_lines - found_line_number) > 3
found_line_number + 2
else
found_line_number
end
data = content.lines[from..to]
OpenStruct.new(
filename: filename,
basename: basename,
ref: ref,
startline: from + 1,
data: data.join
)
end
private
def base_options
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment