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.



19
20
21
# File 'lib/jay_api/elasticsearch/index.rb', line 19

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

#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: 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:



49
50
51
# File 'lib/jay_api/elasticsearch/index.rb', line 49

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.



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

def index_name
  @index_name ||= index_names.first
end