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, document_type) ⇒ SingleIndex

Returns a new instance of SingleIndex.



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

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

Instance Method Details

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

Yields:

  • (b)


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

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

#create(index_def) ⇒ Object



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

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



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

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

#deleteObject



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

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

#delete_by_query(type, body) ⇒ Object



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

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

#delete_document(type, id) ⇒ Object



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

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

#delete_if_definedObject



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

def delete_if_defined
  delete unless missing?
end

#flushObject



93
94
95
# File 'lib/elasticity/strategies/single_index.rb', line 93

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

#get_document(type, id) ⇒ Object



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

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

#index_document(type, id, attributes) ⇒ Object



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

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



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

def mappings
  @client.index_get_mapping(index: @index_name, type: @document_type).values.first
rescue Elasticsearch::Transport::Transport::Errors::NotFound
  nil
end

#missing?Boolean

Returns:

  • (Boolean)


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

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

#recreate(index_def) ⇒ Object



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

def recreate(index_def)
  delete_if_defined
  create(index_def)
end

#ref_index_nameObject



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

def ref_index_name
  @index_name
end

#remap!Object

Raises:

  • (NotImplementedError)


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

def remap!
  raise NotImplementedError
end

#search(type, body) ⇒ Object



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

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

#settingsObject



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

def settings
  @client.index_get_settings(index: @index_name, type: @document_type).values.first
rescue Elasticsearch::Transport::Transport::Errors::NotFound
  nil
end