class EsClient::Index

Attributes

name[R]
options[R]

Public Class Methods

new(name, options={}) click to toggle source
# File lib/es_client/index.rb, line 5
def initialize(name, options={})
  @name = build_name(name)
  @options = options
end

Public Instance Methods

build_name(name) click to toggle source
# File lib/es_client/index.rb, line 10
def build_name(name)
  return name unless EsClient.index_prefix
  "#{EsClient.index_prefix}_#{name}"
end
bulk(action, type, documents) click to toggle source
# File lib/es_client/index.rb, line 77
def bulk(action, type, documents)
  payload = []
  documents.each do |document|
    payload << {action => {_index: name, _type: type, _id: document[:id]}}
    case action
      when :index
        payload << document
      when :update
        document_for_update = {doc: document}
        document_for_update.update(document[:bulk_options]) if document[:bulk_options]
        payload << document_for_update
    end
  end
  serialized_payload = "\n" + payload.map(&:to_json).join("\n") + "\n"
  EsClient.client.post("/#{name}/#{type}/_bulk", body: serialized_payload)
end
create() click to toggle source
# File lib/es_client/index.rb, line 24
def create
  request_options = @options.present? ? {body: @options.to_json} : {}
  EsClient.client.post!("/#{name}", request_options)
end
delete() click to toggle source
# File lib/es_client/index.rb, line 29
def delete
  return unless exists?
  EsClient.client.delete!("/#{name}")
end
destroy_document(type, id) click to toggle source
# File lib/es_client/index.rb, line 69
def destroy_document(type, id)
  EsClient.client.delete("/#{name}/#{type}/#{id}")
end
exists?() click to toggle source
# File lib/es_client/index.rb, line 15
def exists?
  EsClient.client.head("/#{name}").success?
end
find(type, id) click to toggle source
# File lib/es_client/index.rb, line 73
def find(type, id)
  EsClient.client.get("/#{name}/#{type}/#{id}").decoded['_source']
end
get_mapping() click to toggle source
# File lib/es_client/index.rb, line 52
def get_mapping
  EsClient.client.get("/#{name}/_mapping").decoded[name]['mappings']
end
get_settings() click to toggle source
# File lib/es_client/index.rb, line 44
def get_settings
  EsClient.client.get("/#{name}/_settings").decoded[name]['settings']
end
put_mapping(type, mapping) click to toggle source
# File lib/es_client/index.rb, line 56
def put_mapping(type, mapping)
  json = {type => mapping}.to_json
  EsClient.client.put("/#{name}/_mapping/#{type}", body: json)
end
put_settings(settings) click to toggle source
# File lib/es_client/index.rb, line 48
def put_settings(settings)
  EsClient.client.put("/#{name}/_settings", body: settings.to_json)
end
recreate() click to toggle source
# File lib/es_client/index.rb, line 19
def recreate
  delete
  create
end
refresh() click to toggle source
# File lib/es_client/index.rb, line 34
def refresh
  EsClient.client.post("/#{name}/_refresh")
end
save_document(type, id, document) click to toggle source
# File lib/es_client/index.rb, line 61
def save_document(type, id, document)
  EsClient.client.post("/#{name}/#{type}/#{id}", body: document.to_json)
end
update_document(type, id, document) click to toggle source
# File lib/es_client/index.rb, line 65
def update_document(type, id, document)
  EsClient.client.post("/#{name}/#{type}/#{id}/_update", body: {doc: document}.to_json)
end