FEATURE: allow consumers to parse a search string (#23528)

This extends search so it can have consumers that:

1. Can split off "term" from various advanced filters and orders
2. Can build a relation of either order or filter

It also moves a lot of stuff around in the search class for clarity.

Two new APIs are exposed:

`.apply_filter` to apply all the special filters to a posts/topics relation
`.apply_order` to force a particular order (eg: order:latest)

This can then be used by semantic search in Discourse AI
This commit is contained in:
Sam
2023-09-12 16:21:01 +10:00
committed by GitHub
parent f08c6d2756
commit f25849501d
2 changed files with 153 additions and 107 deletions

View File

@@ -2748,4 +2748,21 @@ RSpec.describe Search do
expect(result.posts.pluck(:id)).to eq([post1.id, post2.id])
end
end
describe "Extensibility features of search" do
it "is possible to parse queries" do
term = "hello l status:closed"
search = Search.new(term)
posts = Post.all.includes(:topic)
posts = search.apply_filters(posts)
posts = search.apply_order(posts)
sql = posts.to_sql
expect(search.term).to eq("hello")
expect(sql).to include("ORDER BY posts.created_at DESC")
expect(sql).to match(/where.*topics.closed/i)
end
end
end