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:

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

schema(dataset = nil, multi: false, **opts, &block) click to toggle source

Define a schema for the relation

@return [self]

Calls superclass method
# 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

call() click to toggle source

Load a relation

@return [Loaded]

@api public

# File lib/rom/elasticsearch/relation.rb, line 162
def call
  Loaded.new(new(dataset.call))
end
count() click to toggle source

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_index() click to toggle source

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() click to toggle source

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_index() click to toggle source

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
from(value) click to toggle source

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
get(id) click to toggle source

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(&block) click to toggle source

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
order(*attrs) click to toggle source

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
page(num) click to toggle source

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
per_page(num = Undefined) click to toggle source

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(name) click to toggle source

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
query(query) click to toggle source

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
query_string(expr) click to toggle source

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() click to toggle source

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
scroll(ttl) click to toggle source

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
size(value) click to toggle source

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

index_params() click to toggle source

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