From e5537d4c775a0648c235cdb45fad63d6e1130a32 Mon Sep 17 00:00:00 2001 From: Rafael dos Santos Silva Date: Tue, 9 May 2023 15:30:50 -0300 Subject: [PATCH] FEATURE: Allow excluding closed topics from semantic related (#55) --- config/settings.yml | 1 + lib/modules/embeddings/semantic_related.rb | 13 ++++++++----- lib/modules/embeddings/topic.rb | 2 +- .../lib/modules/embeddings/semantic_related_spec.rb | 12 +++++++++++- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/config/settings.yml b/config/settings.yml index d2e16797..0d3ed860 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -148,6 +148,7 @@ plugins: ai_embeddings_generate_for_pms: false ai_embeddings_semantic_related_topics_enabled: false ai_embeddings_semantic_related_topics: 5 + ai_embeddings_semantic_related_include_closed_topics: true ai_embeddings_pg_connection_string: "" ai_embeddings_semantic_search_enabled: default: false diff --git a/lib/modules/embeddings/semantic_related.rb b/lib/modules/embeddings/semantic_related.rb index 93b50bec..f6b32af5 100644 --- a/lib/modules/embeddings/semantic_related.rb +++ b/lib/modules/embeddings/semantic_related.rb @@ -34,13 +34,16 @@ module DiscourseAi return ::Topic.none end - # array_position forces the order of the topics to be preserved - ::Topic - .visible - .listable_topics - .secured + topic_list = ::Topic.visible.listable_topics.secured + + unless SiteSetting.ai_embeddings_semantic_related_include_closed_topics + topic_list = topic_list.where(closed: false) + end + + topic_list .where("id <> ?", topic.id) .where(id: candidate_ids) + # array_position forces the order of the topics to be preserved .order("array_position(ARRAY#{candidate_ids}, id)") .limit(SiteSetting.ai_embeddings_semantic_related_topics) end diff --git a/lib/modules/embeddings/topic.rb b/lib/modules/embeddings/topic.rb index 0044efd5..8e009a34 100644 --- a/lib/modules/embeddings/topic.rb +++ b/lib/modules/embeddings/topic.rb @@ -33,7 +33,7 @@ module DiscourseAi topic_id = :topic_id LIMIT 1 ) - LIMIT 11 + LIMIT 100 SQL # Happens when the topic doesn't have any embeddings diff --git a/spec/lib/modules/embeddings/semantic_related_spec.rb b/spec/lib/modules/embeddings/semantic_related_spec.rb index e9b8b566..e753f6cd 100644 --- a/spec/lib/modules/embeddings/semantic_related_spec.rb +++ b/spec/lib/modules/embeddings/semantic_related_spec.rb @@ -11,6 +11,7 @@ describe DiscourseAi::Embeddings::SemanticRelated do fab!(:private_topic) { Fabricate(:private_message_topic) } fab!(:secured_category) { Fabricate(:category, read_restricted: true) } fab!(:secured_category_topic) { Fabricate(:topic, category: secured_category) } + fab!(:closed_topic) { Fabricate(:topic, closed: true) } before { SiteSetting.ai_embeddings_semantic_related_topics_enabled = true } @@ -20,7 +21,7 @@ describe DiscourseAi::Embeddings::SemanticRelated do DiscourseAi::Embeddings::Topic .any_instance .expects(:symmetric_semantic_search) - .returns(Topic.unscoped.order(id: :desc).limit(10).pluck(:id)) + .returns(Topic.unscoped.order(id: :desc).limit(100).pluck(:id)) end after { Discourse.cache.clear } @@ -30,10 +31,19 @@ describe DiscourseAi::Embeddings::SemanticRelated do expect(results).to include(normal_topic_1) expect(results).to include(normal_topic_2) expect(results).to include(normal_topic_3) + expect(results).to include(closed_topic) expect(results).to_not include(target) expect(results).to_not include(unlisted_topic) expect(results).to_not include(private_topic) expect(results).to_not include(secured_category_topic) end + + context "when ai_embeddings_semantic_related_include_closed_topics is false" do + before { SiteSetting.ai_embeddings_semantic_related_include_closed_topics = false } + it "do not return closed topics" do + results = described_class.candidates_for(target).to_a + expect(results).to_not include(closed_topic) + end + end end end