Class: JayAPI::Elasticsearch::Index

Inherits:
Object
  • Object
show all
Includes:
Indexable
Defined in:
lib/jay_api/elasticsearch/index.rb

Overview

Represents an Elasticsearch index. Allows data to be pushed to it one record at a time or in batches of the specified size.

Constant Summary

Constants included from Indexable

JayAPI::Elasticsearch::Indexable::DEFAULT_DOC_TYPE, JayAPI::Elasticsearch::Indexable::SUPPORTED_TYPES

Instance Attribute Summary

Attributes included from Indexable

#batch_size, #client

Instance Method Summary collapse

Methods included from Indexable

#delete_by_query, #delete_by_query_async, #flush, #push, #queue_size, #search

Constructor Details

#initialize(client:, index_name:, batch_size: 100, logger: nil) ⇒ Index

Returns a new instance of Index.

Parameters:

  • client (JayAPI::Elasticsearch::Client)

    The Elasticsearch Client object.

  • index_name (String)

    The name of the Elasticsearch index.

  • batch_size (Integer) (defaults to: 100)

    The size of the batch. When this many items are pushed into the index they are flushed to the Elasticsearch instance.

  • logger (Logging::Logger, nil) (defaults to: nil)

    The logger object to use, if none is given a new one will be created.



24
25
26
# File 'lib/jay_api/elasticsearch/index.rb', line 24

def initialize(client:, index_name:, batch_size: 100, logger: nil)
  super(client: client, index_names: [index_name], batch_size: batch_size, logger: logger)
end

Instance Method Details

#force_merge(only_expunge_deletes: nil) ⇒ Hash

Starts a Forced Segment Merge process on the index.

⚠️ For big indexes this process can take a very long time, make sure to adjust the timeout when creating the client.

Parameters:

  • only_expunge_deletes (Boolean) (defaults to: nil)

    Specifies whether the operation should only remove deleted documents.

Returns:

  • (Hash)

    A Hash with the result of the index merging process, it looks like this:

    { "_shards" => { "total" => 10, "successful" => 10, "failed" => 0 } }

Raises:



117
118
119
120
121
122
123
124
125
126
# File 'lib/jay_api/elasticsearch/index.rb', line 117

def force_merge(only_expunge_deletes: nil)
  unless settings.blocks.write_blocked?
    raise ::JayAPI::Elasticsearch::Errors::WritableIndexError,
          "Write block for '#{index_name}' has not been enabled. " \
          "Please enable the index's write block before performing a segment merge"
  end

  params = { index: index_name, only_expunge_deletes: }.compact
  client.transport_client.indices.forcemerge(**params)
end

#index(data, type: DEFAULT_DOC_TYPE) ⇒ 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

Parameters:

  • data (Hash)

    The data to be sent.

  • type (String, nil) (defaults to: DEFAULT_DOC_TYPE)

    The type of the document. When set to nil the decision is left to Elasticsearch's API. Which will normally default to _doc.

Returns:

  • (Hash)

    A Hash containing information about the created document. An example of such Hash is:



54
55
56
# File 'lib/jay_api/elasticsearch/index.rb', line 54

def index(data, type: DEFAULT_DOC_TYPE)
  super.first
end

#index_nameString

Returns The name of the Elasticsearch index.

Returns:

  • (String)

    The name of the Elasticsearch index.



29
30
31
# File 'lib/jay_api/elasticsearch/index.rb', line 29

def index_name
  @index_name ||= index_names.first
end

#settingsJayAPI::Elasticsearch::Indices::Settings

Returns The settings for the index.

Returns:



100
101
102
103
# File 'lib/jay_api/elasticsearch/index.rb', line 100

def settings
  # DO NOT MEMOIZE! Leave it to the caller.
  ::JayAPI::Elasticsearch::Indices::Settings.new(client.transport_client, index_name)
end

#update(id:, doc: nil, script: nil) ⇒ Hash

Updates a document by ID. { "_index" : "xyz01_build_properties", "_type" : "_doc", "_id" : "ns4AAZ8BXEjZhYMmw-8y", "_version" : 7, "result" : "updated", "_shards" : { "total" : 2, "successful" : 2, "failed" : 0 }, "_seq_no" : 11, "_primary_term" : 1 }

For information on how to use the script and about the contents of the returned Hash please see: https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-update

Parameters:

  • id (String)

    The ID of the document to update.

  • doc (Hash, nil) (defaults to: nil)

    The partial document to update the existing document with. If nil is given, the script parameter must be provided. If both doc and script are given, the doc parameter will be ignored by Elasticsearch.

  • script (JayAPI::Elasticsearch::Script, Hash, nil) (defaults to: nil)

    The script to update the existing document with. If nil is given, the doc parameter must be provided. It's recommended to use a JayAPI::Elasticsearch::Script object, but a Hash can also be used.

Returns:

  • (Hash)

    A Hash containing information about the updated document. An example of such Hash is:

Raises:

  • (ArgumentError)

    If both doc and script are nil.

  • (Elasticsearch::Transport::Transport::ServerError)

    If the update fails.



92
93
94
95
96
# File 'lib/jay_api/elasticsearch/index.rb', line 92

def update(id:, doc: nil, script: nil)
  raise ArgumentError, "Either 'doc' or 'script' must be provided" if doc.blank? && script.blank?

  client.update(index: index_name, id:, body: { doc: doc, script: script&.to_h }.compact)
end