Class: ROM::Elasticsearch::Dataset
- Inherits:
-
Object
- Object
- ROM::Elasticsearch::Dataset
- 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
-
#client ⇒ Hash
readonly
Default body.
- #include_metadata ⇒ Bool readonly
-
#params(new = nil) ⇒ Hash
readonly
Return a new dataset with new params.
- #tuple_proc ⇒ Object readonly
Instance Method Summary collapse
-
#body(new = nil) ⇒ Hash
Return a new dataset with new body.
-
#call ⇒ Dataset
Return a dataset with pre-set client response.
-
#create_index(opts = EMPTY_HASH) ⇒ Hash
Create an index.
-
#delete ⇒ Hash
Delete everything matching configured params and/or body.
-
#delete_index(opts = EMPTY_HASH) ⇒ Hash
Delete an index.
-
#each {|| ... } ⇒ Object
Materialize and iterate over results.
-
#from(num) ⇒ Dataset
Return dataset with :from set.
-
#get(id) ⇒ Dataset
included
from QueryMethods
Return a new dataset configured to search by :id.
-
#index ⇒ Symbol
Return configured index name.
-
#map {|| ... } ⇒ Array
Map dataset tuples.
-
#mappings ⇒ Hash
Return index mappings.
-
#put(data) ⇒ Hash
Put new data under configured index.
-
#query(query) ⇒ Dataset
included
from QueryMethods
Return a new dataset configured to search via :query body option.
-
#query_string(expression) ⇒ Dataset
included
from QueryMethods
Return a new dataset configured to search via :query_string body option.
-
#refresh ⇒ Dataset
Refresh index.
-
#scroll(ttl) ⇒ Dataset
included
from ScrollMethods
Return dataset with :scroll set.
-
#search(options) ⇒ Dataset
included
from QueryMethods
Return a new dataset configured to search via new body options.
-
#settings ⇒ Hash
Return index settings.
-
#size(num) ⇒ Dataset
Return dataset with :size set.
-
#sort(*fields) ⇒ Dataset
Return dataset with :sort set.
-
#to_a ⇒ Array<Hash>
Materialize the dataset.
-
#type ⇒ Symbol
Return configured type from params.
Instance Attribute Details
#client ⇒ Hash (readonly)
Returns default body.
38 |
# File 'lib/rom/elasticsearch/dataset.rb', line 38 param :client |
#include_metadata ⇒ Bool (readonly)
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
42 |
# File 'lib/rom/elasticsearch/dataset.rb', line 42 option :params, default: -> { EMPTY_HASH } |
#tuple_proc ⇒ Object (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
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 |
#call ⇒ Dataset
Return a dataset with pre-set client response
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
249 250 251 |
# File 'lib/rom/elasticsearch/dataset.rb', line 249 def create_index(opts = EMPTY_HASH) client.indices.create(params.merge(opts)) end |
#delete ⇒ Hash
Delete everything matching configured params and/or body
If body is empty it will delete everything*
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
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
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, ) end |
#from(num) ⇒ Dataset
Return dataset with :from set
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
#index ⇒ Symbol
Return configured index name
167 168 169 |
# File 'lib/rom/elasticsearch/dataset.rb', line 167 def index params[:index] end |
#map {|| ... } ⇒ Array
Map dataset tuples
149 150 151 |
# File 'lib/rom/elasticsearch/dataset.rb', line 149 def map(&block) to_a.map(&block) end |
#mappings ⇒ Hash
Return index mappings
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
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
#query_string(expression) ⇒ Dataset Originally defined in module QueryMethods
Return a new dataset configured to search via :query_string body option
#refresh ⇒ Dataset
Refresh index
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
#search(options) ⇒ Dataset Originally defined in module QueryMethods
Return a new dataset configured to search via new body options
#settings ⇒ Hash
Return index settings
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
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
216 217 218 |
# File 'lib/rom/elasticsearch/dataset.rb', line 216 def sort(*fields) params(sort: fields.join(SORT_VALUES_SEPARATOR)) end |
#to_a ⇒ Array<Hash>
Materialize the dataset
124 125 126 |
# File 'lib/rom/elasticsearch/dataset.rb', line 124 def to_a to_enum.to_a end |
#type ⇒ Symbol
Return configured type from params
158 159 160 |
# File 'lib/rom/elasticsearch/dataset.rb', line 158 def type params[:type] end |