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

Search for fact-checks by multiple keywords regardless the order. #2012

Merged
merged 5 commits into from
Aug 28, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions app/models/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ def filtered_explainers(filters = {})
query = query.where(updated_at: Range.new(*format_times_search_range_filter(JSON.parse(filters[:updated_at]), nil))) unless filters[:updated_at].blank?

# Filter by text
query = query.where('(title ILIKE ? OR url ILIKE ? OR description ILIKE ?)', *["%#{filters[:text]}%"]*3) if filters[:text].to_s.size > 2
query = self.filter_by_keywords(query, filters, 'Explainer') if filters[:text].to_s.size > 2

# Exclude the ones already applied to a target item
target = ProjectMedia.find_by_id(filters[:target_id].to_i)
Expand Down Expand Up @@ -535,7 +535,7 @@ 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
query = self.filter_by_keywords(query, filters) if filters[:text].to_s.size > 2

# Exclude the ones already applied to a target item
target = ProjectMedia.find_by_id(filters[:target_id].to_i)
Expand All @@ -544,6 +544,16 @@ def filtered_fact_checks(filters = {})
query
end

def filter_by_keywords(query, filters, type = 'FactCheck')
tsquery = Team.sanitize_sql_array(["websearch_to_tsquery(?)", filters[:text]]) # FIXME: May not work for all languages
if type == 'FactCheck'
tsvector = "to_tsvector('simple', coalesce(title, '') || ' ' || coalesce(summary, '') || coalesce(url, ''))"
else
tsvector = "to_tsvector('simple', coalesce(title, '') || ' ' || coalesce(description, '') || coalesce(url, ''))"
end
query.where(Arel.sql("#{tsvector} @@ #{tsquery}"))
end

# private
#
# Please add private methods to app/models/concerns/team_private.rb
Expand Down
25 changes: 25 additions & 0 deletions test/models/team_2_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1527,4 +1527,29 @@ def setup
tbi.save!
assert_equal ['none', 'link_preview'], t.available_newsletter_header_types
end

test "should search for fact-checks and explainers by keywords" do
Sidekiq::Testing.fake!
t = create_team
# Fact-checks
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
assert_equal 0, t.filtered_fact_checks(text: 'Foo Bar Delta').count
# Explainer
create_explainer title: 'Some Other Test', team: t
create_explainer title: 'Bar Bravo Foo Test', team: t
create_explainer title: 'Foo Alpha Bar Test', team: t
assert_equal 3, t.filtered_explainers.count
assert_equal 3, t.filtered_explainers(text: 'Test').count
assert_equal 2, t.filtered_explainers(text: 'Foo Bar').count
assert_equal 1, t.filtered_explainers(text: 'Foo Bar Bravo').count
assert_equal 1, t.filtered_explainers(text: 'Foo Bar Alpha').count
assert_equal 0, t.filtered_fact_checks(text: 'Foo Bar Delta').count
end
end
Loading