Class: Elasticated::Client
- Inherits:
-
Object
- Object
- Elasticated::Client
- Includes:
- Mixins::Configurable
- Defined in:
- lib/elasticated/client.rb
Instance Attribute Summary collapse
-
#transport ⇒ Object
Returns the value of attribute transport.
Instance Method Summary collapse
- #bulk(bulk_body, opts = {}) ⇒ Object
- #count(body, opts = {}) ⇒ Object
- #create_alias(index_name, new_alias) ⇒ Object
- #create_index(index_name, opts = {}) ⇒ Object
- #create_percolator(query_source, opts = {}) ⇒ Object
- #delete_by_query(body, opts = {}) ⇒ Object
- #delete_document(document_id, opts = {}) ⇒ Object
- #get_document(document_id, opts = {}) ⇒ Object
- #get_documents(documents_ids, opts = {}) ⇒ Object
- #index_document(document_source, opts = {}) ⇒ Object
- #index_exists?(index_name) ⇒ Boolean
-
#initialize(opts = {}) ⇒ Client
constructor
A new instance of Client.
- #percolate(document_source, opts = {}) ⇒ Object
- #refresh ⇒ Object
- #refresh_index(index_name) ⇒ Object
- #refresh_indices(index_names) ⇒ Object
- #remove_alias(index_name, index_alias) ⇒ Object
- #remove_aliases(index_name) ⇒ Object
- #scroll(scroll_id, opts = {}) ⇒ Object
- #search(body, opts = {}) ⇒ Object
- #update_document(document_source, opts = {}) ⇒ Object
- #update_mapping(index_name, type_name, mapping, opts = {}) ⇒ Object
Methods included from Mixins::Configurable
Constructor Details
#initialize(opts = {}) ⇒ Client
Returns a new instance of Client.
7 8 9 |
# File 'lib/elasticated/client.rb', line 7 def initialize(opts={}) self.transport = ::Elasticsearch::Client.new Configuration..merge opts end |
Instance Attribute Details
#transport ⇒ Object
Returns the value of attribute transport.
5 6 7 |
# File 'lib/elasticated/client.rb', line 5 def transport @transport end |
Instance Method Details
#bulk(bulk_body, opts = {}) ⇒ Object
122 123 124 125 |
# File 'lib/elasticated/client.rb', line 122 def bulk(bulk_body, opts={}) log_query :bulk, bulk_body, opts transport.bulk opts.merge body: bulk_body end |
#count(body, opts = {}) ⇒ Object
87 88 89 90 |
# File 'lib/elasticated/client.rb', line 87 def count(body, opts={}) log_query :count, body.to_json, opts transport.count opts.merge body: body end |
#create_alias(index_name, new_alias) ⇒ Object
51 52 53 54 |
# File 'lib/elasticated/client.rb', line 51 def create_alias(index_name, new_alias) transport.indices.put_alias index: index_name, name: new_alias log.info "Alias '#{new_alias}' for index '#{index_name}' created" end |
#create_index(index_name, opts = {}) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/elasticated/client.rb', line 38 def create_index(index_name, opts={}) args = { index: index_name } shards = opts[:shards] mapping = opts[:mapping] body = Hash.new body.merge!(settings: { number_of_shards: shards }) if shards body.merge!(mappings: mapping.to_h) if mapping # both Hash and Mapping::Builder responds to 'to_h' args.merge! body: body unless body.empty? log.info "Creating index '#{index_name}'" transport.indices.create args log.info "Index '#{index_name}' created" end |
#create_percolator(query_source, opts = {}) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/elasticated/client.rb', line 21 def create_percolator(query_source, opts={}) transport.index index: opts.fetch(:index), type: '.percolator', id: opts.fetch(:id), body: { query: query_source } end |
#delete_by_query(body, opts = {}) ⇒ Object
102 103 104 105 |
# File 'lib/elasticated/client.rb', line 102 def delete_by_query(body, opts={}) log_query :delete, body.to_json, opts transport.delete_by_query opts.merge body: body end |
#delete_document(document_id, opts = {}) ⇒ Object
117 118 119 120 |
# File 'lib/elasticated/client.rb', line 117 def delete_document(document_id, opts={}) log_query :delete, document_id, opts transport.delete opts.merge id: document_id end |
#get_document(document_id, opts = {}) ⇒ Object
107 108 109 110 |
# File 'lib/elasticated/client.rb', line 107 def get_document(document_id, opts={}) log_query :get, document_id, opts transport.get opts.merge id: document_id end |
#get_documents(documents_ids, opts = {}) ⇒ Object
112 113 114 115 |
# File 'lib/elasticated/client.rb', line 112 def get_documents(documents_ids, opts={}) log_query :mget, documents_ids, opts transport.mget opts.merge body: { ids: documents_ids } end |
#index_document(document_source, opts = {}) ⇒ Object
11 12 13 14 |
# File 'lib/elasticated/client.rb', line 11 def index_document(document_source, opts={}) response = transport.index opts.merge body: document_source log.debug "Document indexed #{response.to_json}" end |
#index_exists?(index_name) ⇒ Boolean
34 35 36 |
# File 'lib/elasticated/client.rb', line 34 def index_exists?(index_name) transport.indices.exists index: index_name end |
#percolate(document_source, opts = {}) ⇒ Object
28 29 30 31 32 |
# File 'lib/elasticated/client.rb', line 28 def percolate(document_source, opts={}) transport.percolate index: opts.fetch(:index), type: opts.fetch(:type), body: { doc: document_source } end |
#refresh ⇒ Object
83 84 85 |
# File 'lib/elasticated/client.rb', line 83 def refresh transport.indices.refresh end |
#refresh_index(index_name) ⇒ Object
72 73 74 75 |
# File 'lib/elasticated/client.rb', line 72 def refresh_index(index_name) transport.indices.refresh index: index_name log.debug "Index '#{index_name}' refreshed" end |
#refresh_indices(index_names) ⇒ Object
77 78 79 80 81 |
# File 'lib/elasticated/client.rb', line 77 def refresh_indices(index_names) indices = index_names*',' transport.indices.refresh index: indices log.debug "Indices '#{indices}' refreshed" end |
#remove_alias(index_name, index_alias) ⇒ Object
63 64 65 66 |
# File 'lib/elasticated/client.rb', line 63 def remove_alias(index_name, index_alias) transport.indices.delete_alias index: index_name, name: index_alias log.info "Alias '#{index_alias}' removed from index '#{index_name}'" end |
#remove_aliases(index_name) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/elasticated/client.rb', line 56 def remove_aliases(index_name) aliases = transport.indices.get_aliases[index_name]['aliases'].map(&:first) rescue Array.new aliases.each do |index_alias| remove_alias index_name, index_alias end end |
#scroll(scroll_id, opts = {}) ⇒ Object
97 98 99 100 |
# File 'lib/elasticated/client.rb', line 97 def scroll(scroll_id, opts={}) log_query :scroll, scroll_id, opts transport.scroll opts.merge scroll_id: scroll_id end |
#search(body, opts = {}) ⇒ Object
92 93 94 95 |
# File 'lib/elasticated/client.rb', line 92 def search(body, opts={}) log_query :search, body.to_json, opts transport.search opts.merge body: body end |
#update_document(document_source, opts = {}) ⇒ Object
16 17 18 19 |
# File 'lib/elasticated/client.rb', line 16 def update_document(document_source, opts={}) response = transport.update opts.merge body: { doc: document_source } log.debug "Document updated #{response.to_json}" end |
#update_mapping(index_name, type_name, mapping, opts = {}) ⇒ Object
68 69 70 |
# File 'lib/elasticated/client.rb', line 68 def update_mapping(index_name, type_name, mapping, opts={}) transport.indices.put_mapping opts.merge(index: index_name, type: type_name, body: mapping) end |