Class: Elastic::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic/index.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index_name = nil) ⇒ Index

Returns a new instance of Index.



25
26
27
# File 'lib/elastic/index.rb', line 25

def initialize(index_name = nil)
  @index_name = (index_name || self.class.generate_index_name).downcase
end

Class Attribute Details

.alias_nameObject

Returns the value of attribute alias_name.



4
5
6
# File 'lib/elastic/index.rb', line 4

def alias_name
  @alias_name
end

.clientObject

Returns the value of attribute client.



4
5
6
# File 'lib/elastic/index.rb', line 4

def client
  @client
end

.mappingsObject

Returns the value of attribute mappings.



4
5
6
# File 'lib/elastic/index.rb', line 4

def mappings
  @mappings
end

.settingsObject

Returns the value of attribute settings.



4
5
6
# File 'lib/elastic/index.rb', line 4

def settings
  @settings
end

Instance Attribute Details

#index_nameObject (readonly)

Returns the value of attribute index_name.



23
24
25
# File 'lib/elastic/index.rb', line 23

def index_name
  @index_name
end

Class Method Details

.generate_index_nameObject



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

def generate_index_name
  "#{alias_name}-#{Time.now.to_i}".downcase
end

.inherited(klass) ⇒ Object



6
7
8
9
10
# File 'lib/elastic/index.rb', line 6

def inherited(klass)
  klass.alias_name    = Elastic::Helpers.to_alias_name(klass)
  klass.settings      = {}
  klass.mappings      = {}
end

.resolveObject



12
13
14
15
16
# File 'lib/elastic/index.rb', line 12

def resolve
  if index_name = client.resolve_alias(name: alias_name).first
    new(index_name)
  end
end

Instance Method Details

#==(other) ⇒ Object



129
130
131
# File 'lib/elastic/index.rb', line 129

def ==(other)
  self.class == other.class && index_name == other.index_name
end

#alias_nameObject



125
126
127
# File 'lib/elastic/index.rb', line 125

def alias_name
  self.class.alias_name
end

#bufferObject



117
118
119
120
121
122
123
# File 'lib/elastic/index.rb', line 117

def buffer
  @buffer ||=
    Buffer.new do |operations|
      @dirty = true
      client.bulk(operations)
    end
end

#bulk(action, id, data = {}) ⇒ Object



94
95
96
# File 'lib/elastic/index.rb', line 94

def bulk(action, id, data = {})
  buffer << bulk_operation(action, id, data)
end

#bulk_operation(action, id, data = {}) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/elastic/index.rb', line 98

def bulk_operation(action, id, data = {})
   = {
    _index: index_name,
    _id:    id,
    retry_on_conflict: 3
  }

  if action.to_sym == :upsert
    data ||= {}
    data[:doc_as_upsert] = true

    action = :update
  end

  [:data] = data unless data.empty?

  { action.to_sym =>  }
end

#count(query = {}) ⇒ Object



63
64
65
66
# File 'lib/elastic/index.rb', line 63

def count(query = {})
  response = client.count(index: index_name, body: query)
  response['count']
end

#createObject



29
30
31
32
33
34
35
36
# File 'lib/elastic/index.rb', line 29

def create
  body = {
    settings: self.class.settings,
    mappings: self.class.mappings,
  }

  client.create_index(index: index_name, body: body)
end

#deleteObject



38
39
40
# File 'lib/elastic/index.rb', line 38

def delete
  client.delete_index(index: index_name)
end

#dirty?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/elastic/index.rb', line 59

def dirty?
  !!@dirty
end

#document_ids(query = {}, options = {}) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/elastic/index.rb', line 86

def document_ids(query = {}, options = {})
  defaults       = {}
  scroll_options = defaults.merge(options).merge(stored_fields: ['_id'])
  docs           = documents(query, scroll_options)

  docs.lazy.map { |doc| doc['_id'] }
end

#documents(query = {}, options = {}) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/elastic/index.rb', line 77

def documents(query = {}, options = {})
  defaults       = { body: query }
  scroll_options = defaults.merge(options)
  scroll         = Scroll.new(client, index_name, scroll_options)
  docs           = scroll.each

  docs.lazy.map { |doc| source_with_id(doc) }
end

#exists?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/elastic/index.rb', line 42

def exists?
  client.index_exists?(index: index_name)
end

#get(id) ⇒ Object



68
69
70
# File 'lib/elastic/index.rb', line 68

def get(id)
  mget([id]).first
end

#mget(ids) ⇒ Object



72
73
74
75
# File 'lib/elastic/index.rb', line 72

def mget(ids)
  docs = client.mget(index_name, ids)
  docs.map { |doc| source_with_id(doc) }
end

#promoteObject



46
47
48
# File 'lib/elastic/index.rb', line 46

def promote
  client.alias_index(name: alias_name, index: index_name)
end

#promoted?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/elastic/index.rb', line 50

def promoted?
  client.index_aliased?(name: alias_name, index: index_name)
end

#refreshObject



54
55
56
57
# File 'lib/elastic/index.rb', line 54

def refresh
  @dirty = false
  client.refresh_index(index: index_name)
end