Module: JayAPI::Elasticsearch::Indexable
Overview
This module houses the Elasticsearch methods that can be used with a single or with multiple indexes. Its main purpose is to avoid code repetition between classes.
Constant Summary collapse
- DEFAULT_DOC_TYPE =
Default type for documents indexed with the #index method.
'nested'- SUPPORTED_TYPES =
Supported document types (for the #index method)
[DEFAULT_DOC_TYPE, nil].freeze
Instance Attribute Summary collapse
-
#batch_size ⇒ Object
readonly
Returns the value of attribute batch_size.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
-
#count(query: nil) ⇒ JayAPI::Elasticsearch::Count
The count of documents in the index(es) matching the given query (if any).
-
#delete_by_query(query, slices: nil, wait_for_completion: true) ⇒ Hash
Delete the documents matching the given query from the Index.
-
#delete_by_query_async(query, slices: nil) ⇒ Concurrent::Promise
Deletes asynchronously the documents matching the given query from the Index.
-
#flush ⇒ Object
Sends whatever is currently in the send queue to the Elasticsearch instance and clears the queue.
-
#index(data, type: DEFAULT_DOC_TYPE) ⇒ Array<Hash>
Sends a record to the Elasticsearch instance right away.
-
#initialize(client:, index_names:, batch_size: 100, logger: nil) ⇒ Object
:reek:ControlParameter (want to avoid the creating of the logger on method definition).
-
#push(data, type: DEFAULT_DOC_TYPE) ⇒ Object
Pushes a record into the index.
-
#queue_size ⇒ Integer
Returns the number of elements currently on the send queue.
-
#search(query, batch_counter: nil, type: nil) ⇒ JayAPI::Elasticsearch::QueryResults
Performs a query on the index.
Instance Attribute Details
#batch_size ⇒ Object (readonly)
Returns the value of attribute batch_size.
29 30 31 |
# File 'lib/jay_api/elasticsearch/indexable.rb', line 29 def batch_size @batch_size end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
29 30 31 |
# File 'lib/jay_api/elasticsearch/indexable.rb', line 29 def client @client end |
Instance Method Details
#count(query: nil) ⇒ JayAPI::Elasticsearch::Count
Returns The count of documents in the index(es) matching the given query (if any).
114 115 116 117 118 119 120 |
# File 'lib/jay_api/elasticsearch/indexable.rb', line 114 def count(query: nil) body = query ? { query: query } : nil JayAPI::Elasticsearch::Count.new( client.count(**{ index: index_names, body: body }.compact) ) end |
#delete_by_query(query, slices: nil, wait_for_completion: true) ⇒ Hash
Delete the documents matching the given query from the Index. For more information on how to build the query please refer to the Elasticsearch DSL documentation: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html
169 170 171 172 173 174 175 176 |
# File 'lib/jay_api/elasticsearch/indexable.rb', line 169 def delete_by_query(query, slices: nil, wait_for_completion: true) request_params = { index: index_names, body: query }.tap do |params| params.merge!(slices: slices) if slices params.merge!(wait_for_completion: false) unless wait_for_completion end client.delete_by_query(**request_params).deep_symbolize_keys end |
#delete_by_query_async(query, slices: nil) ⇒ Concurrent::Promise
Deletes asynchronously the documents matching the given query from the Index. For more information on how to build the query please refer to the Elasticsearch DSL documentation: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html
190 191 192 |
# File 'lib/jay_api/elasticsearch/indexable.rb', line 190 def delete_by_query_async(query, slices: nil) async.delete_by_query(query, slices: slices) end |
#flush ⇒ Object
Sends whatever is currently in the send queue to the Elasticsearch instance and clears the queue.
124 125 126 127 128 |
# File 'lib/jay_api/elasticsearch/indexable.rb', line 124 def flush return unless @batch.any? flush! end |
#index(data, type: DEFAULT_DOC_TYPE) ⇒ Array<Hash>
Sends a record to the Elasticsearch instance right away. { "_index" => "xyz01_unit_test", "_type" => "nested", "_id" => "SVY1mJEBQ5CNFZM8Lodt", "_version" => 1, "result" => "created", "_shards" => { "total" => 2, "successful" => 1, "failed" => 0 }, "_seq_no" => 0, "_primary_term" => 1 }
For information on the contents of this Hash please see: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#docs-index-api-response-body
85 86 87 88 |
# File 'lib/jay_api/elasticsearch/indexable.rb', line 85 def index(data, type: DEFAULT_DOC_TYPE) validate_type(type) index_names.map { |index_name| client.index index: index_name, type: type, body: data } end |
#initialize(client:, index_names:, batch_size: 100, logger: nil) ⇒ Object
:reek:ControlParameter (want to avoid the creating of the logger on method definition)
39 40 41 42 43 44 45 46 47 |
# File 'lib/jay_api/elasticsearch/indexable.rb', line 39 def initialize(client:, index_names:, batch_size: 100, logger: nil) @logger = logger || Logging.logger[self] @client = client @index_names = index_names @batch_size = batch_size @batch = [] end |
#push(data, type: DEFAULT_DOC_TYPE) ⇒ Object
Pushes a record into the index. (This does not send the record to the Elasticsearch instance, only puts it into the send queue).
55 56 57 58 59 60 61 62 |
# File 'lib/jay_api/elasticsearch/indexable.rb', line 55 def push(data, type: DEFAULT_DOC_TYPE) validate_type(type) index_names.each do |index_name| batch << { index: { _index: index_name, _type: type, data: data }.compact } end flush! if batch.size >= batch_size end |
#queue_size ⇒ Integer
Returns the number of elements currently on the send queue.
132 133 134 |
# File 'lib/jay_api/elasticsearch/indexable.rb', line 132 def queue_size batch.size end |
#search(query, batch_counter: nil, type: nil) ⇒ JayAPI::Elasticsearch::QueryResults
Performs a query on the index. For more information on how to build the query please refer to the Elasticsearch DSL query: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html
100 101 102 103 104 105 106 107 108 |
# File 'lib/jay_api/elasticsearch/indexable.rb', line 100 def search(query, batch_counter: nil, type: nil) begin response = Response.new(client.search(index: index_names, body: query)) rescue ::Elasticsearch::Transport::Transport::Errors::BadRequest logger.error "The 'search' query is invalid: #{JSON.pretty_generate(query)}" raise end query_results(query, response, batch_counter, type) end |