Module: Elasticsearch::Persistence::Repository

Includes:
Model::Indexing::ClassMethods, Find, Search, Serialize, Store
Defined in:
lib/elasticsearch/persistence/repository.rb,
lib/elasticsearch/persistence/repository/dsl.rb,
lib/elasticsearch/persistence/repository/find.rb,
lib/elasticsearch/persistence/repository/store.rb,
lib/elasticsearch/persistence/repository/search.rb,
lib/elasticsearch/persistence/repository/serialize.rb,
lib/elasticsearch/persistence/repository/response/results.rb

Overview

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

Since:

  • 6.0.0

Defined Under Namespace

Modules: ClassMethods, DSL, Find, Response, Search, Serialize, Store Classes: DocumentNotFound

Constant Summary collapse

DEFAULT_INDEX_NAME =

The default index name.

Returns:

  • (String)

    The default index name.

Since:

  • 6.0.0

'repository'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Search

#count, #search

Methods included from Find

#exists?, #find

Methods included from Serialize

#deserialize, #serialize

Methods included from Store

#delete, #save, #update

Instance Attribute Details

#optionsHash (readonly)

The repository options.

Returns:

  • (Hash)

Since:

  • 6.0.0



87
88
89
# File 'lib/elasticsearch/persistence/repository.rb', line 87

def options
  @options
end

Class Method Details

.included(base) ⇒ Object

Since:

  • 6.0.0



38
39
40
# File 'lib/elasticsearch/persistence/repository.rb', line 38

def self.included(base)
  base.send(:extend, ClassMethods)
end

Instance Method Details

#clientElasticsearch::Client

Get the client used by the repository.

Examples:

repository.client

Returns:

  • (Elasticsearch::Client)

    The repository’s client.

Since:

  • 6.0.0



116
117
118
119
120
# File 'lib/elasticsearch/persistence/repository.rb', line 116

def client
  @client ||= @options[:client] ||
                __get_class_value(:client) ||
                Elasticsearch::Client.new
end

#index_exists?(*args) ⇒ true, false

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

Examples:

repository.index_exists?

Returns:

  • (true, false)

    Whether the index exists.

Since:

  • 6.0.0



203
204
205
# File 'lib/elasticsearch/persistence/repository.rb', line 203

def index_exists?(*args)
  super
end

#index_nameString, Symbol

Get the index name used by the repository.

Examples:

repository.index_name

Returns:

  • (String, Symbol)

    The repository’s index name.

Since:

  • 6.0.0



130
131
132
133
134
# 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

#initialize(options = {}) ⇒ Object

Initialize a repository instance.

Examples:

Initialize the repository.

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

Parameters:

  • options (Hash) (defaults to: {})

    The options to use.

Options Hash (options):

  • :index_name (Symbol, String)

    The name of the index.

  • :client (Symbol, String)

    The client used to handle requests to and from Elasticsearch.

  • :klass (Symbol, String)

    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.

  • :mapping (Elasticsearch::Model::Indexing::Mappings, Hash)

    The mapping for this index.

  • :settings (Elasticsearch::Model::Indexing::Settings, Hash)

    The settings for this index.

Since:

  • 6.0.0



104
105
106
# File 'lib/elasticsearch/persistence/repository.rb', line 104

def initialize(options = {})
  @options = options
end

#inspectString

Get the nicer formatted string for use in inspection.

Examples:

Inspect the repository.

repository.inspect

Returns:

  • (String)

    The repository inspection.

Since:

  • 6.0.0



215
216
217
# File 'lib/elasticsearch/persistence/repository.rb', line 215

def inspect
  "#<#{self.class}:0x#{object_id} index_name=#{index_name} klass=#{klass}>"
end

#klassClass

Get the class used by the repository when deserializing.

Examples:

repository.klass

Returns:

  • (Class)

    The repository’s klass for deserializing.

Since:

  • 6.0.0



144
145
146
# File 'lib/elasticsearch/persistence/repository.rb', line 144

def klass
  @klass ||= @options[:klass] || __get_class_value(:klass)
end

#mapping(*args) ⇒ Elasticsearch::Model::Indexing::Mappings Also known as: mappings

Note:

If mappings were set when the repository was created, a block passed to this method will not be evaluated.

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

end

Examples:

repository.mapping

Set the mappings with a block.

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

Returns:

  • (Elasticsearch::Model::Indexing::Mappings)

    The index mappings.

Since:

  • 6.0.0



165
166
167
168
169
170
171
# 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

#settings(*args) ⇒ Elasticsearch::Model::Indexing::Settings

Get the index settings.

Examples:

repository.settings

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

Returns:

  • (Elasticsearch::Model::Indexing::Settings)

    The index settings.

Since:

  • 6.0.0



191
192
193
# File 'lib/elasticsearch/persistence/repository.rb', line 191

def settings(*args)
  @memoized_settings ||= @options[:settings] || __get_class_value(:settings) || (super && @settings)
end