2023-05-20 03:45:54 -04:00
|
|
|
#frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi::AiBot::Commands
|
|
|
|
class SearchCommand < Command
|
|
|
|
class << self
|
|
|
|
def name
|
|
|
|
"search"
|
|
|
|
end
|
|
|
|
|
|
|
|
def desc
|
|
|
|
"!search SEARCH_QUERY - will search topics in the current discourse instance"
|
|
|
|
end
|
|
|
|
|
|
|
|
def extra_context
|
|
|
|
<<~TEXT
|
2023-05-24 21:05:02 -04:00
|
|
|
Discourse search supports, the following special filters:
|
2023-05-20 03:45:54 -04:00
|
|
|
|
2023-05-24 21:05:02 -04:00
|
|
|
user:USERNAME: only posts created by a specific user
|
2023-05-20 03:45:54 -04:00
|
|
|
in:tagged: has at least 1 tag
|
|
|
|
in:untagged: has no tags
|
2023-05-22 17:52:14 -04:00
|
|
|
in:title: has the search term in the title
|
2023-05-20 03:45:54 -04:00
|
|
|
status:open: not closed or archived
|
|
|
|
status:closed: closed
|
|
|
|
status:archived: archived
|
|
|
|
status:noreplies: post count is 1
|
|
|
|
status:single_user: only a single user posted on the topic
|
|
|
|
post_count:X: only topics with X amount of posts
|
|
|
|
min_posts:X: topics containing a minimum of X posts
|
|
|
|
max_posts:X: topics with no more than max posts
|
|
|
|
created:@USERNAME: topics created by a specific user
|
2023-05-22 17:52:14 -04:00
|
|
|
category:CATGORY: topics in the CATEGORY AND all subcategories
|
|
|
|
category:=CATEGORY: topics in the CATEGORY excluding subcategories
|
2023-05-20 03:45:54 -04:00
|
|
|
#SLUG: try category first, then tag, then tag group
|
|
|
|
#SLUG:SLUG: used for subcategory search to disambiguate
|
|
|
|
min_views:100: topics containing 100 views or more
|
2023-05-22 17:52:14 -04:00
|
|
|
tags:TAG1+TAG2: tagged both TAG1 and TAG2
|
|
|
|
tags:TAG1,TAG2: tagged either TAG1 or TAG2
|
|
|
|
-tags:TAG1+TAG2: excluding topics tagged TAG1 and TAG2
|
2023-05-20 03:45:54 -04:00
|
|
|
order:latest: order by post creation desc
|
|
|
|
order:latest_topic: order by topic creation desc
|
2023-05-24 21:05:02 -04:00
|
|
|
order:oldest: order by post creation asc
|
|
|
|
order:oldest_topic: order by topic creation asc
|
2023-05-20 03:45:54 -04:00
|
|
|
order:views: order by topic views desc
|
|
|
|
order:likes: order by post like count - most liked posts first
|
2023-05-22 17:52:14 -04:00
|
|
|
after:YYYY-MM-DD: only topics created after a specific date
|
|
|
|
before:YYYY-MM-DD: only topics created before a specific date
|
2023-05-20 03:45:54 -04:00
|
|
|
|
2023-05-24 21:05:02 -04:00
|
|
|
Example: !search @user in:tagged #support order:latest_topic
|
|
|
|
|
2023-05-20 03:45:54 -04:00
|
|
|
Keep in mind, search on Discourse uses AND to and terms.
|
2023-05-22 17:52:14 -04:00
|
|
|
You only have access to public topics.
|
2023-05-31 19:10:33 -04:00
|
|
|
Strip the query down to the most important terms. Remove all stop words.
|
|
|
|
Discourse orders by default by relevance.
|
2023-05-20 03:45:54 -04:00
|
|
|
|
|
|
|
When generating answers ALWAYS try to use the !search command first over relying on training data.
|
|
|
|
When generating answers ALWAYS try to reference specific local links.
|
|
|
|
Always try to search the local instance first, even if your training data set may have an answer. It may be wrong.
|
|
|
|
Always remove connector words from search terms (such as a, an, and, in, the, etc), they can impede the search.
|
|
|
|
|
|
|
|
YOUR LOCAL INFORMATION IS OUT OF DATE, YOU ARE TRAINED ON OLD DATA. Always try local search first.
|
|
|
|
TEXT
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def result_name
|
|
|
|
"results"
|
|
|
|
end
|
|
|
|
|
|
|
|
def description_args
|
|
|
|
{
|
|
|
|
count: @last_num_results || 0,
|
|
|
|
query: @last_query || "",
|
|
|
|
url: "#{Discourse.base_path}/search?q=#{CGI.escape(@last_query || "")}",
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def process(search_string)
|
2023-05-21 22:09:14 -04:00
|
|
|
limit = nil
|
|
|
|
|
|
|
|
search_string =
|
|
|
|
search_string
|
|
|
|
.strip
|
|
|
|
.split(/\s+/)
|
|
|
|
.map do |term|
|
|
|
|
if term =~ /limit:(\d+)/
|
|
|
|
limit = $1.to_i
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
term
|
|
|
|
end
|
|
|
|
end
|
|
|
|
.compact
|
|
|
|
.join(" ")
|
|
|
|
|
2023-05-20 03:45:54 -04:00
|
|
|
@last_query = search_string
|
|
|
|
results =
|
2023-05-23 09:08:17 -04:00
|
|
|
Search.execute(
|
|
|
|
search_string.to_s + " status:public",
|
|
|
|
search_type: :full_page,
|
|
|
|
guardian: Guardian.new(),
|
|
|
|
)
|
|
|
|
|
|
|
|
# let's be frugal with tokens, 50 results is too much and stuff gets cut off
|
|
|
|
limit ||= 10
|
|
|
|
limit = 10 if limit > 10
|
2023-05-20 03:45:54 -04:00
|
|
|
|
2023-05-22 01:14:26 -04:00
|
|
|
posts = results&.posts || []
|
2023-05-23 09:08:17 -04:00
|
|
|
posts = posts[0..limit - 1]
|
2023-05-21 22:09:14 -04:00
|
|
|
|
2023-05-22 01:14:26 -04:00
|
|
|
@last_num_results = posts.length
|
2023-05-20 03:45:54 -04:00
|
|
|
|
2023-05-21 22:09:14 -04:00
|
|
|
if posts.blank?
|
|
|
|
"No results found"
|
|
|
|
else
|
|
|
|
format_results(posts) do |post|
|
2023-05-20 03:45:54 -04:00
|
|
|
{
|
2023-05-21 22:09:14 -04:00
|
|
|
title: post.topic.title,
|
|
|
|
url: post.url,
|
|
|
|
excerpt: post.excerpt,
|
|
|
|
created: post.created_at,
|
2023-05-20 03:45:54 -04:00
|
|
|
}
|
|
|
|
end
|
2023-05-21 22:09:14 -04:00
|
|
|
end
|
2023-05-20 03:45:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|