# File app/models/elasticsearch/index.rb, line 108 def areas_for_elasticsearch areas_for_elasticsearch=[] self.areas.each do |area| areas_for_elasticsearch << [area.name_en, area.name_es, area.name_eu].join('|') end areas_for_elasticsearch.join(';') end
# File app/models/elasticsearch/index.rb, line 116 def date_for_elasticsearch if self.is_a?(Album) || self.is_a?(Proposal) # HQ || self.is_a?(Question) || self.is_a?(Answer) self.created_at elsif self.is_a?(Event) self.starts_at else self.published_at || self.created_at end end
# File app/models/elasticsearch/index.rb, line 158 def delete_from_elasticsearch_server begin uri=(URI.parse("#{ELASTICSEARCH_URI}/#{self.class.to_s.tableize}/#{self.id}")) Net::HTTP.start(uri.host, uri.port) do |http| response = http.send_request("DELETE", uri.request_uri) # Elasticsearch::Base::log "Elasticsearch#delete_from_elasticsearch_server #{response.code} #{response.message} #{response.body}" Elasticsearch::Base::log "Elasticsearch#delete_from_elasticsearch_server #{self.class.name}##{self.id} response: #{response.code} #{response.message}" end rescue => e Elasticsearch::Base::log "Elastic search server is not available. Probably, #{self.class.name}##{self.id} has not been correctly deleted. ERROR: #{e}" end end
# File app/models/elasticsearch/index.rb, line 25 def fields_for_search if self.is_a?(Politician) h = {"public_name" => self.public_name, "role" => [self.public_role_en, self.public_role_es, self.public_role_eu].join('|'), "body_es" => self.description_es, "body_eu" => self.description_eu, "body_en" => self.description_en, "areas" => self.areas_for_elasticsearch, "areas_an" => self.areas_for_elasticsearch, "organization" => self.organization_for_elasticsearch, "organization_an" => self.organization_for_elasticsearch} # HQ # elsif self.is_a?(Answer) # h = {"body_es" => self.body.to_s.strip_html, # "body_eu" => self.video_title.to_s.strip_html, # "body_en" => self.video_description.to_s.strip_html, # "politicians" => self.author.public_name, # "politicians_an" => self.author.public_name, # "published_at" => self.date_for_elasticsearch, # "year" => I18n.localize(self.date_for_elasticsearch, :format => '%Y'), # "month" => AvailableLocales::AVAILABLE_LANGUAGES.keys.map{|loc| I18n.localize(self.date_for_elasticsearch, :format => '%B', :locale => loc)}.join('|')} else h = {"title_es" => self.title_es, "title_eu" => self.title_eu, "title_en" => self.title_en, "body_es" => self.body_es.to_s.strip_html, "body_eu" => self.body_eu.to_s.strip_html, "body_en" => self.body_en.to_s.strip_html, "tags" => self.public_tags_for_elasticsearch, "tags_an" => self.public_tags_for_elasticsearch, "politicians" => self.politicians_for_elasticsearch, "politicians_an" => self.politicians_for_elasticsearch, "areas" => self.areas_for_elasticsearch, "areas_an" => self.areas_for_elasticsearch, "published_at" => self.date_for_elasticsearch, "year" => I18n.localize(self.date_for_elasticsearch, :format => '%Y'), "month" => AvailableLocales::AVAILABLE_LANGUAGES.keys.map(&:to_s).sort.map{|loc| I18n.localize(self.date_for_elasticsearch, :format => '%B', :locale => loc)}.join('|'), "organization" => self.organization_for_elasticsearch, "organization_an" => self.organization_for_elasticsearch} if self.is_a?(News) || self.is_a?(Event) h.merge!({ "speaker" => [self.speaker_en, self.speaker_es, self.speaker_eu].join(',')}) end if self.is_a?(Video) h.merge!({ "subtitles_es" => self.subtitles_es_to_text, "subtitles_eu" => self.subtitles_eu_to_text, "subtitles_en" => self.subtitles_en_to_text }) end if self.is_a?(Debate) # use subtitle fields to store description h.merge!({ "subtitles_es" => self.description_es.to_s.strip_html, "subtitles_eu" => self.description_eu.to_s.strip_html, "subtitles_en" => self.description_en.to_s.strip_html }) end end h.merge!({"term" => AvailableLocales::AVAILABLE_LANGUAGES.keys.map(&:to_s).sort.map{|loc| I18n.translate('organizations.term', :term => self.term_for_elasticsearch, :locale => loc)}.join('|')}) if self.term_for_elasticsearch.present? h end
# File app/models/elasticsearch/index.rb, line 126 def organization_for_elasticsearch self.organization.nil? ? '' : [self.organization.name_en, self.organization.name_es, self.organization.name_eu].join('|') end
# File app/models/elasticsearch/index.rb, line 96 def politicians_for_elasticsearch politicians_for_elasticsearch=[] self.politicians.each do |pol| politicians_for_elasticsearch << pol.public_name end # HQ # if self.is_a?(Question) && self.for_whom.present? # politicians_for_elasticsearch << self.for_whom.public_name # end politicians_for_elasticsearch.join(';') end
# File app/models/elasticsearch/index.rb, line 130 def term_for_elasticsearch if self.respond_to?(:department) && self.department self.department.term end end
# File app/models/elasticsearch/index.rb, line 136 def update_elasticsearch_server begin if ((self.changed.include?('draft') || self.changed.include?('published_at')) && !self.published?) || (self.is_a?(Proposal) && !self.approved?) self.delete_from_elasticsearch_server end if self.published? || (self.is_a?(Proposal) && self.approved?) h = self.fields_for_search h.each {|k, v| h[k] = v.tildes if v.is_a?(String) && k.match(/_an/).nil?} uri=(URI.parse("#{ELASTICSEARCH_URI}/#{self.class.to_s.tableize}/#{self.id}")) Net::HTTP.start(uri.host, uri.port) do |http| headers = { 'Content-Type' => 'application/json'} data = h.to_json response = http.send_request("PUT", uri.request_uri, data, headers) # Elasticsearch::Base::log "Elasticsearch#update_elasticsearch_server: #{response.code} #{response.message} #{response.body}" Elasticsearch::Base::log "Elasticsearch#update_elasticsearch_server #{self.class.name}##{self.id} response: #{response.code} #{response.message}" end end rescue => e Elasticsearch::Base::log "Elastic search server is not available. Probably, #{self.class.name}##{self.id} has not been correctly indexed. Error: #{e}" end end