Class: ROM::Elasticsearch::Dataset

Inherits:
Object
  • Object
show all
Extended by:
Initializer
Includes:
QueryMethods, ScrollMethods
Defined in:
lib/rom/elasticsearch/dataset.rb

Overview

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.

Constant Summary collapse

SORT_VALUES_SEPARATOR =

Sort values separator

","
ALL =

Default query options

{query: {match_all: EMPTY_HASH}}.freeze
SOURCE_KEY =

The source key in raw results

"_source"
TUPLE_PROC =

default tuple proc which extracts raw source data from response item

-> t { t[SOURCE_KEY] }
TUPLE_PROC_WITH_METADATA =

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

-> t { TUPLE_PROC[t].merge(_metadata: t) }

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ScrollMethods

#scroll, #scroll_enumerator

Methods included from QueryMethods

#get, #query, #query_string, #search

Constructor Details

#initialize(*args, **kwargs) ⇒ Dataset

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Dataset.



68
69
70
71
# 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

Instance Attribute Details

#clientHash (readonly)

Returns default body.

Returns:

  • (Hash)

    default body



38
# File 'lib/rom/elasticsearch/dataset.rb', line 38

param :client

#include_metadataBool (readonly)

Returns:

  • (Bool)


50
# File 'lib/rom/elasticsearch/dataset.rb', line 50

option :include_metadata, default: -> { false }

#params(new = nil) ⇒ Hash (readonly)

Return a new dataset with new params

Parameters:

  • new (Hash) (defaults to: nil)

    New params data

Returns:

  • (Hash)


42
# File 'lib/rom/elasticsearch/dataset.rb', line 42

option :params, default: -> { EMPTY_HASH }

#tuple_procObject (readonly)



58
59
60
# File 'lib/rom/elasticsearch/dataset.rb', line 58

def tuple_proc
  @tuple_proc
end

Instance Method Details

#body(new = nil) ⇒ Hash

Return a new dataset with new body

Parameters:

  • new (Hash) (defaults to: nil)

    New body data

Returns:

  • (Hash)


179
180
181
182
183
184
185
# File 'lib/rom/elasticsearch/dataset.rb', line 179

def body(new = nil)
  if new.nil?
    @body
  else
    with(body: body.merge(new))
  end
end

#callDataset

Return a dataset with pre-set client response

Returns:



270
271
272
# File 'lib/rom/elasticsearch/dataset.rb', line 270

def call
  with(response: response)
end

#create_index(opts = EMPTY_HASH) ⇒ Hash

Create an index

Parameters:

  • opts (Hash) (defaults to: EMPTY_HASH)

    ES options

Returns:

  • (Hash)


250
251
252
# File 'lib/rom/elasticsearch/dataset.rb', line 250

def create_index(opts = EMPTY_HASH)
  client.indices.create(params.merge(opts))
end

#deleteHash

Delete everything matching configured params and/or body

If body is empty it *will delete everything**

Returns:

  • (Hash)

    raw response hash from the client



109
110
111
112
113
114
115
116
117
# 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) ⇒ Hash

Delete an index

Parameters:

  • opts (Hash) (defaults to: EMPTY_HASH)

    ES options

Returns:

  • (Hash)


261
262
263
# File 'lib/rom/elasticsearch/dataset.rb', line 261

def delete_index(opts = EMPTY_HASH)
  client.indices.delete(params.merge(opts))
end

#each {|| ... } ⇒ Object

Materialize and iterate over results

Yield Parameters:

  • (Hash)

Raises:

  • (SearchError)

    in case of the client raising an exception



135
136
137
138
139
140
141
# 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) ⇒ Dataset

Return dataset with :from set

Parameters:

  • num (Integer)

Returns:



228
229
230
# File 'lib/rom/elasticsearch/dataset.rb', line 228

def from(num)
  params(from: num)
end

#indexSymbol

Return configured index name

Returns:

  • (Symbol)


168
169
170
# File 'lib/rom/elasticsearch/dataset.rb', line 168

def index
  params[:index]
end

#map {|| ... } ⇒ Array

Map dataset tuples

Yield Parameters:

  • (Hash)

Returns:

  • (Array)


150
151
152
# File 'lib/rom/elasticsearch/dataset.rb', line 150

def map(&block)
  to_a.map(&block)
end

#mappingsHash

Return index mappings

Returns:

  • (Hash)


98
99
100
# File 'lib/rom/elasticsearch/dataset.rb', line 98

def mappings
  client.indices.get_mapping[index.to_s]["mappings"]
end

#put(data) ⇒ Hash

Put new data under configured index

Parameters:

  • data (Hash)

Returns:

  • (Hash)


80
81
82
# File 'lib/rom/elasticsearch/dataset.rb', line 80

def put(data)
  client.index(**params, body: data)
end

#refreshDataset

Refresh index

Returns:



207
208
209
210
# File 'lib/rom/elasticsearch/dataset.rb', line 207

def refresh
  client.indices.refresh(index: index)
  self
end

#settingsHash

Return index settings

Returns:

  • (Hash)


89
90
91
# File 'lib/rom/elasticsearch/dataset.rb', line 89

def settings
  client.indices.get_settings[index.to_s]["settings"]["index"]
end

#size(num) ⇒ Dataset

Return dataset with :size set

Parameters:

  • num (Integer)

Returns:



239
240
241
# File 'lib/rom/elasticsearch/dataset.rb', line 239

def size(num)
  params(size: num)
end

#sort(*fields) ⇒ Dataset

Return dataset with :sort set

Returns:



217
218
219
# File 'lib/rom/elasticsearch/dataset.rb', line 217

def sort(*fields)
  params(sort: fields.join(SORT_VALUES_SEPARATOR))
end

#to_aArray<Hash>

Materialize the dataset

Returns:

  • (Array<Hash>)


124
125
126
# File 'lib/rom/elasticsearch/dataset.rb', line 124

def to_a
  to_enum.to_a
end

#typeSymbol

Return configured type from params

Returns:

  • (Symbol)


159
160
161
# File 'lib/rom/elasticsearch/dataset.rb', line 159

def type
  params[:type]
end