Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: include tag and category context in search #217

Merged
merged 3 commits into from Sep 12, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion lib/modules/ai_bot/commands/search_command.rb
Expand Up @@ -154,17 +154,33 @@ def process(**search_args)
end

@last_num_results = posts.length
# this is the general pattern from core
# if there are millions of hidden tags it may fail
hidden_tags = nil

if posts.blank?
{ args: search_args, rows: [], instruction: "nothing was found, expand your search" }
else
format_results(posts, args: search_args) do |post|
{
category_names = [
post.topic.category&.parent_category&.name,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how the search command is executed in the background. Will it cause n+1 in category and parent_category attributes too?

post.topic.category&.name,
].compact.join(" > ")
row = {
title: post.topic.title,
url: Discourse.base_path + post.url,
excerpt: post.excerpt,
created: post.created_at,
category: category_names,
}

if SiteSetting.tagging_enabled
hidden_tags ||= DiscourseTagging.hidden_tag_names
# using map over pluck to avoid n+1 (assuming caller preloading)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of assuming can we check whether the association is loaded or not? like post.topic.association(:tags).loaded?. And we can raise an error if it's not loaded.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is the responsibility of Search or SemanticSearch ... but maybe we can raise a warning in case this happens.

tags = post.topic.tags.map(&:name) - hidden_tags
row[:tags] = tags.join(", ") if tags.present?
end
row
end
end
end
Expand Down
18 changes: 9 additions & 9 deletions spec/lib/modules/ai_bot/commands/read_command_spec.rb
Expand Up @@ -9,19 +9,19 @@
fab!(:tag_funny) { Fabricate(:tag, name: "funny") }
fab!(:tag_sad) { Fabricate(:tag, name: "sad") }
fab!(:tag_hidden) { Fabricate(:tag, name: "hidden") }
fab!(:staff_tag_group) { Fabricate(:tag_group, name: "Staff only", tag_names: ["hidden"]) }
fab!(:staff_tag_group) do
tag_group = Fabricate.build(:tag_group, name: "Staff only", tag_names: ["hidden"])

tag_group.permissions = [
[Group::AUTO_GROUPS[:staff], TagGroupPermission.permission_types[:full]],
]
tag_group.save!
tag_group
end
fab!(:topic_with_tags) do
Fabricate(:topic, category: category, tags: [tag_funny, tag_sad, tag_hidden])
end

let(:staff) { Group::AUTO_GROUPS[:staff] }
let(:full) { TagGroupPermission.permission_types[:full] }

before do
staff_tag_group.permissions = [[staff, full]]
staff_tag_group.save!
end

describe "#process" do
it "can read a topic" do
topic_id = topic_with_tags.id
Expand Down
42 changes: 37 additions & 5 deletions spec/lib/modules/ai_bot/commands/search_command_spec.rb
Expand Up @@ -9,9 +9,28 @@
before { SearchIndexer.enable }
after { SearchIndexer.disable }

fab!(:parent_category) { Fabricate(:category, name: "animals") }
fab!(:category) { Fabricate(:category, parent_category: parent_category, name: "amazing-cat") }

fab!(:tag_funny) { Fabricate(:tag, name: "funny") }
fab!(:tag_sad) { Fabricate(:tag, name: "sad") }
fab!(:tag_hidden) { Fabricate(:tag, name: "hidden") }
fab!(:staff_tag_group) do
tag_group = Fabricate.build(:tag_group, name: "Staff only", tag_names: ["hidden"])

tag_group.permissions = [
[Group::AUTO_GROUPS[:staff], TagGroupPermission.permission_types[:full]],
]
tag_group.save!
tag_group
end
fab!(:topic_with_tags) do
Fabricate(:topic, category: category, tags: [tag_funny, tag_sad, tag_hidden])
end

describe "#process" do
it "can handle no results" do
post1 = Fabricate(:post)
post1 = Fabricate(:post, topic: topic_with_tags)
search = described_class.new(bot_user: bot_user, post: post1, args: nil)

results = search.process(query: "order:fake ABDDCDCEDGDG")
Expand Down Expand Up @@ -42,7 +61,7 @@
hyde_embedding,
)

post1 = Fabricate(:post)
post1 = Fabricate(:post, topic: topic_with_tags)
search = described_class.new(bot_user: bot_user, post: post1, args: nil)

DiscourseAi::Embeddings::VectorRepresentations::AllMpnetBaseV2
Expand All @@ -60,16 +79,30 @@
it "supports subfolder properly" do
Discourse.stubs(:base_path).returns("/subfolder")

post1 = Fabricate(:post)
post1 = Fabricate(:post, topic: topic_with_tags)

search = described_class.new(bot_user: bot_user, post: post1, args: nil)

results = search.process(limit: 1, user: post1.user.username)
expect(results[:rows].to_s).to include("/subfolder" + post1.url)
end

it "returns category and tags" do
post1 = Fabricate(:post, topic: topic_with_tags)
search = described_class.new(bot_user: bot_user, post: post1, args: nil)
results = search.process(user: post1.user.username)

row = results[:rows].first
category = row[results[:column_names].index("category")]

expect(category).to eq("animals > amazing-cat")

tags = row[results[:column_names].index("tags")]
expect(tags).to eq("funny, sad")
end

it "can handle limits" do
post1 = Fabricate(:post)
post1 = Fabricate(:post, topic: topic_with_tags)
_post2 = Fabricate(:post, user: post1.user)
_post3 = Fabricate(:post, user: post1.user)

Expand All @@ -78,7 +111,6 @@

results = search.process(limit: 2, user: post1.user.username)

expect(results[:column_names].length).to eq(4)
expect(results[:rows].length).to eq(2)

# just searching for everything
Expand Down