module Elasticsearch::Index::InstanceMethods

Public Instance Methods

areas_for_elasticsearch() click to toggle source
# 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
date_for_elasticsearch() click to toggle source
# 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
delete_from_elasticsearch_server() click to toggle source
# 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
organization_for_elasticsearch() click to toggle source
# 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
politicians_for_elasticsearch() click to toggle source
# 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
public_tags_for_elasticsearch() click to toggle source
# File app/models/elasticsearch/index.rb, line 88
def public_tags_for_elasticsearch
  tags_for_elasticsearch=[]
  self.public_tags_without_politicians.each do |tag|
    tags_for_elasticsearch << [tag.name_en, tag.name_es, tag.name_eu].join('|')
  end  
  tags_for_elasticsearch.join(';')
end
term_for_elasticsearch() click to toggle source
# File app/models/elasticsearch/index.rb, line 130
def term_for_elasticsearch
  if self.respond_to?(:department) && self.department
    self.department.term
  end
end
update_elasticsearch_server() click to toggle source
# 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