Module: Elastictastic

Defined in:
lib/elastictastic.rb,
lib/elastictastic/util.rb,
lib/elastictastic/dirty.rb,
lib/elastictastic/field.rb,
lib/elastictastic/index.rb,
lib/elastictastic/rotor.rb,
lib/elastictastic/scope.rb,
lib/elastictastic/client.rb,
lib/elastictastic/errors.rb,
lib/elastictastic/scoped.rb,
lib/elastictastic/search.rb,
lib/elastictastic/adapter.rb,
lib/elastictastic/railtie.rb,
lib/elastictastic/version.rb,
lib/elastictastic/document.rb,
lib/elastictastic/observer.rb,
lib/elastictastic/callbacks.rb,
lib/elastictastic/multi_get.rb,
lib/elastictastic/observing.rb,
lib/elastictastic/middleware.rb,
lib/elastictastic/properties.rb,
lib/elastictastic/association.rb,
lib/elastictastic/persistence.rb,
lib/elastictastic/thrift/rest.rb,
lib/elastictastic/validations.rb,
lib/elastictastic/multi_search.rb,
lib/elastictastic/parent_child.rb,
lib/elastictastic/server_error.rb,
lib/elastictastic/test_helpers.rb,
lib/elastictastic/thrift/types.rb,
lib/elastictastic/configuration.rb,
lib/elastictastic/scope_builder.rb,
lib/elastictastic/basic_document.rb,
lib/elastictastic/thrift_adapter.rb,
lib/elastictastic/nested_document.rb,
lib/elastictastic/thrift/constants.rb,
lib/elastictastic/embedded_document.rb,
lib/elastictastic/transport_methods.rb,
lib/elastictastic/optimistic_locking.rb,
lib/elastictastic/child_collection_proxy.rb,
lib/elastictastic/mass_assignment_security.rb,
lib/elastictastic/bulk_persistence_strategy.rb,
lib/elastictastic/new_relic_instrumentation.rb,
lib/elastictastic/discrete_persistence_strategy.rb

Defined Under Namespace

Modules: BasicDocument, Callbacks, Dirty, Document, EmbeddedDocument, MassAssignmentSecurity, Middleware, NewRelicInstrumentation, Observing, OptimisticLocking, ParentChild, Persistence, Properties, Scoped, ServerError, TestHelpers, Thrift, TransportMethods, Util, Validations Classes: Adapter, Association, BulkPersistenceStrategy, ChildCollectionProxy, Client, Configuration, ConnectionFailed, DiscretePersistenceStrategy, ExconAdapter, Field, Index, MultiGet, MultiSearch, NetHttpAdapter, Railtie, Rotor, Scope, ScopeBuilder, Search, ThriftAdapter

Constant Summary collapse

Error =
Class.new(StandardError)
CancelSave =
Class.new(Error)
IllegalModificationError =
Class.new(Error)
OperationNotAllowed =
Class.new(Error)
MissingParameter =
Class.new(Error)
NoServerAvailable =
Class.new(ConnectionFailed)
RecordInvalid =
Class.new(Error)
VERSION =
'0.11.2'
Observer =
Class.new(ActiveModel::Observer)
NestedDocument =
EmbeddedDocument

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configConfiguration

