module Elasticsearch::Persistence::Repository

The base Repository mixin. This module should be included in classes that represent an Elasticsearch repository.

@since 6.0.0

Constants

DEFAULT_INDEX_NAME

The default index name.

@return [ String ] The default index name.

@since 6.0.0

Attributes

options[R]

The repository options.

@return [ Hash ]

@since 6.0.0

Public Class Methods

included(base) click to toggle source
# File lib/elasticsearch/persistence/repository.rb, line 38
def self.included(base)
  base.send(:extend, ClassMethods)
end
new(options = {}) click to toggle source

Initialize a repository instance.

@example Initialize the repository.

MyRepository.new(index_name: 'notes', klass: Note)

@param [ Hash ] options The options to use.

@option options [ Symbol, String ] :index_name The name of the index. @option options [ Symbol, String ] :client The client used to handle requests to and from Elasticsearch. @option options [ Symbol, String ] :klass The class used to instantiate an object when documents are

deserialized. The default is nil, in which case the raw document will be returned as a Hash.

@option options [ Elasticsearch::Model::Indexing::Mappings, Hash ] :mapping The mapping for this index. @option options [ Elasticsearch::Model::Indexing::Settings, Hash ] :settings The settings for this index.

@since 6.0.0

# File lib/elasticsearch/persistence/repository.rb, line 104
def initialize(options = {})
  @options = options
end

Public Instance Methods

client() click to toggle source

Get the client used by the repository.

@example

repository.client

@return [ Elasticsearch::Client ] The repository’s client.

@since 6.0.0

# File lib/elasticsearch/persistence/repository.rb, line 116
def client
  @client ||= @options[:client] ||
                __get_class_value(:client) ||
                Elasticsearch::Client.new
end
index_exists?(*args) click to toggle source

Determine whether the index with this repository’s index name exists.

@example

repository.index_exists?

@return [ true, false ] Whether the index exists.

@since 6.0.0

Calls superclass method
# File lib/elasticsearch/persistence/repository.rb, line 203
def index_exists?(*args)
  super
end
index_name() click to toggle source

Get the index name used by the repository.

@example

repository.index_name

@return [ String, Symbol ] The repository’s index name.

@since 6.0.0

# File lib/elasticsearch/persistence/repository.rb, line 130
def index_name
  @index_name ||= @options[:index_name] ||
                    __get_class_value(:index_name) ||
                    DEFAULT_INDEX_NAME
end
inspect() click to toggle source

Get the nicer formatted string for use in inspection.

@example Inspect the repository.

repository.inspect

@return [ String ] The repository inspection.

@since 6.0.0

# File lib/elasticsearch/persistence/repository.rb, line 215
def inspect
  "#<#{self.class}:0x#{object_id} index_name=#{index_name} klass=#{klass}>"
end
klass() click to toggle source

Get the class used by the repository when deserializing.

@example

repository.klass

@return [ Class ] The repository’s klass for deserializing.

@since 6.0.0

# File lib/elasticsearch/persistence/repository.rb, line 144
def klass
  @klass ||= @options[:klass] || __get_class_value(:klass)
end
mapping(*args) click to toggle source

Get the index mapping. Optionally pass a block to define the mappings.

@example

repository.mapping

@example Set the mappings with a block.

  repository.mapping dynamic: 'strict' do
    indexes :foo
  end
end

@note If mappings were set when the repository was created, a block passed to this

method will not be evaluated.

@return [ Elasticsearch::Model::Indexing::Mappings ] The index mappings.

@since 6.0.0

Calls superclass method
# File lib/elasticsearch/persistence/repository.rb, line 165
def mapping(*args)
  @memoized_mapping ||= @options[:mapping] || (begin
    if _mapping = __get_class_value(:mapping)
      _mapping
    end
  end) || (super && @mapping)
end
Also aliased as: mappings
mappings(*args)
Alias for: mapping
settings(*args) click to toggle source

Get the index settings.

@example

repository.settings

@example Set the settings with a block.

repository.settings number_of_shards: 1, number_of_replicas: 0 do
  mapping dynamic: 'strict' do
    indexes :foo do
      indexes :bar
    end
  end
end

@return [ Elasticsearch::Model::Indexing::Settings ] The index settings.

@since 6.0.0

Calls superclass method
# File lib/elasticsearch/persistence/repository.rb, line 191
def settings(*args)
  @memoized_settings ||= @options[:settings] || __get_class_value(:settings) || (super && @settings)
end

Private Instance Methods

__get_class_value(_method_) click to toggle source
# File lib/elasticsearch/persistence/repository.rb, line 221
def __get_class_value(_method_)
  self.class.send(_method_) if self.class.respond_to?(_method_)
end