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
-
#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.
28 29 30 |
# File 'lib/jay_api/elasticsearch/indexable.rb', line 28 def batch_size @batch_size end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
28 29 30 |
# File 'lib/jay_api/elasticsearch/indexable.rb', line 28 def client @client end |
Instance Method Details
#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: www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html
156 157 158 159 160 161 162 163 |
# File 'lib/jay_api/elasticsearch/indexable.rb', line 156 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: www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html
177 178 179 |
# File 'lib/jay_api/elasticsearch/indexable.rb', line 177 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.
111 112 113 114 115 |
# File 'lib/jay_api/elasticsearch/indexable.rb', line 111 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: www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#docs-index-api-response-body
84 85 86 87 |
# File 'lib/jay_api/elasticsearch/indexable.rb', line 84 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)
38 39 40 41 42 43 44 45 46 |
# File 'lib/jay_api/elasticsearch/indexable.rb', line 38 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).
54 55 56 57 58 59 60 61 |
# File 'lib/jay_api/elasticsearch/indexable.rb', line 54 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.
119 120 121 |
# File 'lib/jay_api/elasticsearch/indexable.rb', line 119 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: www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html
99 100 101 102 103 104 105 106 107 |
# File 'lib/jay_api/elasticsearch/indexable.rb', line 99 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 |