Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

manually load attachments' fulltext on search

ulferts6d71f2d26a58
diff --git a/app/helpers/search_helper.rb b/app/helpers/search_helper.rb
index 3f484441fb9..1119c217211 100644
--- a/app/helpers/search_helper.rb
+++ b/app/helpers/search_helper.rb
@@ -29,35 +29,43 @@
#++
module SearchHelper
- def highlight_tokens(text, tokens)
- return text unless text && tokens && !tokens.empty?
+ def highlight_tokens(text, tokens, text_on_not_found: false)
+ split_text = text_split_by_token(text, tokens)
+
+ return nil unless split_text.length > 1 || text_on_not_found
- re_tokens = tokens.map { |t| Regexp.escape(t) }
- regexp = Regexp.new "(#{re_tokens.join('|')})", Regexp::IGNORECASE
result = ''
- text.split(regexp).each_with_index do |words, i|
+ split_text.each_with_index do |words, i|
if result.length > 1200
# maximum length of the preview reached
result << '...'
break
end
- if i.even?
- result << h(words.length > 100 ? "#{words.slice(0..44)} ... #{words.slice(-45..-1)}" : words)
- else
- t = (tokens.index(words.downcase) || 0) % 4
- result << content_tag('span', h(words), class: "search-highlight token-#{t}")
- end
+
+ result << if i.even?
+ abbreviated_text(words)
+ else
+ token_span(tokens, words)
+ end
end
result.html_safe
end
- def highlight_first(texts, tokens)
- texts.each do |text|
- if has_tokens? text, tokens
- return highlight_tokens text, tokens
- end
- end
- highlight_tokens texts[-1], tokens
+ def highlight_tokens_in_event(event, tokens)
+ # This way, the attachments are only loaded in case the tokens are not found inside
+ # the journal notes.
+ highlight_tokens(last_journal(event).try(:notes), tokens) or
+ highlight_tokens(attachment_fulltexts(event), tokens) or
+ highlight_tokens(attachment_filenames(event), tokens) or
+ highlight_tokens(event.event_description, tokens, text_on_not_found: true)
+ end
+
+ def text_split_by_token(text, tokens)
+ return [text].compact unless text && tokens && !tokens.empty?
+
+ re_tokens = tokens.map { |t| Regexp.escape(t) }
+ regexp = Regexp.new "(#{re_tokens.join('|')})", Regexp::IGNORECASE
+ text.split(regexp)
end
def has_tokens?(text, tokens)
@@ -88,14 +96,6 @@ module SearchHelper
end
end
- def attachment_fulltexts(event)
- attachment_strings_for(:fulltext, event)
- end
-
- def attachment_filenames(event)
- attachment_strings_for(:filename, event)
- end
-
def type_label(t)
OpenProject::GlobalSearch.tab_name(t)
end
@@ -124,9 +124,30 @@ module SearchHelper
private
- def attachment_strings_for(attribute_name, event)
+ def attachment_fulltexts(event)
+ only_if_tsv_supported(event) do
+ Attachment.where(id: event.attachment_ids).pluck(:fulltext).join(' ')
+ end
+ end
+
+ def attachment_filenames(event)
+ only_if_tsv_supported(event) do
+ event.attachments&.map(&:filename)&.join(' ')
+ end
+ end
+
+ def only_if_tsv_supported(event)
if EnterpriseToken.allows_to?(:attachment_filters) && OpenProject::Database.allows_tsv? && event.respond_to?(:attachments)
- event.attachments&.map(&attribute_name)&.join(' ')
+ yield
end
end
+
+ def token_span(tokens, words)
+ t = (tokens.index(words.downcase) || 0) % 4
+ content_tag('span', h(words), class: "search-highlight token-#{t}")
+ end
+
+ def abbreviated_text(words)
+ h(words.length > 100 ? "#{words.slice(0..44)} ... #{words.slice(-45..-1)}" : words)
+ end
end
diff --git a/app/views/search/index.html.erb b/app/views/search/index.html.erb
index 5c60e8e801d..44e4a2bebeb 100644
--- a/app/views/search/index.html.erb
+++ b/app/views/search/index.html.erb
@@ -63,10 +63,7 @@ See COPYRIGHT and LICENSE files for more details.
<%= link_to highlight_tokens(truncate(e.event_title, escape: false, length: 255), @tokens), with_notes_anchor(e, @tokens) %>
</dt>
<dd>
- <span class="description"><%= highlight_first([last_journal(e).try(:notes),
- attachment_fulltexts(e),
- attachment_filenames(e),
- e.event_description], @tokens) %></span>
+ <span class="description"><%= highlight_tokens_in_event(e, @tokens) %></span>
<span class="author"><%= format_time(e.event_datetime) %></span>
</dd>
<% end %>
diff --git a/spec/features/search_spec.rb b/spec/features/search_spec.rb
index 298da26fef5..7b8d537111a 100644
--- a/spec/features/search_spec.rb
+++ b/spec/features/search_spec.rb
@@ -209,6 +209,7 @@ describe 'Search', type: :feature, js: true, with_settings: { per_page_options:
end
end
+ # rubocop:disable RSpec/MultipleMemoizedHelpers
context 'project search' do
let(:subproject) { FactoryBot.create :project, parent: project }
let!(:other_work_package) do
@@ -337,7 +338,44 @@ describe 'Search', type: :feature, js: true, with_settings: { per_page_options:
filters.expect_no_filter_by 'subprojectId', 'subprojectId'
end
end
+
+ context 'for a project search with attachments' do
+ let!(:attachment) do
+ FactoryBot.create(:attachment,
+ container: work_packages[9]).tap do |a|
+ Attachment
+ .where(id: a.id)
+ .update_all(['fulltext = ?, fulltext_tsv = to_tsvector(?, ?)',
+ attachment_text,
+ 'english',
+ attachment_text])
+ end
+ end
+ let(:query) { "word" }
+ let(:attachment_text) { "A text with the #{query} included" }
+
+ it 'finds work packages with attachments' do
+ with_enterprise_token :attachment_filters
+
+ global_search.search query
+ global_search.submit_in_project_and_subproject_scope
+
+ expect(page)
+ .to have_content attachment_text
+
+ expect(page)
+ .to have_selector(".search-highlight", text: query)
+
+ page.find('[data-qa-tab-id="work_packages"]').click
+
+ table = Pages::EmbeddedWorkPackagesTable.new(find('.work-packages-embedded-view--container'))
+ table.ensure_work_package_not_listed!(work_packages[0])
+ table.ensure_work_package_not_listed!(work_packages[1])
+ table.expect_work_package_listed(work_packages[9])
+ end
+ end
end
+ # rubocop:enable RSpec/MultipleMemoizedHelpers
describe 'search for projects' do
let!(:searched_for_project) { FactoryBot.create(:project, name: 'Searched for project') }
diff --git a/spec/helpers/search_helper_spec.rb b/spec/helpers/search_helper_spec.rb
index c6af0d7c387..e4d7e4938a2 100644
--- a/spec/helpers/search_helper_spec.rb
+++ b/spec/helpers/search_helper_spec.rb
@@ -88,35 +88,91 @@ describe 'search/index', type: :helper do
end
end
- describe '#highlight_first' do
- let(:tokens) { %w(token) }
+ describe '#highlight_tokens_in_event' do
+ let(:journal_notes) { "Journals notes" }
+ let(:event_description) { "The description of the event" }
+ let(:attachment_fulltext) { "The fulltext of the attachment" }
+ let(:attachment_filename) { "attachment_filename.txt" }
+ let(:journal) { FactoryBot.build_stubbed(:work_package_journal, notes: journal_notes) }
+ let(:event) do
+ instance_double('WorkPackage',
+ last_journal: journal,
+ last_loaded_journal: journal,
+ event_description: event_description,
+ attachment_ids: [42],
+ attachments: [FactoryBot.build_stubbed(:attachment, filename: attachment_filename)]).tap do |e|
+ scope = instance_double(ActiveRecord::Relation)
+
+ allow(Attachment)
+ .to receive(:where)
+ .with(id: e.attachment_ids)
+ .and_return(scope)
+
+ allow(scope)
+ .to receive(:pluck)
+ .with(:fulltext)
+ .and_return [attachment_fulltext]
+ end
+ end
+
+ before do
+ with_enterprise_token :attachment_filters
+ end
+
+ context 'with the token in the journal notes' do
+ let(:tokens) { %w(journals) }
- subject { helper.highlight_first titles, tokens }
+ it 'shows the text in the notes' do
+ expect(helper.highlight_tokens_in_event(event, tokens))
+ .to eql '<span class="search-highlight token-0">Journals</span> notes'
- context 'when first is matched' do
- let(:first) { 'This is a token' }
- let(:second) { 'I have some token for you' }
- let(:titles) { [first, second] }
- let(:first_highlighted) { 'This is a <span class="search-highlight token-0">token</span>' }
+ end
+ end
+
+ context 'with the token in the description' do
+ let(:tokens) { %w(description) }
- it { is_expected.to eq first_highlighted }
+ it 'shows the text in the description' do
+ expect(helper.highlight_tokens_in_event(event, tokens))
+ .to eql 'The <span class="search-highlight token-0">description</span> of the event'
+ end
end
- context 'when first is not matched' do
- let(:first) { 'This is a book' }
- let(:second) { 'I have some token for you' }
- let(:titles) { [first, second] }
- let(:second_highlighted) { 'I have some <span class="search-highlight token-0">token</span> for you' }
+ context 'with the token in the description and empty journal notes' do
+ let(:tokens) { %w(description) }
+ let(:journal_notes) { "" }
- it { is_expected.to eq second_highlighted }
+ it 'shows the text in the description' do
+ expect(helper.highlight_tokens_in_event(event, tokens))
+ .to eql 'The <span class="search-highlight token-0">description</span> of the event'
+ end
end
- context 'when both first and second is not matched' do
- let(:first) { 'This is a book' }
- let(:second) { 'I have some book for you' }
- let(:titles) { [first, second] }
+ context 'with the token in the attachment text' do
+ let(:tokens) { %w(fulltext) }
- it { is_expected.to eq second }
+ it 'shows the text in the fulltext' do
+ expect(helper.highlight_tokens_in_event(event, tokens))
+ .to eql 'The <span class="search-highlight token-0">fulltext</span> of the attachment'
+ end
+ end
+
+ context 'with the token in the attachment filename' do
+ let(:tokens) { %w(filename) }
+
+ it 'shows the text in the fulltext' do
+ expect(helper.highlight_tokens_in_event(event, tokens))
+ .to eql 'attachment_<span class="search-highlight token-0">filename</span>.txt'
+ end
+ end
+
+ context 'with the token in neither' do
+ let(:tokens) { %w(bogus) }
+
+ it 'shows the description (without highlight)' do
+ expect(helper.highlight_tokens_in_event(event, tokens))
+ .to eql 'The description of the event'
+ end
end
end
end