Skip to content

Commit

Permalink
CV2-6038: set value with nil instead of remove field from ES doc
Browse files Browse the repository at this point in the history
  • Loading branch information
melsawy committed Feb 6, 2025
1 parent bfe9d25 commit 692e7fd
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 27 deletions.
14 changes: 0 additions & 14 deletions app/lib/check_elastic_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ def update_elasticsearch_doc(keys, data = {}, pm_id = nil, skip_get_data = false
ElasticSearchWorker.perform_in(1.second, YAML::dump(model), YAML::dump(options), 'update_doc')
end

def remove_fields_from_elasticsearch_doc(keys, pm_id)
return if self.disable_es_callbacks || RequestStore.store[:disable_es_callbacks]
options = { keys: keys, pm_id: pm_id }
model = { klass: self.class.name, id: self.id }
ElasticSearchWorker.perform_in(1.second, YAML::dump(model), YAML::dump(options), 'remove_fields')
end

def update_recent_activity(obj)
# update `updated_at` date for both PG & ES
updated_at = Time.now
Expand Down Expand Up @@ -72,13 +65,6 @@ def update_elasticsearch_doc_bg(options)
end
end

def remove_fields_from_elasticsearch_doc_bg(options)
options[:keys].each do |k|
source = "ctx._source.remove('#{k}')"
$repository.client.update index: CheckElasticSearchModel.get_index_alias, id: options[:doc_id], body: { script: { source: source } }
end
end

# Get a fresh data based on data(Hash)
def get_fresh_value(data)
value = data['default']
Expand Down
24 changes: 12 additions & 12 deletions app/models/claim_description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ def text_fields

def article_elasticsearch_data(action = 'create_or_update')
return if self.project_media_id.nil? || self.disable_es_callbacks || RequestStore.store[:disable_es_callbacks]
if action == 'destroy'
self.remove_fields_from_elasticsearch_doc(['claim_description_content', 'claim_description_context'], self.project_media_id)
else
data = { 'claim_description_content' => self.description, 'claim_description_context' => self.context }
self.index_in_elasticsearch(self.project_media_id, data)
end
data = action == 'destroy' ? {
'claim_description_content' => nil,
'claim_description_context' => nil
} : {
'claim_description_content' => self.description,
'claim_description_context' => self.context
}
self.index_in_elasticsearch(self.project_media_id, data)
end

def project_media_was
Expand Down Expand Up @@ -90,13 +92,11 @@ def update_report
fact_check.save!
end
# update ES
# Remove claim_description fields
self.remove_fields_from_elasticsearch_doc(['claim_description_content', 'claim_description_context'], pm.id)
# clear claim_description fields
data = { 'claim_description_content' => nil, 'claim_description_context' => nil }
# clear fact-check values
unless self.fact_check.nil?
data = { 'fact_check_title' => '', 'fact_check_summary' => '', 'fact_check_url' => '', 'fact_check_languages' => [] }
self.fact_check.index_in_elasticsearch(pm.id, data)
end
data.merge!({ 'fact_check_title' => '', 'fact_check_summary' => '', 'fact_check_url' => '', 'fact_check_languages' => [] }) unless self.fact_check.nil?
self.index_in_elasticsearch(pm.id, data)
end
end

Expand Down
1 change: 0 additions & 1 deletion app/workers/elastic_search_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def perform(model_data, options, type)
ops = {
'create_doc' => 'create_elasticsearch_doc_bg',
'update_doc' => 'update_elasticsearch_doc_bg',
'remove_fields' => 'remove_fields_from_elasticsearch_doc_bg',
'update_doc_team' => 'update_elasticsearch_doc_team_bg',
'create_update_doc_nested' => 'create_update_nested_obj_bg',
'destroy_doc' => 'destroy_elasticsearch_doc',
Expand Down

0 comments on commit 692e7fd

Please sign in to comment.