diff --git a/app/lib/check_elastic_search.rb b/app/lib/check_elastic_search.rb index 2c79880953..cc04fb646a 100644 --- a/app/lib/check_elastic_search.rb +++ b/app/lib/check_elastic_search.rb @@ -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] = [] } diff --git a/app/models/concerns/project_media_cached_fields.rb b/app/models/concerns/project_media_cached_fields.rb index 2cf623976c..1476300167 100644 --- a/app/models/concerns/project_media_cached_fields.rb +++ b/app/models/concerns/project_media_cached_fields.rb @@ -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] diff --git a/app/models/project_media.rb b/app/models/project_media.rb index bfc6c46574..f7ea917a39 100644 --- a/app/models/project_media.rb +++ b/app/models/project_media.rb @@ -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 diff --git a/app/repositories/media_search.rb b/app/repositories/media_search.rb index 31da9ed4f4..33e969062a 100644 --- a/app/repositories/media_search.rb +++ b/app/repositories/media_search.rb @@ -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 diff --git a/db/migrate/20230906054844_add_mapping_for_fact_check_published_on_field.rb b/db/migrate/20230906054844_add_mapping_for_fact_check_published_on_field.rb new file mode 100644 index 0000000000..bce79efa40 --- /dev/null +++ b/db/migrate/20230906054844_add_mapping_for_fact_check_published_on_field.rb @@ -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 diff --git a/lib/check_search.rb b/lib/check_search.rb index 11fbc24188..5d2fb15f0a 100644 --- a/lib/check_search.rb +++ b/lib/check_search.rb @@ -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) diff --git a/test/controllers/elastic_search_9_test.rb b/test/controllers/elastic_search_9_test.rb index 262bff662c..c799fc6cb1 100644 --- a/test/controllers/elastic_search_9_test.rb +++ b/test/controllers/elastic_search_9_test.rb @@ -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