Flex::ActiveModel

The Flex::ActiveModel inclues the Flex::ModelIndexer, which includes Flex::ModelSyncer so you can use their method too (see Flex::ModelIndexer and Flex::ModelSyncer). Besides it includes the ActiveAttr::Model module from the active_attr, and the ActiveModel validations and callbacks, so refer to them for the documentation.

We list here only the specific methods or overrides that are not documented elsewhere. We also omit the standard CRUD methods that Flex::ActiveModel implements on its own but behave like in standard ActiveRecord.

Class Methods

attribute :properties

The attribute method is implemented by active_attr. Flex::ActiveModel extends it so it accepts a :properties argument that is used by flex to generate the mapping.

attribute :price, :properties => {'type' => 'float'}, :default => 0
attribute :analyzed

The attribute method is implemented by active_attr. Flex::ActiveModel extends it so it accepts a :analyzed argument that is expanded to :properties => { 'type' => 'string', 'index' => 'not_analyzed' } and used by flex to generate the maping.

attribute :code, :analyzed => false
attribute :not_analyzed

Same as :analized but with inverted logic

attribute :code, :not_analyzed => true
attribute_created_at

Adds the created_at attribute wich will be automatically filled by the date of the creation.

attribute_updated_at

Adds the updated_at attribute wich will be automatically filled by the date of the update.

attribute_timestamps

Shortcut that calls both attribute_created_at and attribute_updated_at.

attribute_attachment

Defines an attachment attribute that integrates with elasticsearch-mapper-attachments:

  • if you omit the arguments it uses :attachment as the <attachment_field_name>
  • you can also pass other properties that will be merged with the default property for attachment
  • it automatically adds a :<attachment_field_name>_scope scope which will add all the meta fields (title, author, …) to the returned fields, exluding the <attachment_field_name> field itself, and including all the other attributes declared before it. For that reason you may want to declare it as the latest attribute.

You can rely on the defaults, or you can override what you need. For example:

# adds a default 'attachment' attribute and a 'attachment_scope' scope
attribute_attachment
# adds a 'page' attribute and a 'page_scope' scope
attribute_attachment :page
# adds a 'file' attribute with explicit properties and a 'file_scope' scope
attribute_attachment :file, { :properties => { 'fields' => { 'file'   => { 'index'    => 'no' },
                                                             'title'  => { 'store'    => 'yes' },
                                                             'author' => { 'analyzer' => 'myAnalizer' } } } }

# a scope that returns the added fields, highlights and a query
scope :searchable do |query|
   attachment_scope
  .highlight(:fields => { :attachment          => {},
                          :'attachment.title'  => {},
                          :'attachment.author' => {} })
  .query_string(query)
end

# pass the attachments as encoded strings, as required by the plugin
MyModel.create :my_attr    => 'foo',
               :attachment => Base64.encode64(the_attachment),
               :page       => Base64.encode64(the_page),
               :file       => Base64.encode64(the_file)

result = MyModel.searchable(the_query_string).first

# the nested fields like 'page.title' and 'page.author' are accessible in ruby as flattened-name methods
puts result.page_title, result.page_author

Instance Methods

safe_update

This method implements the optimistic lock on update documented here. It is done through a simple block wrapper:

record.safe_update do |r|
  r.amount += 100
end

If you are trying to update a stale object, the block is yielded again with a fresh reloaded document and the document is saved only when it is not stale anymore (i.e. the _version has not changed since it has been loaded).

safe_update!

Like safe_update but will raise a Flex::ActiveModel::DocumentInvalidError if the document is not valid.

raw_result

This method returns the original elasticsearch result as it comes from the internal request. You can call it also on any array collection result.

raw_document

This method returns the original elasticsearch document as it comes from the internal request.

flex_id

You define this method if you want to manage the elasticsearch document id yourself. That is specially useful in certain contexts, when you may need to create a new document or update an old one, based on certain attributes (that identify the record). For example, you may want to reindex a document when it has the same source and date, while you want to create a new one if there is no document with that title and date. You may be tempted to query the index for the presence of the document, so decide to update or create, but there is an easier way: just generate an id based on title and date and that behaviour will happen automatically. For example:

def flex_id
  Digest::MD5.hexdigest [title, date.to_s(:date)].join
end

(see Overriding Flex Metafields)
document.method_missing

This method forwards the missing methods raw_document structure, so you can also call all the methods added by all the result extenders (see flex Result Extenders and flex-models Result Extenders)

Flex::RefreshCallback

You can include this module if you want flex to refresh the index automatically. It will add 2 callbacks: after_save and after_destroy with a call to the Flex.refresh_index API method.