Class: Waistband::Index

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

Constant Summary collapse

MAX_RETRIES =
10

Instance Method Summary collapse

Constructor Details

#initialize(index) ⇒ Index

Returns a new instance of Index.



12
13
14
15
16
17
# File 'lib/waistband/index.rb', line 12

def initialize(index)
  @index        = index
  @index_name   = config['name']
  @stringify    = config['stringify']
  @retries      = 0
end

Instance Method Details

#create!Object

create the index



20
21
22
23
24
# File 'lib/waistband/index.rb', line 20

def create!
  RestClient.post(url, index_json)
rescue RestClient::BadRequest => ex
  nil
end

#delete!(key) ⇒ Object



62
63
64
# File 'lib/waistband/index.rb', line 62

def delete!(key)
  RestClient.delete(url_for_key(key))
end

#destroy!Object

destroy the index



27
28
29
30
31
# File 'lib/waistband/index.rb', line 27

def destroy!
  RestClient.delete(url)
rescue RestClient::ResourceNotFound => ex
  nil
end

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



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

def query(term, options = {})
  Waistband::Query.new(@index_name, term, options)
end

#read(key) ⇒ Object



66
67
68
69
70
71
# File 'lib/waistband/index.rb', line 66

def read(key)
  fetched = RestClient.get(url_for_key(key))
  JSON.parse(fetched)['_source'].with_indifferent_access
rescue RestClient::ResourceNotFound => ex
  nil
end

#refreshObject

refresh the index



38
39
40
# File 'lib/waistband/index.rb', line 38

def refresh
  RestClient.post("#{url}/_refresh", {})
end

#store!(key, data) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/waistband/index.rb', line 42

def store!(key, data)
  # map everything to strings
  if @stringify
    original_data = data

    if data.is_a?(Array)
      data = Waistband::StringifiedArray.new(data)
    elsif data.is_a?(Hash)
      data = Waistband::StringifiedHash.new_from(data)
    end

    data = data.stringify_all if data.respond_to?(:stringify_all)
  end

  result  = RestClient.put(url_for_key(key), data.to_json)
  data    = original_data if @stringify

  result
end

#update_settings!Object



33
34
35
# File 'lib/waistband/index.rb', line 33

def update_settings!
  RestClient.put("#{url}/_settings", settings_json)
end