Class: Scalastic::Partition

Inherits:
Object
  • Object
show all
Defined in:
lib/scalastic/partition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(es_client, config, id) ⇒ Partition

Returns a new instance of Partition.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
# File 'lib/scalastic/partition.rb', line 9

def initialize(es_client, config, id)
  raise(ArgumentError, 'ES client is nil!') if es_client.nil?
  raise(ArgumentError, 'config is nil!') if config.nil?
  raise(ArgumentError, 'id is empty!') if id.nil? || id.to_s.empty?

  @es_client = es_client
  @config = config
  @id = id
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/scalastic/partition.rb', line 6

def config
  @config
end

#es_clientObject (readonly)

Returns the value of attribute es_client.



5
6
7
# File 'lib/scalastic/partition.rb', line 5

def es_client
  @es_client
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/scalastic/partition.rb', line 7

def id
  @id
end

Instance Method Details

#delete(args = {}) ⇒ Object



43
44
45
46
# File 'lib/scalastic/partition.rb', line 43

def delete(args = {})
  args = args.merge(index: config.search_endpoint(id))
  es_client.delete(args)
end

#exists?Boolean

Returns:

  • (Boolean)


48
49
50
51
52
# File 'lib/scalastic/partition.rb', line 48

def exists?
  names = [config.search_endpoint(id), config.index_endpoint(id)]
  all_aliases = es_client.indices.get_aliases name: names.join(',')
  all_aliases.any?{|_index, data| data['aliases'].any?}
end

#extend_to(args) ⇒ Object

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
29
# File 'lib/scalastic/partition.rb', line 19

def extend_to(args)
  index = args[:index]
  raise(ArgumentError, 'Missing required argument :index') if index.nil? || index.to_s.empty?

  index_alias = config.index_endpoint(id)
  indices = es_client.indices.get_aliases(name: index_alias).select{|i, d| d['aliases'].any?}.keys
  actions = indices.map{|i| {remove: {index: i, alias: index_alias}}}
  actions << {add: EsActionsGenerator.new_index_alias(config, args.merge(id: id))}
  actions << {add: EsActionsGenerator.new_search_alias(config, args.merge(id: id))}
  es_client.indices.update_aliases(body: {actions: actions})
end

#index(args) ⇒ Object



36
37
38
39
40
41
# File 'lib/scalastic/partition.rb', line 36

def index(args)
  args = {body: {}}.merge(args)
  args[:body][config.partition_selector.to_sym] = id
  args = args.merge(index: config.index_endpoint(id))
  es_client.index(args)
end

#inspectObject

TODO: add bulk



56
57
58
# File 'lib/scalastic/partition.rb', line 56

def inspect
  "ES partition #{id}"
end

#search(args = {}) ⇒ Object



31
32
33
34
# File 'lib/scalastic/partition.rb', line 31

def search(args = {})
  args = args.merge(index: config.search_endpoint(id))
  es_client.search(args)
end