Skip to content

Commit

Permalink
Search by multiple keywords regardless the order.
Browse files Browse the repository at this point in the history
Fixes: CV2-5129.
  • Loading branch information
caiosba committed Aug 28, 2024
1 parent 32bd426 commit 1598516
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 5 additions & 1 deletion app/models/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,11 @@ def filtered_fact_checks(filters = {})
query = query.where('fact_checks.report_status' => filters[:report_status].to_a.map(&:to_s)) unless filters[:report_status].blank?

# Filter by text
query = query.where('(fact_checks.title ILIKE ? OR fact_checks.url ILIKE ? OR fact_checks.summary ILIKE ?)', *["%#{filters[:text]}%"]*3) if filters[:text].to_s.size > 2
if filters[:text].to_s.size > 2
tsquery = Team.sanitize_sql_array(["to_tsquery(?)", filters[:text].split(/\s+/).map(&:strip).join(' & ')]) # FIXME: May not work for all languages

This comment has been minimized.

Copy link
@computermacgyver

computermacgyver Aug 28, 2024

Contributor

Not sure if plainto_tsquery is any better for other languages? It does seem simpler, however as we don't have to split the query: https://www.postgresql.org/docs/current/textsearch-controls.html

tsvector = "to_tsvector('simple', coalesce(title, '') || ' ' || coalesce(summary, '') || coalesce(url, ''))"
query = query.where(Arel.sql("#{tsvector} @@ #{tsquery}"))
end

# Exclude the ones already applied to a target item
target = ProjectMedia.find_by_id(filters[:target_id].to_i)
Expand Down
13 changes: 13 additions & 0 deletions test/models/team_2_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1527,4 +1527,17 @@ def setup
tbi.save!
assert_equal ['none', 'link_preview'], t.available_newsletter_header_types
end

test "should search for fact-checks by keywords" do
Sidekiq::Testing.fake!
t = create_team
create_fact_check title: 'Some Other Test', claim_description: create_claim_description(project_media: create_project_media(team: t))
create_fact_check title: 'Bar Bravo Foo Test', claim_description: create_claim_description(project_media: create_project_media(team: t))
create_fact_check title: 'Foo Alpha Bar Test', claim_description: create_claim_description(project_media: create_project_media(team: t))
assert_equal 3, t.filtered_fact_checks.count
assert_equal 3, t.filtered_fact_checks(text: 'Test').count
assert_equal 2, t.filtered_fact_checks(text: 'Foo Bar').count
assert_equal 1, t.filtered_fact_checks(text: 'Foo Bar Bravo').count
assert_equal 1, t.filtered_fact_checks(text: 'Foo Bar Alpha').count
end
end

0 comments on commit 1598516

Please sign in to comment.