Class: Realm::Elasticsearch::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/realm/elasticsearch/repository.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Repository

Returns a new instance of Repository.



20
21
22
# File 'lib/realm/elasticsearch/repository.rb', line 20

def initialize(client)
  @client = client
end

Class Method Details

.index_name(value = :not_provided) ⇒ Object



14
15
16
17
18
# File 'lib/realm/elasticsearch/repository.rb', line 14

def self.index_name(value = :not_provided)
  @index_name = value.to_sym unless value == :not_provided
  @index_name = repo_name.pluralize unless defined?(@index_name)
  @index_name
end

.repo_name(value = :not_provided) ⇒ Object



8
9
10
11
12
# File 'lib/realm/elasticsearch/repository.rb', line 8

def self.repo_name(value = :not_provided)
  @repo_name = value.to_sym unless value == :not_provided
  @repo_name = name.demodulize.underscore unless defined?(@repo_name)
  @repo_name
end

Instance Method Details

#allObject



30
31
32
# File 'lib/realm/elasticsearch/repository.rb', line 30

def all
  format_multiple(raw_search(query: { match_all: {} }))
end

#create(id: nil, **attrs) ⇒ Object



34
35
36
37
38
# File 'lib/realm/elasticsearch/repository.rb', line 34

def create(id: nil, **attrs)
  client.index(index: index_name, type: '_doc', id: id, body: attrs, refresh: refresh?)
rescue ::Elasticsearch::Transport::Transport::Errors::Conflict
  raise Realm::Persistence::Conflict
end

#delete(id:) ⇒ Object



48
49
50
51
52
53
# File 'lib/realm/elasticsearch/repository.rb', line 48

def delete(id:)
  client.delete(index: index_name, type: '_doc', id: id, refresh: refresh?)
  true
rescue ::Elasticsearch::Transport::Transport::Errors::NotFound
  false
end

#delete_by(params) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/realm/elasticsearch/repository.rb', line 59

def delete_by(params)
  client.delete_by_query(
    index: index_name,
    refresh: refresh?,
    body: { query: {
      bool: {
        must: match_params(params),
      },
    } },
  )
end

#find(id:) ⇒ Object



24
25
26
27
28
# File 'lib/realm/elasticsearch/repository.rb', line 24

def find(id:)
  format_single(client.get(index: index_name, id: id))
rescue ::Elasticsearch::Transport::Transport::Errors::NotFound
  nil
end

#raw_search(yaml: nil, options: {}, **body) ⇒ Object



75
76
77
# File 'lib/realm/elasticsearch/repository.rb', line 75

def raw_search(yaml: nil, options: {}, **body)
  client.search(index: index_name, body: yaml ? YAML.safe_load(yaml) : body, **options)
end

#raw_update(id, body = {}) ⇒ Object



71
72
73
# File 'lib/realm/elasticsearch/repository.rb', line 71

def raw_update(id, body = {})
  client.update(index: index_name, type: '_doc', id: id, body: body, refresh: refresh?)
end

#search_by(params) ⇒ Object



55
56
57
# File 'lib/realm/elasticsearch/repository.rb', line 55

def search_by(params)
  format_multiple(raw_search(query: { bool: { must: match_params(params) } }))
end

#truncate!Object



79
80
81
82
83
84
85
86
# File 'lib/realm/elasticsearch/repository.rb', line 79

def truncate!
  client.delete_by_query(
    index: index_name,
    body: { query: { match_all: {} } },
    conflicts: 'proceed',
    refresh: refresh?,
  )
end

#update(id:, **attrs) ⇒ Object



40
41
42
# File 'lib/realm/elasticsearch/repository.rb', line 40

def update(id:, **attrs)
  raw_update(id, doc: attrs)
end

#upsert(id:, **attrs) ⇒ Object



44
45
46
# File 'lib/realm/elasticsearch/repository.rb', line 44

def upsert(id:, **attrs)
  raw_update(id, doc: attrs, doc_as_upsert: true)
end