Class: Elasticity::Strategies::SingleIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/elasticity/strategies/single_index.rb

Constant Summary collapse

STATUSES =
[:missing, :ok]

Instance Method Summary collapse

Constructor Details

#initialize(client, index_name) ⇒ SingleIndex


6
7
8
9
# File 'lib/elasticity/strategies/single_index.rb', line 6

def initialize(client, index_name)
  @client     = client
  @index_name = index_name
end

Instance Method Details

#bulk {|b| ... } ⇒ Object

Yields:

  • (b)

74
75
76
77
78
# File 'lib/elasticity/strategies/single_index.rb', line 74

def bulk
  b = Bulk::Index.new(@client, @index_name)
  yield b
  b.execute
end

#create(index_def) ⇒ Object


23
24
25
26
27
28
29
# File 'lib/elasticity/strategies/single_index.rb', line 23

def create(index_def)
  if missing?
    @client.index_create(index: @index_name, body: index_def)
  else
    raise IndexError.new(@index_name, "index already exist")
  end
end

#create_if_undefined(index_def) ⇒ Object


31
32
33
# File 'lib/elasticity/strategies/single_index.rb', line 31

def create_if_undefined(index_def)
  create(index_def) if missing?
end

#deleteObject


35
36
37
# File 'lib/elasticity/strategies/single_index.rb', line 35

def delete
  @client.index_delete(index: @index_name)
end

#delete_by_query(type, body) ⇒ Object


70
71
72
# File 'lib/elasticity/strategies/single_index.rb', line 70

def delete_by_query(type, body)
  @client.delete_by_query(index: @index_name, type: type, body: body)
end

#delete_document(type, id) ⇒ Object


58
59
60
# File 'lib/elasticity/strategies/single_index.rb', line 58

def delete_document(type, id)
  @client.delete(index: @index_name, type: type, id: id)
end

#delete_if_definedObject


39
40
41
# File 'lib/elasticity/strategies/single_index.rb', line 39

def delete_if_defined
  delete unless missing?
end

#flushObject


96
97
98
# File 'lib/elasticity/strategies/single_index.rb', line 96

def flush
  @client.index_flush(index: @index_name)
end

#get_document(type, id) ⇒ Object


62
63
64
# File 'lib/elasticity/strategies/single_index.rb', line 62

def get_document(type, id)
  @client.get(index: @index_name, type: type, id: id)
end

#index_document(type, id, attributes) ⇒ Object


48
49
50
51
52
53
54
55
56
# File 'lib/elasticity/strategies/single_index.rb', line 48

def index_document(type, id, attributes)
  res = @client.index(index: @index_name, type: type, id: id, body: attributes)

  if id = res["_id"]
    [id, res["created"]]
  else
    raise IndexError.new(@update_alias, "failed to index document")
  end
end

#mappingsObject


88
89
90
91
92
93
94
# File 'lib/elasticity/strategies/single_index.rb', line 88

def mappings
  args = { index: @index_name }
  mapping = @client.index_get_mapping(index: @index_name)
  mapping[@index_name]["mappings"]
rescue Elasticsearch::Transport::Transport::Errors::NotFound
  nil
end

#missing?Boolean


19
20
21
# File 'lib/elasticity/strategies/single_index.rb', line 19

def missing?
  not @client.index_exists(index: @index_name)
end

#recreate(index_def) ⇒ Object


43
44
45
46
# File 'lib/elasticity/strategies/single_index.rb', line 43

def recreate(index_def)
  delete_if_defined
  create(index_def)
end

#ref_index_nameObject


11
12
13
# File 'lib/elasticity/strategies/single_index.rb', line 11

def ref_index_name
  @index_name
end

#remap!Object

Raises:

  • (NotImplementedError)

15
16
17
# File 'lib/elasticity/strategies/single_index.rb', line 15

def remap!
  raise NotImplementedError
end

#search(type, body) ⇒ Object


66
67
68
# File 'lib/elasticity/strategies/single_index.rb', line 66

def search(type, body)
  Search::Facade.new(@client, Search::Definition.new(@index_name, type, body))
end

#settingsObject


80
81
82
83
84
85
86
# File 'lib/elasticity/strategies/single_index.rb', line 80

def settings
  args = { index: @index_name }
  settings = @client.index_get_settings(index: @index_name)
  settings[@index_name]["settings"]
rescue Elasticsearch::Transport::Transport::Errors::NotFound
  nil
end