Class: Lycra::Document::Proxy::ClassProxy

Inherits:
Object
  • Object
show all
Includes:
BaseProxy
Defined in:
lib/lycra/document/proxy.rb

Instance Attribute Summary

Attributes included from BaseProxy

#target

Instance Method Summary collapse

Methods included from BaseProxy

#client, #client=, #initialize, #method_missing, #respond_to_missing?

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Lycra::Document::Proxy::BaseProxy

Instance Method Details

#alias_exists?Boolean

Returns:

  • (Boolean)


440
441
442
# File 'lib/lycra/document/proxy.rb', line 440

def alias_exists?
  client.indices.exists_alias? name: alias_name
end

#alias_name(index_alias = nil) ⇒ Object



395
396
397
398
# File 'lib/lycra/document/proxy.rb', line 395

def alias_name(index_alias=nil)
  @_lycra_alias_name = index_alias if index_alias
  @_lycra_alias_name ||= document_type.pluralize
end

#alias_name=(index_alias) ⇒ Object



400
401
402
# File 'lib/lycra/document/proxy.rb', line 400

def alias_name=(index_alias)
  alias_name index_alias
end

#aliased_indexObject



444
445
446
# File 'lib/lycra/document/proxy.rb', line 444

def aliased_index
  client.indices.get_alias(name: alias_name).keys.first
end

#create_alias!(options = {}) ⇒ Object



452
453
454
455
456
# File 'lib/lycra/document/proxy.rb', line 452

def create_alias!(options={})
  # TODO custom error classes
  raise "Alias already exists" if alias_exists?
  client.indices.put_alias name: alias_name, index: index_name
end

#delete_alias!(options = {}) ⇒ Object



458
459
460
461
462
# File 'lib/lycra/document/proxy.rb', line 458

def delete_alias!(options={})
  # TODO custom error classes
  raise "Alias does not exists" unless alias_exists?
  client.indices.delete_alias name: alias_name, index: aliased_index
end

#document_type(type = nil) ⇒ Object



413
414
415
416
# File 'lib/lycra/document/proxy.rb', line 413

def document_type(type=nil)
  @_lycra_document_type = type if type
  @_lycra_document_type ||= target.name.demodulize.gsub(/Document\Z/, '').underscore
end

#document_type=(type) ⇒ Object



418
419
420
# File 'lib/lycra/document/proxy.rb', line 418

def document_type=(type)
  document_type type
end

#index_aliased?Boolean

Returns:

  • (Boolean)


448
449
450
# File 'lib/lycra/document/proxy.rb', line 448

def index_aliased?
  alias_exists? && aliased_index == index_name
end

#index_fingerprint(hashed = nil) ⇒ Object



380
381
382
383
384
385
386
387
388
389
# File 'lib/lycra/document/proxy.rb', line 380

def index_fingerprint(hashed=nil)
  @_lycra_index_fingerprint = hashed if hashed
  @_lycra_index_fingerprint ||= Digest::MD5.hexdigest(mappings.to_s)

  if @_lycra_index_fingerprint.is_a?(Proc)
    instance_exec(&@_lycra_index_fingerprint)
  else
    @_lycra_index_fingerprint
  end
end

#index_fingerprint=(hashed) ⇒ Object



391
392
393
# File 'lib/lycra/document/proxy.rb', line 391

def index_fingerprint=(hashed)
  index_fingerprint hashed
end

#index_name(index = nil) ⇒ Object



404
405
406
407
# File 'lib/lycra/document/proxy.rb', line 404

def index_name(index=nil)
  @_lycra_index_name = index if index
  @_lycra_index_name ||= "#{alias_name}-#{index_fingerprint}"
end

#index_name=(index) ⇒ Object



409
410
411
# File 'lib/lycra/document/proxy.rb', line 409

def index_name=(index)
  index_name index
end

#mapping(mapping = nil) ⇒ Object Also known as: mappings



422
423
424
425
426
427
# File 'lib/lycra/document/proxy.rb', line 422

def mapping(mapping=nil)
  @_lycra_mapping = mapping if mapping
  { document_type.to_s.underscore.to_sym => (@_lycra_mapping || {}).merge({
      properties: attributes.map { |name, type| [name, type.mapping] }.to_h
  }) }
end

#search(query_or_payload, options = {}) ⇒ Object



435
436
437
438
# File 'lib/lycra/document/proxy.rb', line 435

def search(query_or_payload, options={})
  options = {index: alias_name}.merge(options)
  super(query_or_payload, options)
end

#settings(settings = nil) ⇒ Object



430
431
432
433
# File 'lib/lycra/document/proxy.rb', line 430

def settings(settings=nil)
  @_lycra_settings = settings if settings
  @_lycra_settings || {}
end