class ROM::Elasticsearch::Dataset

Elasticsearch dataset

Uses an elasticsearch client object provided by the gateway, holds basic params with information about index name and type, and optional body for additional queries.

Dataset object also provide meta information about indices, like custom settings and mappings.

@api public

Constants

ALL

Default query options

SORT_VALUES_SEPARATOR

Sort values separator

SOURCE_KEY

The source key in raw results

TUPLE_PROC

default tuple proc which extracts raw source data from response item

TUPLE_PROC_WITH_METADATA

tuple proc used when :include_metadata is enabled, resulting tuples will include raw response hash under _metadata key

Attributes

tuple_proc[R]

@!attribute [r] tuple_proc

@return [Proc] low-level tuple processing function used in #each

Public Class Methods

new(*args, **kwargs) click to toggle source

@api private

Calls superclass method
# File lib/rom/elasticsearch/dataset.rb, line 68
def initialize(*args, **kwargs)
  super
  @tuple_proc = options[:include_metadata] ? TUPLE_PROC_WITH_METADATA : TUPLE_PROC
end

Public Instance Methods

body(new = nil) click to toggle source

Return a new dataset with new body

@param [Hash] new New body data

@return [Hash]

@api public

# File lib/rom/elasticsearch/dataset.rb, line 179
def body(new = nil)
  if new.nil?
    @body
  else
    with(body: body.merge(new))
  end
end
call() click to toggle source

Return a dataset with pre-set client response

@return [Dataset]

@api public

# File lib/rom/elasticsearch/dataset.rb, line 270
def call
  with(response: response)
end
create_index(opts = EMPTY_HASH) click to toggle source

Create an index

@param [Hash] opts ES options

@api public

@return [Hash]

# File lib/rom/elasticsearch/dataset.rb, line 250
def create_index(opts = EMPTY_HASH)
  client.indices.create(params.merge(opts))
end
delete() click to toggle source

Delete everything matching configured params and/or body

If body is empty it *will delete everything**

@return [Hash] raw response hash from the client

@api public

# File lib/rom/elasticsearch/dataset.rb, line 109
def delete
  if body.empty? && params[:id]
    client.delete(params)
  elsif body.empty?
    client.delete_by_query(params.merge(body: body.merge(ALL)))
  else
    client.delete_by_query(params.merge(body: body))
  end
end
delete_index(opts = EMPTY_HASH) click to toggle source

Delete an index

@param [Hash] opts ES options

@api public

@return [Hash]

# File lib/rom/elasticsearch/dataset.rb, line 261
def delete_index(opts = EMPTY_HASH)
  client.indices.delete(params.merge(opts))
end
each() { |tuple_proc| ... } click to toggle source

Materialize and iterate over results

@yieldparam [Hash]

@raise [SearchError] in case of the client raising an exception

@api public

# File lib/rom/elasticsearch/dataset.rb, line 135
def each
  return to_enum unless block_given?

  view.each { |result| yield(tuple_proc[result]) }
rescue ::Elasticsearch::Transport::Transport::Error => e
  raise SearchError.new(e, options)
end
from(num) click to toggle source

Return dataset with :from set

@param [Integer] num

@return [Dataset]

@api public

# File lib/rom/elasticsearch/dataset.rb, line 228
def from(num)
  params(from: num)
end
index() click to toggle source

Return configured index name

@return [Symbol]

@api public

# File lib/rom/elasticsearch/dataset.rb, line 168
def index
  params[:index]
end
map(&block) click to toggle source

Map dataset tuples

@yieldparam [Hash]

@return [Array]

@api public

# File lib/rom/elasticsearch/dataset.rb, line 150
def map(&block)
  to_a.map(&block)
end
mappings() click to toggle source

Return index mappings

@return [Hash]

@api public

# File lib/rom/elasticsearch/dataset.rb, line 98
def mappings
  client.indices.get_mapping[index.to_s]["mappings"]
end
params(new = nil) click to toggle source

Return a new dataset with new params

@param [Hash] new New params data

@return [Hash]

@api public

# File lib/rom/elasticsearch/dataset.rb, line 194
def params(new = nil)
  if new.nil?
    @params
  else
    with(params: params.merge(new))
  end
end
put(data) click to toggle source

Put new data under configured index

@param [Hash] data

@return [Hash]

@api public

# File lib/rom/elasticsearch/dataset.rb, line 80
def put(data)
  client.index(**params, body: data)
end
refresh() click to toggle source

Refresh index

@return [Dataset]

@api public

# File lib/rom/elasticsearch/dataset.rb, line 207
def refresh
  client.indices.refresh(index: index)
  self
end
settings() click to toggle source

Return index settings

@return [Hash]

@api public

# File lib/rom/elasticsearch/dataset.rb, line 89
def settings
  client.indices.get_settings[index.to_s]["settings"]["index"]
end
size(num) click to toggle source

Return dataset with :size set

@param [Integer] num

@return [Dataset]

@api public

# File lib/rom/elasticsearch/dataset.rb, line 239
def size(num)
  params(size: num)
end
sort(*fields) click to toggle source

Return dataset with :sort set

@return [Dataset]

@api public

# File lib/rom/elasticsearch/dataset.rb, line 217
def sort(*fields)
  params(sort: fields.join(SORT_VALUES_SEPARATOR))
end
to_a() click to toggle source

Materialize the dataset

@return [Array<Hash>]

@api public

# File lib/rom/elasticsearch/dataset.rb, line 124
def to_a
  to_enum.to_a
end
type() click to toggle source

Return configured type from params

@return [Symbol]

@api public

# File lib/rom/elasticsearch/dataset.rb, line 159
def type
  params[:type]
end

Private Instance Methods

response() click to toggle source

@api private

# File lib/rom/elasticsearch/dataset.rb, line 292
def response
  options[:response] || client.search(**params, body: body)
end
view() click to toggle source

Return results of a query based on configured params and body

@return [Array<Hash>]

@api private

# File lib/rom/elasticsearch/dataset.rb, line 281
def view
  if params[:id]
    [client.get(params)]
  elsif params[:scroll]
    scroll_enumerator(client, response)
  else
    response.fetch("hits").fetch("hits")
  end
end