class ROM::Elasticsearch::Relation
Elasticsearch
relation API
Provides access to indexed data, and methods for managing indices. Works like a standard Relation
, which means it's lazy and composable, and has access to commands via `Relation#command`.
Indices are configured based on two settings:
-
`Relation#name.dataset` - which is configured in a standard way via `schema` block
-
`Relation.index_settings` - which is a class-level setting
Optionally, query DSL can be enabled via `:query_dsl` plugin.
@example setting up a relation
class Pages < ROM::Relation[:elasticsearch] schema do attribute :id, Types::ID attribute :title, Types.Keyword attribute :body, Types.Text(analyzer: "snowball") end end
@example using query DSL
class Users < ROM::Relation[:elasticsearch] use :query_dsl schema do # ... end end users.search do query do match name: "Jane" end end
@api public
Public Class Methods
Define a schema for the relation
@return [self]
# File lib/rom/elasticsearch/relation.rb, line 149 def self.schema(dataset = nil, multi: false, **opts, &block) if multi super(IndexName[:_all], **opts, &block) else super(dataset, **opts, &block) end end
Public Instance Methods
Load a relation
@return [Loaded]
@api public
# File lib/rom/elasticsearch/relation.rb, line 162 def call Loaded.new(new(dataset.call)) end
Return count of documents in the index
@example
users.count GET /_count
@example
users.search(<query>).count GET /_count?q=<query>
@return [Integer]
@api public
# File lib/rom/elasticsearch/relation.rb, line 339 def count dataset.client.count( index: dataset.index, body: dataset.body )["count"] end
Create relation's index in ES
@return [Hash] raw response from the client
@api public
# File lib/rom/elasticsearch/relation.rb, line 292 def create_index dataset.create_index(index_params) end
Delete all indexed data from the current relation
@return [Hash] raw response from the client
@api public
# File lib/rom/elasticsearch/relation.rb, line 310 def delete dataset.delete end
Delete relation's index in ES
@return [Hash] raw response from the client
@api public
# File lib/rom/elasticsearch/relation.rb, line 301 def delete_index dataset.delete_index end
Restrict relation data by offset
@example
users.search.from(100) GET /_search?from=100
@return [Integer]
@api public
# File lib/rom/elasticsearch/relation.rb, line 355 def from(value) new(dataset.from(value)) end
Restrict indexed data by id
@return [Relation]
@api public
# File lib/rom/elasticsearch/relation.rb, line 241 def get(id) new(dataset.get(id)) end
Map indexed data
@yieldparam [Hash,ROM::Struct]
@return [Array<Hash,ROM::Struct>]
@api public
# File lib/rom/elasticsearch/relation.rb, line 221 def map(&block) to_a.map(&block) end
Return a relation with changed sorting logic
@param [Array<Symbol,Attribute>] attrs
@return [Relation]
@api public
# File lib/rom/elasticsearch/relation.rb, line 173 def order(*attrs) new(dataset.sort(*schema.project(*attrs).map(&:to_sort_expr))) end
Return a relation with page number set
@param [Integer] num
@return [Relation]
@api public
# File lib/rom/elasticsearch/relation.rb, line 184 def page(num) new(dataset.from((num - 1) * per_page), current_page: num) end
Return a relation with per-page number set
@param [Integer] num
@return [Relation]
@api public
# File lib/rom/elasticsearch/relation.rb, line 195 def per_page(num = Undefined) if num.equal?(Undefined) options[:per_page] else new(dataset.size(num), per_page: num) end end
Pluck specific attribute values
@param [Symbol] name The name of the attribute
@return [Array]
@api public
# File lib/rom/elasticsearch/relation.rb, line 232 def pluck(name) map { |t| t[name] } end
Restrict relation data by a query search
@example
users.query(match: { name: "Jane" })
@param [Hash] query Query options compatible with Elasticsearch::Client API
@return [Relation]
@api public
# File lib/rom/elasticsearch/relation.rb, line 269 def query(query) new(dataset.query(query)) end
Restrict relation data by a string-based query
@example
users.query_string("name:'Jane'")
@param [Hash] query Query string compatible with Elasticsearch::Client API
@return [Relation]
@api public
# File lib/rom/elasticsearch/relation.rb, line 283 def query_string(expr) new(dataset.query_string(expr)) end
Refresh indexed data
@example
users.command(:create).call(id: 1, name: "Jane").refresh.to_a
@return [Relation]
@api public
# File lib/rom/elasticsearch/relation.rb, line 322 def refresh new(dataset.refresh) end
Return a relation with scroll set
@param [String] ttl
@return [Relation]
@api public
# File lib/rom/elasticsearch/relation.rb, line 210 def scroll(ttl) new(dataset.scroll(ttl)) end
Restrict relation data by a search query
@example
users.search(query: { match: { name: "Jane" } })
@param [Hash] options Search options compatible with Elasticsearch::Client API
@return [Relation]
@api public
# File lib/rom/elasticsearch/relation.rb, line 255 def search(options) new(dataset.search(options)) end
Restrict relation data by limit the count of documents
@example
users.search.size(100) GET /_search?size=100
@return [Integer]
@api public
# File lib/rom/elasticsearch/relation.rb, line 368 def size(value) new(dataset.size(value)) end
Private Instance Methods
Dataset
index params based on relation configuration
@return [Hash]
@api private
# File lib/rom/elasticsearch/relation.rb, line 379 def index_params {index: name.dataset.to_sym, body: { settings: self.class.index_settings, mappings: {properties: schema.to_properties} }} end