Skip to content

Commit

Permalink
Fixes zammad#5099 - Knowledge base search result order is wrong with …
Browse files Browse the repository at this point in the history
…ES backend
  • Loading branch information
mantas committed Jul 26, 2024
1 parent a734ba9 commit a6f8be0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/controllers/knowledge_base/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def search
flavor: params[:flavor],
index: params[:index],
limit: params[:limit],
highlight_enabled: params[:highlight_enabled]
highlight_enabled: params[:highlight_enabled],
order_by: { updated_at: :desc }
)

include_locale = params[:include_locale] && KnowledgeBase.with_multiple_locales_exists?
Expand Down
43 changes: 43 additions & 0 deletions spec/requests/knowledge_base/search_with_details_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,47 @@
expect(json_response['result'].count).to be 7
end
end

context 'when sorting' do
let(:answers) do
Array.new(3) do |nth|
travel nth.seconds do
create(:knowledge_base_answer, :published, :with_attachment,
category: category,
translation_attributes: { title: "#{search_phrase} #{nth}" })
end
end
end

let(:search_phrase) { 'paging test' }

before do |example|
answers

travel(3.hours) { answers[1].translations.first.touch }

next if !example.metadata[:searchindex]

searchindex_model_reload([KnowledgeBase::Translation, KnowledgeBase::Category::Translation, KnowledgeBase::Answer::Translation])
end

shared_examples 'test sorting' do
it 'sorts answers from most up-to-date to oldest' do
post endpoint, params: { query: search_phrase }

returned_ids = json_response['details'].pluck('id')
expected_ids = [answers[1], answers[2], answers[0]].map { |elem| elem.translations.first.id }

expect(returned_ids).to eq expected_ids
end
end

context 'with elasticsearch' do
include_examples 'test sorting'
end

context 'with no elasticsearch', searchindex: false do
include_examples 'test sorting'
end
end
end

0 comments on commit a6f8be0

Please sign in to comment.