module Elasticsearch::Persistence::Repository::Find

Retrieves one or more domain objects from the repository

Constants

DOCS

The key for accessing the document found and returned from an

Elasticsearch _mget query.
FOUND

The key for the boolean value indicating whether a particular id

has been successfully found in an Elasticsearch _mget query.

Public Instance Methods

exists?(id, options={}) click to toggle source

Return if object exists in the repository

@example

repository.exists?(1)
=> true

@param [ String, Integer ] id The id to search. @param [ Hash ] options The options.

@return [true, false]

# File lib/elasticsearch/persistence/repository/find.rb, line 65
def exists?(id, options={})
  request = { index: index_name, id: id }
  client.exists(request.merge(options))
end
find(*args) click to toggle source

Retrieve a single object or multiple objects from Elasticsearch by ID or IDs

@example Retrieve a single object by ID

repository.find(1)
# => <Note ...>

@example Retrieve multiple objects by IDs

repository.find(1, 2)
# => [<Note ...>, <Note ...>

@return [Object,Array]

# File lib/elasticsearch/persistence/repository/find.rb, line 41
def find(*args)
  options  = args.last.is_a?(Hash) ? args.pop : {}
  ids      = args

  if args.size == 1
    id = args.pop
    id.is_a?(Array) ? __find_many(id, options) : __find_one(id, options)
  else
    __find_many args, options
  end
end

Private Instance Methods

__find_many(ids, options={}) click to toggle source

@api private

# File lib/elasticsearch/persistence/repository/find.rb, line 94
def __find_many(ids, options={})
  request = { index: index_name, body: { ids: ids } }
  documents = client.mget(request.merge(options))
  documents[DOCS].map do |document|
    deserialize(document) if document[FOUND]
  end
end
__find_one(id, options={}) click to toggle source

@api private

# File lib/elasticsearch/persistence/repository/find.rb, line 84
def __find_one(id, options={})
  request = { index: index_name, id: id }
  document = client.get(request.merge(options))
  deserialize(document)
rescue Elastic::Transport::Transport::Errors::NotFound => e
  raise DocumentNotFound, e.message, caller
end