Elastictastic global configuration. In a Rails environment, you can configure Elastictastic by creating a ‘config/elastictastic.yml` file, whose keys will be passed into the Configuration object when your application boots. In non-Rails environment, you can configure Elastictastic directly using the object returned by this method.

Returns:



57
58
59
# File 'lib/elastictastic.rb', line 57

def config
  @config ||= Configuration.new
end

Class Method Details

.bulk(options = {}) { ... } ⇒ Object

Perform write operations in a single request to ElasticSearch. Highly recommended for any operation which writes a large quantity of data to ElasticSearch. Write operations (e.g. save/destroy documents) are buffered in the client and sent to ElasticSearch when the bulk operation exits (or when an auto-flush threshold is reached; see below).

Since write operations inside a bulk block are not performed synchronously, server-side errors will only be raised once the bulk block completes; you may pass a block into Document#save and Document#destroy that will be called once the operation completes. The block is passed an error param if the operation was not successful.

Examples:

Create posts in bulk

Elastictastic.bulk do
  params[:posts].each do |post_params|
    Post.new(post_params).save!
  end
end # posts are actually persisted here

Custom handling for conflicting IDs in a bulk block

errors = []
Elastictastic.bulk do
  params[:posts].each do |post_params|
    Post.new(post_params).save! do |e|
      case e
      when nil # success!
      when Elastictastic::ServerError::DocumentAlreadyExistsEngineException
        conflicting_ids << post_params[:id]
      else
        raise e
      end
    end
  end
end

Parameters:

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

    a customizable set of options

Options Hash (options):

  • :auto_flush (Fixnum)

    Flush to ElasticSearch after this many operations performed.

Yields:

  • Block during which all write operations are buffered for bulk



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/elastictastic.rb', line 149

def bulk(options = {})
  original_persister = self.persister
  bulk_persister = self.persister =
    Elastictastic::BulkPersistenceStrategy.new(options)
  begin
    yield
  ensure
    self.persister = original_persister
  end
  bulk_persister.flush
end

.clientClient

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return a lower-level ElasticSearch client. This is likely to be extracted into a separate gem in the future.

Returns:



81
82
83
# File 'lib/elastictastic.rb', line 81

def client
  Thread.current['Elastictastic::client'] ||= Client.new(config)
end

.Index(name_or_index) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Coerce the argument to an Elastictastic index.

Parameters:



197
198
199
# File 'lib/elastictastic.rb', line 197

def Index(name_or_index)
  Index === name_or_index ?  name_or_index : Index.new(name_or_index)
end

.json_decode(json) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Use Elastictastic’s configured JSON decoder to decode a JSON message

Parameters:

  • JSON (String)

    message to decode

Returns:

  • Ruby object represented by json param



183
184
185
186
187
188
189
# File 'lib/elastictastic.rb', line 183

def json_decode(json)
  if config.json_engine.respond_to?(:decode)
    config.json_engine.decode(json)
  else
    config.json_engine.load(json)
  end
end

.json_encode(object) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Use Elastictastic’s configured JSON encoder to encode a JSON message.

Parameters:

  • Object (Object)

    to encode to JSON

Returns:

  • JSON representation of object



168
169
170
171
172
173
174
# File 'lib/elastictastic.rb', line 168

def json_encode(object)
  if config.json_engine.respond_to?(:encode)
    config.json_engine.encode(object)
  else
    config.json_engine.dump(object)
  end
end

.multi_get(&block) ⇒ Object



61
62
63
# File 'lib/elastictastic.rb', line 61

def multi_get(&block)
  MultiGet.new.tap(&block).to_a
end

.multi_search(*scopes) ⇒ Object

Perform multiple searches in a single request to ElasticSearch. Each scope will be eagerly populated with results.

Parameters:

  • collection (Scope, Array)

    of scopes to execute multisearch on



71
72
# File 'lib/elastictastic.rb', line 71

def multi_search(*scopes)
end

.persisterDiscretePersistenceStrategy, BulkPersistenceStrategy

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The current persistence strategy for ElasticSearch. Usually this will be the DiscretePersistenceStrategy singleton; inside a ::bulk block, it will be an instance of BulkPersistenceStrategy

Returns:

See Also:

  • bulk


105
106
107
108
# File 'lib/elastictastic.rb', line 105

def persister
  Thread.current['Elastictastic::persister'] ||=
    Elastictastic::DiscretePersistenceStrategy.instance
end

.persister=(persister) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Set the current persistence strategy

Parameters:

See Also:

  • persister


92
93
94
# File 'lib/elastictastic.rb', line 92

def persister=(persister)
  Thread.current['Elastictastic::persister'] = persister
end