Method: SearchFlip::Index::ClassMethods#index

Defined in:
lib/search_flip/index.rb

#index(scope, options = {}, additional_index_options = {}) ⇒ Object

Indexes the given record set, array of records or individual record. A record set usually is an ActiveRecord::Relation, but can be any other ORM as well. Uses the Elasticsearch bulk API no matter what is provided. Refreshes the index if auto_refresh is enabled. Raises SearchFlip::ResponseError in case any errors occur.

Examples:

CommentIndex.import Comment.all
CommentIndex.import [comment1, comment2]
CommentIndex.import Comment.first
CommentIndex.import Comment.all, ignore_errors: [409]
CommentIndex.import Comment.all, raise: false

Parameters:

  • scope

    A record set, array of records or individual record to index

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

    Specifies options regarding the bulk indexing

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

    Provides custom index options for eg routing, versioning, etc

Options Hash (options):

  • ignore_errors (Array)

    Specifies an array of http status codes that shouldn’t raise any exceptions, like eg 409 for conflicts, ie when optimistic concurrency control is used.

  • raise (Boolean)

    Prevents any exceptions from being raised. Please note that this only applies to the bulk response, not to the request in general, such that connection errors, etc will still raise.

See Also:



534
535
536
537
538
539
540
541
542
# File 'lib/search_flip/index.rb', line 534

def index(scope, options = {}, additional_index_options = {})
  bulk options do |indexer|
    each_record(scope, index_scope: true) do |object|
      indexer.index record_id(object), serialize(object), index_options(object).merge(additional_index_options)
    end
  end

  scope
end