class Realm::Elasticsearch::Repository

Attributes

client[R]

Public Class Methods

index_name(value = :not_provided) click to toggle source
# 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
new(client) click to toggle source
# File lib/realm/elasticsearch/repository.rb, line 20
def initialize(client)
  @client = client
end
repo_name(value = :not_provided) click to toggle source
# 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

Public Instance Methods

all() click to toggle source
# File lib/realm/elasticsearch/repository.rb, line 30
def all
  format_multiple(raw_search(query: { match_all: {} }))
end
create(id: nil, **attrs) click to toggle source
# 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:) click to toggle source
# 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) click to toggle source
# 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:) click to toggle source
# 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_update(id, body = {}) click to toggle source
# 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) click to toggle source
# File lib/realm/elasticsearch/repository.rb, line 55
def search_by(params)
  format_multiple(raw_search(query: { bool: { must: match_params(params) } }))
end
truncate!() click to toggle source
# 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) click to toggle source
# File lib/realm/elasticsearch/repository.rb, line 40
def update(id:, **attrs)
  raw_update(id, doc: attrs)
end
upsert(id:, **attrs) click to toggle source
# File lib/realm/elasticsearch/repository.rb, line 44
def upsert(id:, **attrs)
  raw_update(id, doc: attrs, doc_as_upsert: true)
end

Protected Instance Methods

format_multiple(results) click to toggle source
# File lib/realm/elasticsearch/repository.rb, line 94
def format_multiple(results)
  docs = results.dig('hits', 'hits').map { |doc| format_single(doc) }
  { docs: docs }
end
format_single(result) click to toggle source
# File lib/realm/elasticsearch/repository.rb, line 90
def format_single(result)
  result['_source'].merge(id: result['_id']).deep_symbolize_keys
end

Private Instance Methods

index_name() click to toggle source
# File lib/realm/elasticsearch/repository.rb, line 113
def index_name
  self.class.index_name
end
match_params(params) click to toggle source
# File lib/realm/elasticsearch/repository.rb, line 109
def match_params(params)
  params.map { |(key, value)| { match: { key => value } } }
end
refresh?() click to toggle source
# File lib/realm/elasticsearch/repository.rb, line 103
def refresh?
  # impacts performance so should be used only in TEST env
  # TODO: remove dependency on Rails
  defined?(::Rails) && ::Rails.env.test?
end