Class: ElasticsearchRecord::ModelApi

Inherits:
Object
  • Object
show all
Defined in:
lib/elasticsearch_record/model_api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ ModelApi

Returns a new instance of ModelApi.



7
8
9
# File 'lib/elasticsearch_record/model_api.rb', line 7

def initialize(klass)
  @klass = klass
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



5
6
7
# File 'lib/elasticsearch_record/model_api.rb', line 5

def klass
  @klass
end

Instance Method Details

#alias_exists?Boolean

Shortcut for alias_exists

Returns:

  • (Boolean)


# File 'lib/elasticsearch_record/model_api.rb', line 145

#aliasesHash

Shortcut for aliases

Returns:

  • (Hash)


# File 'lib/elasticsearch_record/model_api.rb', line 128

#backup!(to: nil, close: true) ⇒ String

Shortcut for backup_table

Parameters:

  • to (String) (defaults to: nil)
  • close (Boolean) (defaults to: true)

Returns:

  • (String)

    backup_name



# File 'lib/elasticsearch_record/model_api.rb', line 180

#block!Boolean

Shortcut to block write access on the index

Returns:

  • (Boolean)

    acknowledged status



# File 'lib/elasticsearch_record/model_api.rb', line 97

#bulk(data, operation = :index, refresh: true, **options) ⇒ Object

bulk handle provided data (single Hash or multiple Array).

Parameters:

  • data (Hash, Array<Hash<Symbol=>Object>>)
    • the data to insert/update/delete ...
  • operation (Symbol) (defaults to: :index)
  • refresh (Boolean, Symbol) (defaults to: true)


267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/elasticsearch_record/model_api.rb', line 267

def bulk(data, operation = :index, refresh: true, **options)
  data = [data] unless data.is_a?(Array)

  _connection.api(:core, :bulk, {
    index:   _index_name,
    body:    case operation
             when :update
               data.map { |item| { update: { _id: (item[:_id].presence || item['_id']), data: { doc: item.except(:_id, '_id') } } } }
             when :delete
               data.map { |item| { delete: { _id: (item[:_id].presence || item['_id']) } } }
             else
               data.map { |item| { operation => { _id: (item[:_id].presence || item['_id']), data: item.except(:_id, '_id') } } }
             end,
    refresh: refresh
  }, "BULK #{operation.to_s.upcase}", **options)
end

#clone!(target_name, **options) ⇒ Boolean

Shortcut for clone_table

Parameters:

  • target_name (String)
  • options (Hash)

Returns:

  • (Boolean)


# File 'lib/elasticsearch_record/model_api.rb', line 168

#close!Boolean

Shortcut to close the opened index.

Returns:

  • (Boolean)

    acknowledged status



# File 'lib/elasticsearch_record/model_api.rb', line 89

#create!(force: false, copy_from: nil, if_not_exists: false, **options) ⇒ Boolean

Shortcut for create_table

Parameters:

  • force (Boolean) (defaults to: false)
  • copy_from (nil, String) (defaults to: nil)
  • options (Hash)

Returns:

  • (Boolean)

    acknowledged status



# File 'lib/elasticsearch_record/model_api.rb', line 161

#delete(data, **options) ⇒ Object

fast delete data. IMPORTANT: Any 'doc'-id must by provided with underscore '_' ( +:_id+ )

Examples:

delete([1,2,3,5])

delete(3)

delete({_id: 2})

Parameters:

  • data (Array<Hash>, Hash)
  • options (Hash)


253
254
255
256
257
258
259
260
261
# File 'lib/elasticsearch_record/model_api.rb', line 253

def delete(data, **options)
  data = [data] unless data.is_a?(Array)

  if data[0].is_a?(Hash)
    bulk(data, :delete, **options)
  else
    bulk(data.map { |id| { _id: id } }, :delete, **options)
  end
end

#drop!(confirm: false) ⇒ Boolean

Shortcut to drop the index

Parameters:

  • confirm (defaults to: false)

Returns:

  • (Boolean)

    acknowledged status



# File 'lib/elasticsearch_record/model_api.rb', line 105

#exists?Boolean

Shortcut for exists

Returns:

  • (Boolean)


# File 'lib/elasticsearch_record/model_api.rb', line 141

#index(data, **options) ⇒ Object

fast insert/update data. IMPORTANT: Any 'doc'-id must by provided with underscore '_' ( +:_id+ )

Examples:

index([{name: 'Hans', age: 34}, {name: 'Peter', age: 22}])

index({_id: 5, name: 'Georg', age: 87})

