mirror of
https://github.com/discourse/discourse.git
synced 2025-01-18 14:03:18 +08:00
Exclude category definition topics from similar search
This commit is contained in:
parent
2c4ada6c7d
commit
0d238db43c
|
@ -319,13 +319,21 @@ class Topic < ActiveRecord::Base
|
||||||
return [] unless raw.present?
|
return [] unless raw.present?
|
||||||
|
|
||||||
# For now, we only match on title. We'll probably add body later on, hence the API hook
|
# For now, we only match on title. We'll probably add body later on, hence the API hook
|
||||||
Topic.select(sanitize_sql_array(["topics.*, similarity(topics.title, :title) AS similarity", title: title]))
|
similar = Topic.select(sanitize_sql_array(["topics.*, similarity(topics.title, :title) AS similarity", title: title]))
|
||||||
.visible
|
.visible
|
||||||
.where(closed: false, archived: false)
|
.where(closed: false, archived: false)
|
||||||
.secured(Guardian.new(user))
|
.secured(Guardian.new(user))
|
||||||
.listable_topics
|
.listable_topics
|
||||||
.limit(SiteSetting.max_similar_results)
|
.limit(SiteSetting.max_similar_results)
|
||||||
.order('similarity desc')
|
.order('similarity desc')
|
||||||
|
|
||||||
|
# Exclude category definitions from similar topic suggestions
|
||||||
|
exclude_topic_ids = Category.pluck(:topic_id).compact!
|
||||||
|
if exclude_topic_ids.present?
|
||||||
|
similar = similar.where("topics.id NOT IN (?)", exclude_topic_ids)
|
||||||
|
end
|
||||||
|
|
||||||
|
similar
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_status(status, enabled, user)
|
def update_status(status, enabled, user)
|
||||||
|
|
|
@ -202,6 +202,14 @@ describe Topic do
|
||||||
Topic.similar_to(nil, nil).should be_blank
|
Topic.similar_to(nil, nil).should be_blank
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "with a category definition" do
|
||||||
|
let!(:category) { Fabricate(:category) }
|
||||||
|
|
||||||
|
it "excludes the category definition topic from similar_to" do
|
||||||
|
Topic.similar_to('category definition for', "no body").should be_blank
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'with a similar topic' do
|
context 'with a similar topic' do
|
||||||
let!(:topic) { Fabricate(:topic, title: "Evil trout is the dude who posted this topic") }
|
let!(:topic) { Fabricate(:topic, title: "Evil trout is the dude who posted this topic") }
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user