Class: Scalastic::PartitionsClient
- Inherits:
-
Object
- Object
- Scalastic::PartitionsClient
- Defined in:
- lib/scalastic/partitions_client.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#es_client ⇒ Object
readonly
Returns the value of attribute es_client.
Instance Method Summary collapse
- #[](id) ⇒ Object
- #create(args = {}) ⇒ Object
- #delete(args = {}) ⇒ Object
-
#initialize(es_client, config = Config.default) ⇒ PartitionsClient
constructor
A new instance of PartitionsClient.
- #prepare_index(args) ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize(es_client, config = Config.default) ⇒ PartitionsClient
Returns a new instance of PartitionsClient.
9 10 11 12 13 14 |
# File 'lib/scalastic/partitions_client.rb', line 9 def initialize(es_client, config = Config.default) raise(ArgumentError, "ES client is nil") if es_client.nil? raise(ArgumentError, "Config is nil") if config.nil? @es_client = es_client @config = config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
7 8 9 |
# File 'lib/scalastic/partitions_client.rb', line 7 def config @config end |
#es_client ⇒ Object (readonly)
Returns the value of attribute es_client.
6 7 8 |
# File 'lib/scalastic/partitions_client.rb', line 6 def es_client @es_client end |
Instance Method Details
#[](id) ⇒ Object
36 37 38 |
# File 'lib/scalastic/partitions_client.rb', line 36 def [](id) Partition.new(es_client, config, id) end |
#create(args = {}) ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/scalastic/partitions_client.rb', line 16 def create(args = {}) actions = [ {add: EsActionsGenerator.new_search_alias(config, args)}, {add: EsActionsGenerator.new_index_alias(config, args)}, ] es_client.indices.update_aliases(body: {actions: actions}) self[args[:id]] end |
#delete(args = {}) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/scalastic/partitions_client.rb', line 25 def delete(args = {}) id = args[:id] || raise(ArgumentError, "Missing required argument :id") pairs = es_client.indices.get_aliases.map{|i, d| d["aliases"].keys.select{|a| config.get_partition_id(a) == id}.map{|a| [i, a]}}.flatten(1) unless pairs.any? #TODO: log a warning return end actions = pairs.map{|i, a| {remove: {index: i, alias: a}}} es_client.indices.update_aliases(body: {actions: actions}) end |
#prepare_index(args) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/scalastic/partitions_client.rb', line 46 def prepare_index(args) index = args[:index] || raise(ArgumentError, "Missing required argument :index") mapping = {properties: {config.partition_selector => {type: "long"}}} es_client.indices.put_mapping(index: index, type: "_default_", body: {"_default_" => mapping}) es_client.indices.put_mapping(index: index, type: "scalastic", body: {"scalastic" => mapping}) end |
#to_a ⇒ Object
40 41 42 43 44 |
# File 'lib/scalastic/partitions_client.rb', line 40 def to_a aliases = es_client.indices.get_aliases partition_ids = aliases.map{|_, data| data["aliases"].keys}.flatten.map{|a| config.get_partition_id(a)}.compact.uniq partition_ids.map{|id| Partition.new(es_client, config, id)} end |