Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/embeddings/semantic_topic_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ def list_semantic_related_topics(topic)
create_list(:semantic_related, query_opts) do |topics|
candidate_ids = DiscourseAi::Embeddings::SemanticRelated.new.related_topic_ids_for(topic)

list =
topics
.where.not(id: topic.id)
.where(id: candidate_ids)
.order("array_position(ARRAY#{candidate_ids}, topics.id)") # array_position forces the order of the topics to be preserved
list = topics.where.not(id: topic.id).where(id: candidate_ids)

list = DiscoursePluginRegistry.apply_modifier(:semantic_related_topics_query, list)

# array_position forces the order of the topics to be preserved
list = list.order("array_position(ARRAY#{candidate_ids}, topics.id)")
list = remove_muted(list, @user, query_opts)
end
end
Expand Down
22 changes: 22 additions & 0 deletions spec/lib/modules/embeddings/semantic_topic_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,28 @@ def stub_semantic_search_with(results)
)
end
end

context "with semantic_related_topics_query modifier registered" do
fab!(:included_topic) { Fabricate(:topic) }
fab!(:excluded_topic) { Fabricate(:topic) }

before { stub_semantic_search_with([included_topic.id, excluded_topic.id]) }

let(:modifier_block) { Proc.new { |query| query.where.not(id: excluded_topic.id) } }

it "Allows modifications to default results (excluding a topic in this case)" do
plugin_instance = Plugin::Instance.new
plugin_instance.register_modifier(:semantic_related_topics_query, &modifier_block)

expect(topic_query.list_semantic_related_topics(target).topics).to eq([included_topic])
ensure
DiscoursePluginRegistry.unregister_modifier(
plugin_instance,
:semantic_related_topics_query,
&modifier_block
)
end
end
end
end
end