Skip to content

Commit

Permalink
CV2-3664: sort by fact-check published_at date (#1646)
Browse files Browse the repository at this point in the history
  • Loading branch information
melsawy authored Sep 6, 2023
1 parent 1b6e81a commit f2e376b
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 5 deletions.
1 change: 0 additions & 1 deletion app/lib/check_elastic_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def create_elasticsearch_doc_bg(options)
ms.attributes[:parent_id] = self.id
ms.attributes[:created_at] = self.created_at.utc
ms.attributes[:updated_at] = self.updated_at.utc
ms.attributes[:media_published_at] = self.media_published_at
ms.attributes[:source_id] = self.source_id
# Intial nested objects with []
['comments', 'tags', 'task_responses', 'assigned_user_ids', 'requests'].each{ |f| ms.attributes[f] = [] }
Expand Down
1 change: 1 addition & 0 deletions app/models/concerns/project_media_cached_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def title_or_description_update

cached_field :fact_check_published_on,
start_as: 0,
update_es: true,
recalculate: :recalculate_fact_check_published_on,
update_on: [FACT_CHECK_EVENT]

Expand Down
6 changes: 3 additions & 3 deletions app/models/project_media.rb
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,9 @@ def add_extra_elasticsearch_data(ms)
ms.attributes[:language] = self.get_dynamic_annotation('language')&.get_field_value('language')
# set fields with integer value including cached fields
fields_i = [
'archived', 'sources_count', 'linked_items_count', 'share_count',
'last_seen', 'demand', 'user_id', 'read', 'suggestions_count',
'related_count', 'reaction_count', 'comment_count', 'media_published_at', 'unmatched'
'archived', 'sources_count', 'linked_items_count', 'share_count','last_seen', 'demand', 'user_id',
'read', 'suggestions_count','related_count', 'reaction_count', 'comment_count', 'media_published_at',
'unmatched', 'fact_check_published_on'
]
fields_i.each{ |f| ms.attributes[f] = self.send(f).to_i }
# add more cached fields
Expand Down
2 changes: 2 additions & 0 deletions app/repositories/media_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,7 @@ class MediaSearch
indexes :unmatched, { type: 'long' }

indexes :report_language, { type: 'keyword', normalizer: 'check' }

indexes :fact_check_published_on, { type: 'long' }
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class AddMappingForFactCheckPublishedOnField < ActiveRecord::Migration[6.1]
def change
options = {
index: CheckElasticSearchModel.get_index_alias,
body: {
properties: {
fact_check_published_on: { type: 'long' },
}
}
}
$repository.client.indices.put_mapping options
end
end
3 changes: 2 additions & 1 deletion lib/check_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ def initialize(options, file = nil, team_id = Team.current&.id)
'type_of_media' => 'type_of_media', 'title' => 'title_index', 'creator_name' => 'creator_name',
'cluster_size' => 'cluster_size', 'cluster_first_item_at' => 'cluster_first_item_at',
'cluster_last_item_at' => 'cluster_last_item_at', 'cluster_requests_count' => 'cluster_requests_count',
'cluster_published_reports_count' => 'cluster_published_reports_count', 'score' => '_score'
'cluster_published_reports_count' => 'cluster_published_reports_count', 'score' => '_score',
'fact_check_published_on' => 'fact_check_published_on'
}

def team_condition(team_id = nil)
Expand Down
18 changes: 18 additions & 0 deletions test/controllers/elastic_search_9_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,5 +246,23 @@ def setup
end
end

test "should sort by fact-check published on data" do
RequestStore.store[:skip_cached_field_update] = false
t = create_team
pm1 = create_project_media team: t, disable_es_callbacks: false
pm2 = create_project_media team: t, disable_es_callbacks: false
pm3 = create_project_media team: t, disable_es_callbacks: false
sleep 2
cd = create_claim_description project_media: pm3
fc = create_fact_check claim_description: cd
cd = create_claim_description project_media: pm1
fc = create_fact_check claim_description: cd
sleep 2
result = CheckSearch.new({ sort: 'fact_check_published_on', sort_type: 'asc' }.to_json, nil, t.id)
assert_equal [pm2.id, pm3.id, pm1.id], result.medias.map(&:id)
result = CheckSearch.new({ sort: 'fact_check_published_on', sort_type: 'desc' }.to_json, nil, t.id)
assert_equal [pm1.id, pm3.id, pm2.id], result.medias.map(&:id)
end

# Please add new tests to test/controllers/elastic_search_10_test.rb
end

0 comments on commit f2e376b

Please sign in to comment.