Module: Elasticsearch::Persistence::Repository::Find

Included in:
Class
Defined in:
lib/elasticsearch/persistence/repository/find.rb

Overview

Retrieves one or more domain objects from the repository

Instance Method Summary collapse

Instance Method Details

#__find_many(ids, options = {}) ⇒ 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.



63
64
65
66
67
68
# File 'lib/elasticsearch/persistence/repository/find.rb', line 63

def __find_many(ids, options={})
  type     = document_type || (klass ? __get_type_from_class(klass) : '_all')
  documents = client.mget( { index: index_name,  body: { ids: ids } }.merge(options) )

  documents['docs'].map { |document| document['found'] ? deserialize(document) : nil }
end

#__find_one(id, options = {}) ⇒ 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.



52
53
54
55
56
57
58
59
# File 'lib/elasticsearch/persistence/repository/find.rb', line 52

def __find_one(id, options={})
  type     = document_type || (klass ? __get_type_from_class(klass) : '_all')
  document = client.get( { index: index_name, id: id }.merge(options) )

  deserialize(document)
rescue Elasticsearch::Transport::Transport::Errors::NotFound => e
  raise DocumentNotFound, e.message, caller
end

#exists?(id, options = {}) ⇒ true, false

Return if object exists in the repository

Examples:


repository.exists?(1)
=> true

Returns:

  • (true, false)


45
46
47
48
# File 'lib/elasticsearch/persistence/repository/find.rb', line 45

def exists?(id, options={})
  type     = document_type || (klass ? __get_type_from_class(klass) : '_all')
  client.exists( { index: index_name,  id: id }.merge(options) )
end

#find(*args) ⇒ Object, Array

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

Examples:

Retrieve a single object by ID


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

Retrieve multiple objects by IDs


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

Returns:

  • (Object, Array)


24
25
26
27
28
29
30
31
32
33
34
# File 'lib/elasticsearch/persistence/repository/find.rb', line 24

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