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, use_new_timestamp_format = nil, include_type_name_on_create = nil) ⇒ 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, use_new_timestamp_format = nil, include_type_name_on_create = nil)
  @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(body) ⇒ Object



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

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

#delete_document(id) ⇒ Object



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

def delete_document(id)
  @client.delete(index: @index_name, 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



101
102
103
# File 'lib/elasticity/strategies/single_index.rb', line 101

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

#get_document(id) ⇒ Object



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

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

#index_document(id, attributes) ⇒ Object



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

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

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

#mappingObject



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

def mapping
  @client.index_get_mapping(index: @index_name).values.first
rescue Elastic::Transport::Transport::Errors::NotFound
  nil
end

#mappingsObject



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

def mappings
  ActiveSupport::Deprecation.warn(
    "Elasticity::Strategies::SingleIndex#mappings is deprecated, "\
    "use mapping instead"
  )
  mapping
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

#refreshObject



105
106
107
# File 'lib/elasticity/strategies/single_index.rb', line 105

def refresh
  @client.index_refresh(index: @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_indexObject



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

def search_index
  @index_name
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).values.first
rescue Elastic::Transport::Transport::Errors::NotFound
  nil
end