Class: EsClient::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/es_client/index.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Index

Returns a new instance of Index.



5
6
7
8
# File 'lib/es_client/index.rb', line 5

def initialize(name, options={})
  @name = build_name(name)
  @options = options
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/es_client/index.rb', line 3

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/es_client/index.rb', line 3

def options
  @options
end

Instance Method Details

#build_name(name) ⇒ Object



10
11
12
13
# 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) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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

#createObject



24
25
26
27
# 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

#deleteObject



29
30
31
32
# File 'lib/es_client/index.rb', line 29

def delete
  return unless exists?
  EsClient.client.delete!("/#{name}")
end

#destroy_document(type, id) ⇒ Object



69
70
71
# File 'lib/es_client/index.rb', line 69

def destroy_document(type, id)
  EsClient.client.delete("/#{name}/#{type}/#{id}")
end

#exists?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/es_client/index.rb', line 15

def exists?
  EsClient.client.head("/#{name}").success?
end

#find(type, id) ⇒ Object



73
74
75
# File 'lib/es_client/index.rb', line 73

def find(type, id)
  EsClient.client.get("/#{name}/#{type}/#{id}").decoded['_source']
end

#get_mappingObject



52
53
54
# File 'lib/es_client/index.rb', line 52

def get_mapping
  EsClient.client.get("/#{name}/_mapping").decoded[name]['mappings']
end

#get_settingsObject



44
45
46
# File 'lib/es_client/index.rb', line 44

def get_settings
  EsClient.client.get("/#{name}/_settings").decoded[name]['settings']
end

#put_mapping(type, mapping) ⇒ Object



56
57
58
59
# 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) ⇒ Object



48
49
50
# File 'lib/es_client/index.rb', line 48

def put_settings(settings)
  EsClient.client.put("/#{name}/_settings", body: settings.to_json)
end

#recreateObject



19
20
21
22
# File 'lib/es_client/index.rb', line 19

def recreate
  delete
  create
end

#refreshObject



34
35
36
# File 'lib/es_client/index.rb', line 34

def refresh
  EsClient.client.post("/#{name}/_refresh")
end

#save_document(type, id, document) ⇒ Object



61
62
63
# 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

#search(query, options = {}) ⇒ Object



38
39
40
41
42
# File 'lib/es_client/index.rb', line 38

def search(query, options={})
  http_options = options.slice(:query, :headers)
  http_options[:body] = query.to_json
  EsClient.client.get("/#{name}/#{options[:type]}/_search", http_options)
end

#update_document(type, id, document) ⇒ Object



65
66
67
# 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