Class: Elastic::Client
- Inherits:
-
Object
- Object
- Elastic::Client
- Defined in:
- lib/elastic/client.rb,
lib/elastic/client/error.rb
Defined Under Namespace
Modules: Error
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
- #alias_exists?(options) ⇒ Boolean
- #alias_index(options) ⇒ Object
- #bulk(data, options = {}) ⇒ Object
- #bulk_operation(action, index, id, data = {}) ⇒ Object
- #clear_scroll(*args) ⇒ Object
- #count(*args) ⇒ Object
- #create_index(options) ⇒ Object
- #delete_index(options) ⇒ Object
- #get(index, id) ⇒ Object
- #get_alias(options) ⇒ Object
- #index_aliased?(options) ⇒ Boolean
- #index_exists?(options) ⇒ Boolean
- #indices(options = {}) ⇒ Object
-
#initialize(connection_options = {}) ⇒ Client
constructor
A new instance of Client.
- #mget(index, ids) ⇒ Object
- #refresh_index(options) ⇒ Object
- #resolve_alias(options) ⇒ Object
- #scroll(*args) ⇒ Object
- #search(*args) ⇒ Object
Constructor Details
#initialize(connection_options = {}) ⇒ Client
Returns a new instance of Client.
7 8 9 |
# File 'lib/elastic/client.rb', line 7 def initialize( = {}) @client = Elasticsearch::Client.new() end |
Instance Attribute Details
#client ⇒ Object (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
34 35 36 |
# File 'lib/elastic/client.rb', line 34 def alias_exists?() execute { indices.exists_alias?() } 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() if alias_exists?(name: [:name]) index_names = resolve_alias(name: [:name]) actions = index_names.map do |index| { remove: { alias: [:name], index: index } } end actions << { add: { alias: [:name], index: [:index] } } execute { indices.update_aliases(body: { actions: actions }) } else execute { indices.put_alias() } end end |
#bulk(data, options = {}) ⇒ Object
75 76 77 78 |
# File 'lib/elastic/client.rb', line 75 def bulk(data, = {}) = .merge(body: data) execute { bulk() } 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
124 125 126 127 128 |
# File 'lib/elastic/client.rb', line 124 def clear_scroll(*args) execute { clear_scroll(*args) } rescue nil end |
#count(*args) ⇒ Object
116 117 118 |
# File 'lib/elastic/client.rb', line 116 def count(*args) execute { count(*args) } end |
#create_index(options) ⇒ Object
18 19 20 |
# File 'lib/elastic/client.rb', line 18 def create_index() execute { indices.create() } end |
#delete_index(options) ⇒ Object
22 23 24 |
# File 'lib/elastic/client.rb', line 22 def delete_index() execute { indices.delete() } end |
#get(index, id) ⇒ Object
91 92 93 |
# File 'lib/elastic/client.rb', line 91 def get(index, id) execute { get(id: id, index: index) } end |
#get_alias(options) ⇒ Object
38 39 40 |
# File 'lib/elastic/client.rb', line 38 def get_alias() execute { indices.get_alias() } end |
#index_aliased?(options) ⇒ Boolean
66 67 68 69 70 71 72 73 |
# File 'lib/elastic/client.rb', line 66 def index_aliased?() if alias_exists?() index_alias = get_alias() index_alias.has_key?([:index]) else false end end |
#index_exists?(options) ⇒ Boolean
30 31 32 |
# File 'lib/elastic/client.rb', line 30 def index_exists?() execute { indices.exists?() } end |
#indices(options = {}) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/elastic/client.rb', line 11 def indices( = {}) = .merge(format: 'json') execute { cat.indices() } rescue => ex ex.status == 404 ? [] : raise end |
#mget(index, ids) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/elastic/client.rb', line 95 def mget(index, ids) ids = Array(ids) return [] if ids.empty? docs = ids.map { |id| { _index: index, _id: id } } = { index: index, body: { docs: docs } } results = execute { mget() } results['docs'].select { |doc| doc['found'] } end |
#refresh_index(options) ⇒ Object
26 27 28 |
# File 'lib/elastic/client.rb', line 26 def refresh_index() execute { indices.refresh() } end |
#resolve_alias(options) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/elastic/client.rb', line 42 def resolve_alias() if alias_exists?() index_alias = get_alias(name: [:name]) index_alias.keys else [] end end |
#scroll(*args) ⇒ Object
120 121 122 |
# File 'lib/elastic/client.rb', line 120 def scroll(*args) execute { scroll(*args) } end |
#search(*args) ⇒ Object
112 113 114 |
# File 'lib/elastic/client.rb', line 112 def search(*args) execute { search(*args) } end |