Elastics::ModelIndexer

Notice: The Elastics::ModelIndexer includes also Elastics::ModelSyncer (see Elastics::ModelSyncer)

Elastics avoids polluting your models with many methods: indeed it adds just a few methods, all starting with the elastics_ prefix to avoid confusion:

Besides you can define a Model.elastics_result method in order to customize the results (see Model.elastics_result)

The Model.elastics_in_batches is used internally to import the records/documents of the model. It is defined for ActiveRecord and Mongoid models. You may want to override it if you want to import only a subset of the table/collection (for example if you need to do an incremental import/live-reindex based on a timestamp).

Class Methods

elastics

You access all the Elastics features included in your model through the elastics proxy object. It implements the methods to map your models to the elasticsearch index, define parent/child relationships and sync.

elastics.index

By default the index used for all the models in your app is the Elastics::Configuration.variables[:index]. In a rails app that gets set before initializing the app (see Configuration). You can map your model to another index by explicitly setting it in your class.

elastics.index = 'foo'
elastics.type

By default the type used for your model is the full-underscored class name. For example MyModel::SuperSpecial will generate my_model__super_special. Notice the double underscore in place of the commonly used slash, which avoids url conflicts. You can map your model to another type by explicitly setting it in your class:

elastics.type = 'bar'
elastics.parent

Defines elasticsearch parent/child relations (see Indexing Records).

elastics.sync(*synced)

Implemented by the Elastics::ModelSyncer module (see elastics.sync).

elastics.synced

Implemented by the Elastics::ModelSyncer module (see elastics.synced).

elastics_result(result)

Custom defined method (see Model.elasticsresult).

self.elastics_in_batches(options, block)

Custom defined method (see Model.elasticsinbatches).

Instance Methods

You usually don’t need to deal with this methods unless you sync manually or have very special needs.

elastics

The elastics instance object is mostly used internally, however you can access all the Elastics features included in your records through it. It implements the methods to manage the elasticsearch document related to your record.

elastics.index

The index for this record.

elastics.type

The document type for this record.

elastics.store

Indexes the elastics_source of this record. It is used internally and you don't need to use this directly if you use Model.elastics.sync on this model.

elastics.remove

Removes the elasticsearch document for this record. It is used internally and you don't need to use this directly if you use Model.elastics.sync on this model.

elastics.sync

Implemented by the Elastics::ModelSyncer module (see elastics.sync).

elastics.get

Retrieves the elasticsearch document from the index. The result is extended as usual (see Elastics Extenders, Elastics Model Extenders and Elastics Rails Extenders). Mostly useful in the console to inspect the elasticsearch related document.

>> comment = Comment.first
>> comment.elastics.get
=> {"_index"=>"my_index", "_type"=>"review_comment","_id"=>"4f15d2712c16bf4337000012", ... }
elastics_source

(see Indexing Records)

elastics_indexable?

(see Indexing Fields)

elastics_action

Should return the elasticsearch action. Useful when you have some logic in your models to delete documents on import. Default 'index'.

Overriding Elastics Metafields
elastics_id, elastics_index, elastics_type, elastics_parent, elastics_routing

Use only if you know what you are doing!

You define any of this methods in your model, if you want to override the elastics.id, elastics.index, elastics.type, elastics.parent and/or elastics.routing generation.

The elastics_index should be used if you want to implement dynamic indices in your model.