Class: ROM::Elasticsearch::Relation

Inherits:
Relation
  • Object
show all
Includes:
ROM::Elasticsearch
Defined in:
lib/rom/elasticsearch/relation.rb,
lib/rom/elasticsearch/relation/loaded.rb

Overview

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.

Examples:

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

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

Defined Under Namespace

Classes: Loaded

Constant Summary

Constants included from ROM::Elasticsearch

VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_pageInteger (readonly)

Returns Currently set page.

Returns:

  • (Integer)

    Currently set page



125
# File 'lib/rom/elasticsearch/relation.rb', line 125

option :current_page, default: -> { 1 }

#per_page(num = Undefined) ⇒ Relation (readonly)

Return a relation with per-page number set

Parameters:

  • num (Integer) (defaults to: Undefined)

Returns:



129
# File 'lib/rom/elasticsearch/relation.rb', line 129

option :per_page, reader: false, optional: true, default: -> { 10 }

Class Method Details

.index_settingsHash .index_settings(settings) ⇒ Object

Manage the index_settings

This is set by default to:

“‘ ruby { number_of_shards: 1,

index: {
  analysis: {
    analyzer: {
      standard_stopwords: {
        type: "standard",
        stopwords: "_english_"
      }
    }
  }

} }.freeze “‘

Overloads:



95
# File 'lib/rom/elasticsearch/relation.rb', line 95

defines :index_settings

.multi_index_typesArray<Symbol> .multi_index_types(types) ⇒ Array<Symbol>

Manage index types for multi-index search

Overloads:

  • .multi_index_typesArray<Symbol>

    Returns a list of index types.

    Returns:

    • (Array<Symbol>)

      a list of index types

  • .multi_index_types(types) ⇒ Array<Symbol>

    Returns a list of index types.

    Examples:

    class Search < ROM::Relation[:elasticsearch]
      multi_index_types :pages, :posts
    
      schema(multi: true) do
        # define your schema
      end
    end

    Returns:

    • (Array<Symbol>)

      a list of index types



114
# File 'lib/rom/elasticsearch/relation.rb', line 114

defines :multi_index_types

.schema(dataset = nil, multi: false, **opts, &block) ⇒ self

Define a schema for the relation

Returns:

  • (self)


149
150
151
152
153
154
155
# 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

Instance Method Details

#callLoaded

Load a relation

Returns:



162
163
164
# File 'lib/rom/elasticsearch/relation.rb', line 162

def call
  Loaded.new(new(dataset.call))
end

#countInteger

Return count of documents in the index

Examples:

users.count
GET /_count
users.search(<query>).count
GET /_count?q=<query>

Returns:

  • (Integer)


339
340
341
342
343
344
# File 'lib/rom/elasticsearch/relation.rb', line 339

def count
  dataset.client.count(
    index: dataset.index,
    body: dataset.body
  )["count"]
end

#create_indexHash

Create relation’s index in ES

Returns:

  • (Hash)

    raw response from the client



292
293
294
# File 'lib/rom/elasticsearch/relation.rb', line 292

def create_index
  dataset.create_index(index_params)
end

#deleteHash

Delete all indexed data from the current relation

Returns:

  • (Hash)

    raw response from the client



310
311
312
# File 'lib/rom/elasticsearch/relation.rb', line 310

def delete
  dataset.delete
end

#delete_indexHash

Delete relation’s index in ES

Returns:

  • (Hash)

    raw response from the client



301
302
303
# File 'lib/rom/elasticsearch/relation.rb', line 301

def delete_index
  dataset.delete_index
end

#from(value) ⇒ Integer

Restrict relation data by offset

Examples:

users.search.from(100)
GET /_search?from=100

Returns:

  • (Integer)


355
356
357
# File 'lib/rom/elasticsearch/relation.rb', line 355

def from(value)
  new(dataset.from(value))
end

#get(id) ⇒ Relation

Restrict indexed data by id

Returns:



241
242
243
# File 'lib/rom/elasticsearch/relation.rb', line 241

def get(id)
  new(dataset.get(id))
end

#map {|| ... } ⇒ Array<Hash,ROM::Struct>

Map indexed data

Yield Parameters:

  • (Hash, ROM::Struct)

Returns:

  • (Array<Hash,ROM::Struct>)


221
222
223
# File 'lib/rom/elasticsearch/relation.rb', line 221

def map(&block)
  to_a.map(&block)
end

#order(*attrs) ⇒ Relation

Return a relation with changed sorting logic

Parameters:

Returns:



173
174
175
# File 'lib/rom/elasticsearch/relation.rb', line 173

def order(*attrs)
  new(dataset.sort(*schema.project(*attrs).map(&:to_sort_expr)))
end

#page(num) ⇒ Relation

Return a relation with page number set

Parameters:

  • num (Integer)

Returns:



184
185
186
# File 'lib/rom/elasticsearch/relation.rb', line 184

def page(num)
  new(dataset.from((num - 1) * per_page), current_page: num)
end

#pluck(name) ⇒ Array

Pluck specific attribute values

Parameters:

  • name (Symbol)

    The name of the attribute

Returns:

  • (Array)


232
233
234
# File 'lib/rom/elasticsearch/relation.rb', line 232

def pluck(name)
  map { |t| t[name] }
end

#query(query) ⇒ Relation

Restrict relation data by a query search

Examples:

users.query(match: { name: "Jane" })

Parameters:

  • query (Hash)

    Query options compatible with Elasticsearch::Client API

Returns:



269
270
271
# File 'lib/rom/elasticsearch/relation.rb', line 269

def query(query)
  new(dataset.query(query))
end

#query_string(expr) ⇒ Relation

Restrict relation data by a string-based query

Examples:

users.query_string("name:'Jane'")

Parameters:

  • query (Hash)

    Query string compatible with Elasticsearch::Client API

Returns:



283
284
285
# File 'lib/rom/elasticsearch/relation.rb', line 283

def query_string(expr)
  new(dataset.query_string(expr))
end

#refreshRelation

Refresh indexed data

Examples:

users.command(:create).call(id: 1, name: "Jane").refresh.to_a

Returns:



322
323
324
# File 'lib/rom/elasticsearch/relation.rb', line 322

def refresh
  new(dataset.refresh)
end

#scroll(ttl) ⇒ Relation

Return a relation with scroll set

Parameters:

  • ttl (String)

Returns:



210
211
212
# File 'lib/rom/elasticsearch/relation.rb', line 210

def scroll(ttl)
  new(dataset.scroll(ttl))
end

#search(options) ⇒ Relation

Restrict relation data by a search query

Examples:

users.search(query: { match: { name: "Jane" } })

Parameters:

  • options (Hash)

    Search options compatible with Elasticsearch::Client API

Returns:



255
256
257
# File 'lib/rom/elasticsearch/relation.rb', line 255

def search(options)
  new(dataset.search(options))
end

#size(value) ⇒ Integer

Restrict relation data by limit the count of documents

Examples:

users.search.size(100)
GET /_search?size=100

Returns:

  • (Integer)


368
369
370
# File 'lib/rom/elasticsearch/relation.rb', line 368

def size(value)
  new(dataset.size(value))
end