Class: Elastic::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic/client.rb,
lib/elastic/client/error.rb

Defined Under Namespace

Modules: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection_options = {}) ⇒ Client

Returns a new instance of Client.



7
8
9
# File 'lib/elastic/client.rb', line 7

def initialize(connection_options = {})
  @client = Elasticsearch::Client.new(connection_options)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/elastic/client.rb', line 5

def client
  @client
end

Instance Method Details

#alias_exists?(options) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/elastic/client.rb', line 34

def alias_exists?(options)
  execute { indices.exists_alias?(options) }
end

#alias_index(options) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/elastic/client.rb', line 51

def alias_index(options)
  if alias_exists?(name: options[:name])
    index_names = resolve_alias(name: options[:name])

    actions = index_names.map do |index|
      { remove: { alias: options[:name], index: index } }
    end
    actions << { add: { alias: options[:name], index: options[:index] } }

    execute { indices.update_aliases(body: { actions: actions }) }
  else
    execute { indices.put_alias(options) }
  end
end

#bulk(data, options = {}) ⇒ Object



75
76
77
78
# File 'lib/elastic/client.rb', line 75

def bulk(data, options = {})
  options = options.merge(body: data)
  execute { bulk(options) }
end

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



80
81
82
83
84
85
86
87
88
89
# File 'lib/elastic/client.rb', line 80

def bulk_operation(action, index, id, data = {})
   = {
    _index: index,
    _id:    id,
  }

  [:data] = data if data && !data.empty?

  { action.to_sym =>  }
end

#clear_scroll(*args) ⇒ Object



120
121
122
123
124
# File 'lib/elastic/client.rb', line 120

def clear_scroll(*args)
  execute { clear_scroll(*args) }
rescue
  nil
end

#count(*args) ⇒ Object



112
113
114
# File 'lib/elastic/client.rb', line 112

def count(*args)
  execute { count(*args) }
end

#create_index(options) ⇒ Object



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

def create_index(options)
  execute { indices.create(options) }
end

#delete_index(options) ⇒ Object



22
23
24
# File 'lib/elastic/client.rb', line 22

def delete_index(options)
  execute { indices.delete(options) }
end

#get_alias(options) ⇒ Object



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

def get_alias(options)
  execute { indices.get_alias(options) }
end

#index_aliased?(options) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
# File 'lib/elastic/client.rb', line 66

def index_aliased?(options)
  if alias_exists?(options)
    index_alias = get_alias(options)
    index_alias.has_key?(options[:index])
  else
    false
  end
end

#index_exists?(options) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/elastic/client.rb', line 30

def index_exists?(options)
  execute { indices.exists?(options) }
end

#indices(options = {}) ⇒ Object



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

def indices(options = {})
  options = options.merge(format: 'json')
  execute { cat.indices(options) }
rescue => ex
  ex.status == 404 ? [] : raise
end

#mget(index, ids) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/elastic/client.rb', line 91

def mget(index, ids)
  ids = Array(ids)
  return [] if ids.empty?

  docs = ids.map { |id| { _index: index, _id: id } }

  options = {
    index: index,
    body: {
      docs: docs
    }
  }

  results = execute { mget(options) }
  results['docs'].select { |doc| doc['found'] }
end

#refresh_index(options) ⇒ Object



26
27
28
# File 'lib/elastic/client.rb', line 26

def refresh_index(options)
  execute { indices.refresh(options) }
end

#resolve_alias(options) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/elastic/client.rb', line 42

def resolve_alias(options)
  if alias_exists?(options)
    index_alias = get_alias(name: options[:name])
    index_alias.keys
  else
    []
  end
end

#scroll(*args) ⇒ Object



116
117
118
# File 'lib/elastic/client.rb', line 116

def scroll(*args)
  execute { scroll(*args) }
end

#search(*args) ⇒ Object



108
109
110
# File 'lib/elastic/client.rb', line 108

def search(*args)
  execute { search(*args) }
end