Parameters:

  • data (Array<Hash>, Hash)
  • options (Hash)


209
210
211
# File 'lib/elasticsearch_record/model_api.rb', line 209

def index(data, **options)
  bulk(data, :index, **options)
end

#insert(data, **options) ⇒ Object

fast insert new data. IMPORTANT: Any 'doc'-id must by provided with underscore '_' ( +:_id+ )

Examples:

insert([{name: 'Hans', age: 34}, {name: 'Peter', age: 22}])

insert({name: 'Georg', age: 87})

Parameters:

  • data (Array<Hash>, Hash)
  • options (Hash)


223
224
225
# File 'lib/elasticsearch_record/model_api.rb', line 223

def insert(data, **options)
  bulk(data, :create, **options)
end

#mapping_exists?Boolean

Shortcut for mapping_exists

Returns:

  • (Boolean)


# File 'lib/elasticsearch_record/model_api.rb', line 153

#mappingsHash

Shortcut for mappings

Returns:

  • (Hash)


# File 'lib/elasticsearch_record/model_api.rb', line 115

#meta_exists?Boolean

Shortcut for meta_exists

Returns:

  • (Boolean)


# File 'lib/elasticsearch_record/model_api.rb', line 157

#metasHash

Shortcut for metas

Returns:

  • (Hash)


# File 'lib/elasticsearch_record/model_api.rb', line 119

#open!Boolean

Shortcut to open the closed index.

Returns:

  • (Boolean)

    acknowledged status



# File 'lib/elasticsearch_record/model_api.rb', line 85

#refresh!Boolean

Shortcut to refresh the index.

Returns:

  • (Boolean)

    result state (returns false if refreshing failed)



# File 'lib/elasticsearch_record/model_api.rb', line 93

#reindex!(target_name, **options) ⇒ Hash

Shortcut for reindex_table

Parameters:

  • target_name (String)
  • options (Hash)

Returns:

  • (Hash)

    reindex stats



# File 'lib/elasticsearch_record/model_api.rb', line 193

#rename!(target_name, timeout: nil, **options) ⇒ Object

Shortcut for rename_table

Parameters:

  • target_name (String)
  • timeout (String (frozen)) (defaults to: nil)
  • options (Hash)


# File 'lib/elasticsearch_record/model_api.rb', line 174

#restore!(from: , timeout: nil, open: true, drop_backup: false) ⇒ Boolean

Shortcut for restore_table

Parameters:

  • from (String) (defaults to: )
  • timeout (String (frozen)) (defaults to: nil)
  • open (Boolean) (defaults to: true)

Returns:

  • (Boolean)

    acknowledged status



# File 'lib/elasticsearch_record/model_api.rb', line 186

#schema(features = []) ⇒ Hash

Shortcut for schema

Parameters:

  • features (Array, Symbol) (defaults to: [])

Returns:

  • (Hash)


# File 'lib/elasticsearch_record/model_api.rb', line 136

#setting_exists?Boolean

Shortcut for setting_exists

Returns:

  • (Boolean)


# File 'lib/elasticsearch_record/model_api.rb', line 149

#settings(flat_settings = true) ⇒ Hash

Shortcut for settings

Parameters:

  • flat_settings (Boolean) (defaults to: true)

    (default: true)

Returns:

  • (Hash)


# File 'lib/elasticsearch_record/model_api.rb', line 123

#stateHash

Shortcut for state

Returns:

  • (Hash)


# File 'lib/elasticsearch_record/model_api.rb', line 132

#truncate!(confirm: false) ⇒ Boolean

Shortcut to truncate the index

Parameters:

  • confirm (defaults to: false)

Returns:

  • (Boolean)

    acknowledged status



# File 'lib/elasticsearch_record/model_api.rb', line 110

#unblock!Boolean

Shortcut to unblock all blocked accesses on the index

Returns:

  • (Boolean)

    acknowledged status



# File 'lib/elasticsearch_record/model_api.rb', line 101

#update(data, **options) ⇒ Object

fast update existing data. IMPORTANT: Any 'doc'-id must by provided with underscore '_' ( +:_id+ )

Examples:

update([{_id: 1, name: 'Hansi'}, {_id: 2, name: 'Peter Parker', age: 42}])

update({_id: 3, name: 'Georg McCain'})

Parameters:

  • data (Array<Hash>, Hash)
  • options (Hash)


237
238
239
# File 'lib/elasticsearch_record/model_api.rb', line 237

def update(data, **options)
  bulk(data, :update, **options)
end