Module: ElasticRecord::Index::Manage

Included in:
ElasticRecord::Index
Defined in:
lib/elastic_record/index/manage.rb

Instance Method Summary collapse

Instance Method Details

#aliased_namesObject



67
68
69
70
71
72
73
74
75
76
# File 'lib/elastic_record/index/manage.rb', line 67

def aliased_names
  if (index_names = all_names).any?
    json = connection.json_get "/#{index_names.join(',')}/_alias"
    json.keys.select do |index_name|
      json[index_name]['aliases'].keys.include?(alias_name)
    end
  else
    []
  end
end

#all_namesObject



78
79
80
81
82
83
# File 'lib/elastic_record/index/manage.rb', line 78

def all_names
  connection.json_get("/#{alias_name}/_mapping/#{model.doctype.name}/").keys
rescue
  # TODO: In ES 1.4, this returns empty rather than a 404
  []
end

#create(index_name = new_index_name) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/elastic_record/index/manage.rb', line 10

def create(index_name = new_index_name)
  connection.json_put "/#{index_name}", {
    "mappings" => mapping_body,
    "settings" => settings
  }
  index_name
end

#create_and_deploy(index_name = new_index_name) ⇒ Object



4
5
6
7
8
# File 'lib/elastic_record/index/manage.rb', line 4

def create_and_deploy(index_name = new_index_name)
  create(index_name)
  deploy(index_name)
  index_name
end

#delete(index_name) ⇒ Object



18
19
20
# File 'lib/elastic_record/index/manage.rb', line 18

def delete(index_name)
  connection.json_delete "/#{index_name}"
end

#delete_allObject



22
23
24
25
26
# File 'lib/elastic_record/index/manage.rb', line 22

def delete_all
  all_names.each do |index_name|
    delete index_name
  end
end

#deploy(index_name) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/elastic_record/index/manage.rb', line 36

def deploy(index_name)
  actions = [
    {
      add: {
        "index" => index_name,
        "alias" => alias_name
      }
    }
  ]

  (aliased_names - [index_name]).each do |index_to_remove|
    actions << {
      remove: {
        "index" => index_to_remove,
        "alias" => alias_name
      }
    }
  end

  connection.json_post '/_aliases', actions: actions
end

#exists?(index_name) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/elastic_record/index/manage.rb', line 28

def exists?(index_name)
  connection.head("/#{index_name}") == '200'
end

#refresh(index_name = alias_name) ⇒ Object



58
59
60
# File 'lib/elastic_record/index/manage.rb', line 58

def refresh(index_name = alias_name)
  connection.json_post "/#{index_name}/_refresh"
end

#resetObject



62
63
64
65
# File 'lib/elastic_record/index/manage.rb', line 62

def reset
  delete_all
  create_and_deploy
end

#type_exists?(index_name = alias_name, type = model.doctype.name) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/elastic_record/index/manage.rb', line 32

def type_exists?(index_name = alias_name, type = model.doctype.name)
  connection.head("/#{index_name}/_mapping/#{type}") == '200'
end