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



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

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



118
119
120
121
122
# File 'lib/elasticsearch/persistence/repository.rb', line 118

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

#document_typeString, Symbol

Get the document type used by the repository object.

Examples:

repository.document_type

Returns:

  • (String, Symbol)

    The repository’s document type.

Since:

  • 6.0.0



132
133
134
135
# File 'lib/elasticsearch/persistence/repository.rb', line 132

def document_type
  @document_type ||= @options[:document_type] ||
                       __get_class_value(:document_type)
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



219
220
221
# File 'lib/elasticsearch/persistence/repository.rb', line 219

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



145
146
147
148
149
# File 'lib/elasticsearch/persistence/repository.rb', line 145

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.

  • :document_type (Symbol, String)

    The type of documents persisted in this repository.

  • :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



106
107
108
# File 'lib/elasticsearch/persistence/repository.rb', line 106

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



231
232
233
# File 'lib/elasticsearch/persistence/repository.rb', line 231

def inspect
  "#<#{self.class}:0x#{object_id} index_name=#{index_name} document_type=#{document_type} 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



159
160
161
# File 'lib/elasticsearch/persistence/repository.rb', line 159

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



180
181
182
183
184
185
186
187
# File 'lib/elasticsearch/persistence/repository.rb', line 180

def mapping(*args)
  @memoized_mapping ||= @options[:mapping] || (begin
    if _mapping = __get_class_value(:mapping)
      _mapping.instance_variable_set(:@type, document_type)
      _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



207
208
209
# File 'lib/elasticsearch/persistence/repository.rb', line 207

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