Module: Realm::Elasticsearch::RakeTasks

Defined in:
lib/realm/elasticsearch/rake_tasks.rb

Class Method Summary collapse

Class Method Details

.setup(engine_name, engine_root: nil, url: ENV['ELASTICSEARCH_URL']) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/realm/elasticsearch/rake_tasks.rb', line 9

def setup(engine_name, engine_root: nil, url: ENV['ELASTICSEARCH_URL'])
  return unless url

  client = ::Elasticsearch::Client.new(url: url)

  Rake.application.in_namespace(:es) do
    Rake::Task.define_task(:create_indexes) do
      with_definitions(engine_name, engine_root) do |index, config|
        client.indices.create(index: index, body: config) unless client.indices.exists(index: index)
      end
    end

    Rake::Task.define_task(:drop_indexes) do
      with_definitions(engine_name, engine_root) do |index, _config|
        client.indices.delete(index: index) if client.indices.exists(index: index)
      end
    end
  end
end