Commit 296f70a0 authored by Dylan Griffith's avatar Dylan Griffith

Extract constants HIGHLIGHT_START_TAG HIGHLIGHT_END_TAG

parent f24f5fb5
...@@ -4,6 +4,8 @@ module Elastic ...@@ -4,6 +4,8 @@ module Elastic
module Latest module Latest
module GitClassProxy module GitClassProxy
SHA_REGEX = /\A[0-9a-f]{5,40}\z/i.freeze SHA_REGEX = /\A[0-9a-f]{5,40}\z/i.freeze
HIGHLIGHT_START_TAG = 'gitlabelasticsearch→'
HIGHLIGHT_END_TAG = '←gitlabelasticsearch'
def elastic_search(query, type: 'all', page: 1, per: 20, options: {}) def elastic_search(query, type: 'all', page: 1, per: 20, options: {})
results = { blobs: [], commits: [] } results = { blobs: [], commits: [] }
...@@ -95,8 +97,8 @@ module Elastic ...@@ -95,8 +97,8 @@ module Elastic
end end
query_hash[:highlight] = { query_hash[:highlight] = {
pre_tags: ["gitlabelasticsearch→"], pre_tags: [HIGHLIGHT_START_TAG],
post_tags: ["←gitlabelasticsearch"], post_tags: [HIGHLIGHT_END_TAG],
fields: es_fields fields: es_fields
} }
end end
...@@ -157,8 +159,8 @@ module Elastic ...@@ -157,8 +159,8 @@ module Elastic
if options[:highlight] if options[:highlight]
query_hash[:highlight] = { query_hash[:highlight] = {
pre_tags: ["gitlabelasticsearch→"], pre_tags: [HIGHLIGHT_START_TAG],
post_tags: ["←gitlabelasticsearch"], post_tags: [HIGHLIGHT_END_TAG],
number_of_fragments: 0, # highlighted text fragments do not work well for code as we want to show a few whole lines of code. We need to get the whole content to determine the exact line number that was highlighted. number_of_fragments: 0, # highlighted text fragments do not work well for code as we want to show a few whole lines of code. We need to get the whole content to determine the exact line number that was highlighted.
fields: { fields: {
"blob.content" => {}, "blob.content" => {},
......
...@@ -128,7 +128,7 @@ module Gitlab ...@@ -128,7 +128,7 @@ module Gitlab
found_line_number = 0 found_line_number = 0
highlight_content.each_line.each_with_index do |line, index| highlight_content.each_line.each_with_index do |line, index|
if line.include?('gitlabelasticsearch→') if line.include?(::Elastic::Latest::GitClassProxy::HIGHLIGHT_START_TAG)
found_line_number = index found_line_number = index
break break
end end
......
...@@ -84,7 +84,7 @@ RSpec.describe Gitlab::Elastic::SearchResults, :elastic, :sidekiq_might_not_need ...@@ -84,7 +84,7 @@ RSpec.describe Gitlab::Elastic::SearchResults, :elastic, :sidekiq_might_not_need
result = { result = {
'_source' => blob, '_source' => blob,
'highlight' => { 'highlight' => {
'blob.content' => ["foo\ngitlabelasticsearch→bar←gitlabelasticsearch\nbaz\n"] 'blob.content' => ["foo\n#{::Elastic::Latest::GitClassProxy::HIGHLIGHT_START_TAG}bar#{::Elastic::Latest::GitClassProxy::HIGHLIGHT_END_TAG}\nbaz\n"]
} }
} }
...@@ -120,7 +120,7 @@ RSpec.describe Gitlab::Elastic::SearchResults, :elastic, :sidekiq_might_not_need ...@@ -120,7 +120,7 @@ RSpec.describe Gitlab::Elastic::SearchResults, :elastic, :sidekiq_might_not_need
bar bar
bar bar
foo foo
gitlabelasticsearch→bar←gitlabelasticsearch # this is the highlighted bar #{::Elastic::Latest::GitClassProxy::HIGHLIGHT_START_TAG}bar#{::Elastic::Latest::GitClassProxy::HIGHLIGHT_END_TAG} # this is the highlighted bar
baz baz
boo boo
bar bar
......
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