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

','.freeze
ALL =

Default query options

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

The source key in raw results

'_source'.freeze
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

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)


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

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:



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

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)


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

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)


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

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



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

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

#get(id) ⇒ Dataset Originally defined in module QueryMethods

Return a new dataset configured to search by :id

Parameters:

  • id (Integer)

Returns:

See Also:

#indexSymbol

Return configured index name

Returns:

  • (Symbol)


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

def index
  params[:index]
end

#map {|| ... } ⇒ Array

Map dataset tuples

Yield Parameters:

  • (Hash)

Returns:

  • (Array)


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

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'][type.to_s]
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

#query(query) ⇒ Dataset Originally defined in module QueryMethods

Return a new dataset configured to search via :query body option

Parameters:

  • query (Hash)

    A query hash

Returns:

See Also:

#query_string(expression) ⇒ Dataset Originally defined in module QueryMethods

Return a new dataset configured to search via :query_string body option

Parameters:

  • expression (String)

    A string query

Returns:

See Also:

#refreshDataset

Refresh index

Returns:



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

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

#scroll(ttl) ⇒ Dataset Originally defined in module ScrollMethods

Return dataset with :scroll set

Parameters:

  • ttl (String)

Returns:

#search(options) ⇒ Dataset Originally defined in module QueryMethods

Return a new dataset configured to search via new body options

Parameters:

  • options (Hash)

    Body options

Returns:

See Also:

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



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

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

#sort(*fields) ⇒ Dataset

Return dataset with :sort set

Returns:



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

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)


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

def type
  params[:type]
end