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
","
- 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
-
#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.
-
#index ⇒ Symbol
Return configured index name.
-
#initialize(*args, **kwargs) ⇒ Dataset
constructor
private
A new instance of Dataset.
-
#map {|| ... } ⇒ Array
Map dataset tuples.
-
#mappings ⇒ Hash
Return index mappings.
-
#put(data) ⇒ Hash
Put new data under configured index.
-
#refresh ⇒ Dataset
Refresh index.
-
#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.
Methods included from ScrollMethods
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 = [:include_metadata] ? TUPLE_PROC_WITH_METADATA : TUPLE_PROC end |
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
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 |
#call ⇒ Dataset
Return a dataset with pre-set client response
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
250 251 252 |
# File 'lib/rom/elasticsearch/dataset.rb', line 250 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
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
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, ) end |
#from(num) ⇒ Dataset
Return dataset with :from set
228 229 230 |
# File 'lib/rom/elasticsearch/dataset.rb', line 228 def from(num) params(from: num) end |
#index ⇒ Symbol
Return configured index name
168 169 170 |
# File 'lib/rom/elasticsearch/dataset.rb', line 168 def index params[:index] end |
#map {|| ... } ⇒ Array
Map dataset tuples
150 151 152 |
# File 'lib/rom/elasticsearch/dataset.rb', line 150 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"] 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 |
#refresh ⇒ Dataset
Refresh index
207 208 209 210 |
# File 'lib/rom/elasticsearch/dataset.rb', line 207 def refresh client.indices.refresh(index: index) self end |
#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
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
217 218 219 |
# File 'lib/rom/elasticsearch/dataset.rb', line 217 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
159 160 161 |
# File 'lib/rom/elasticsearch/dataset.rb', line 159 def type params[:type] end |