class Polipus::ElasticSearch::Page

Constants

DEFAULT_INDEX_NAME

Public Class Methods

clear_index!() click to toggle source
# File lib/polipus-elasticsearch/index/page.rb, line 93
def self.clear_index!
  client.delete_by_query(
    index: index_name,
     body: { query: { match_all: {} } }
  )
end
client() click to toggle source
# File lib/polipus-elasticsearch/index/page.rb, line 80
def self.client
  __elasticsearch__.client
end
count() click to toggle source
# File lib/polipus-elasticsearch/index/page.rb, line 84
def self.count
  client.count(index: index_name, type: document_type)['count'].to_i
end
create_index!(name) click to toggle source
# File lib/polipus-elasticsearch/index/page.rb, line 88
def self.create_index!(name)
  index_name(name) unless name.nil?
  __elasticsearch__.create_index!(index: index_name)
end
delete_index!() click to toggle source
# File lib/polipus-elasticsearch/index/page.rb, line 100
def self.delete_index!
  client.indices.delete(index: index_name)
end
exists?(id) click to toggle source
# File lib/polipus-elasticsearch/index/page.rb, line 104
def self.exists?(id)
  client.exists?(
    index: index_name,
    type: document_type,
    id: id
  )
end
get(id) click to toggle source
# File lib/polipus-elasticsearch/index/page.rb, line 112
def self.get(id)
  return unless exists?(id)
  client.get_source(
    index: index_name,
    type: document_type,
    id: id
  )
end
index_exists?() click to toggle source
# File lib/polipus-elasticsearch/index/page.rb, line 121
def self.index_exists?
  client.indices.exists?(index: index_name)
end
process_document(obj) click to toggle source
# File lib/polipus-elasticsearch/index/page.rb, line 125
def self.process_document(obj)
  doc = { '_type' => document_type }
  properties.each do |p|
    doc[p.to_s] = obj.respond_to?(p.to_s) ? obj.send(p.to_s) : obj[p.to_s]
  end
  doc.reject { |_, value| value.nil? }
end
properties() click to toggle source
# File lib/polipus-elasticsearch/index/page.rb, line 133
def self.properties
  mapping.to_hash[document_type.to_sym][:properties].keys.map { |k| k.to_s }
end
remove(id, refresh = false) click to toggle source
# File lib/polipus-elasticsearch/index/page.rb, line 137
def self.remove(id, refresh = false)
  return unless exists?(id)
  client.delete(
    index: index_name,
    type: document_type,
    id: id,
    refresh: refresh,
    version: Time.now.to_i,
    version_type: :external
  )
end
setup(client_, index_name = DEFAULT_INDEX_NAME) click to toggle source
# File lib/polipus-elasticsearch/index/page.rb, line 149
def self.setup(client_, index_name = DEFAULT_INDEX_NAME)
  __elasticsearch__.client = client_
  self.index_name(index_name)
end
store(document, refresh = false) click to toggle source
# File lib/polipus-elasticsearch/index/page.rb, line 154
def self.store(document, refresh = false)
  document = process_document(document)
  client.index(
    index: index_name,
    type: document_type,
    id: document['id'],
    body: document,
    refresh: refresh,
    version: document['fetched_at'].to_i,
    version_type: :external
  )
  document['id']
end