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
@!attribute [r] tuple_proc
@return [Proc] low-level tuple processing function used in #each
Public Class Methods
@api private
# 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
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
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 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 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 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
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
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
Return configured index name
@return [Symbol]
@api public
# File lib/rom/elasticsearch/dataset.rb, line 168 def index params[:index] end
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
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
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 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 index
@return [Dataset]
@api public
# File lib/rom/elasticsearch/dataset.rb, line 207 def refresh client.indices.refresh(index: index) self end
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
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
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
Materialize the dataset
@return [Array<Hash>]
@api public
# File lib/rom/elasticsearch/dataset.rb, line 124 def to_a to_enum.to_a end
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
@api private
# File lib/rom/elasticsearch/dataset.rb, line 292 def response options[:response] || client.search(**params, body: body) end
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