Class: ROM::Elasticsearch::Relation
- Inherits:
-
Relation
- Object
- Relation
- ROM::Elasticsearch::Relation
- 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 viaschema
blockRelation.index_settings
- which is a class-level setting
Optionally, query DSL can be enabled via :query_dsl
plugin.
Defined Under Namespace
Classes: Loaded
Instance Attribute Summary collapse
-
#current_page ⇒ Integer
readonly
Currently set page.
-
#per_page(num = Undefined) ⇒ Relation
readonly
Return a relation with per-page number set.
Class Method Summary collapse
-
.index_settings ⇒ Object
Manage the index_settings.
-
.multi_index_types ⇒ Object
Manage index types for multi-index search.
-
.schema(dataset = nil, multi: false, **opts, &block) ⇒ self
Define a schema for the relation.
Instance Method Summary collapse
-
#call ⇒ Loaded
Load a relation.
-
#count ⇒ Integer
Return count of documents in the index.
-
#create_index ⇒ Hash
Create relation's index in ES.
-
#delete ⇒ Hash
Delete all indexed data from the current relation.
-
#delete_index ⇒ Hash
Delete relation's index in ES.
-
#from(value) ⇒ Integer
Restrict relation data by offset.
-
#get(id) ⇒ Relation
Restrict indexed data by id.
-
#map {|| ... } ⇒ Array<Hash,ROM::Struct>
Map indexed data.
-
#order(*attrs) ⇒ Relation
Return a relation with changed sorting logic.
-
#page(num) ⇒ Relation
Return a relation with page number set.
-
#pluck(name) ⇒ Array
Pluck specific attribute values.
-
#query(query) ⇒ Relation
Restrict relation data by a query search.
-
#query_string(expr) ⇒ Relation
Restrict relation data by a string-based query.
-
#refresh ⇒ Relation
Refresh indexed data.
-
#scroll(ttl) ⇒ Relation
Return a relation with scroll set.
-
#search(options) ⇒ Relation
Restrict relation data by a search query.
-
#size(value) ⇒ Integer
Restrict relation data by limit the count of documents.
Instance Attribute Details
#current_page ⇒ Integer (readonly)
Returns 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
129 |
# File 'lib/rom/elasticsearch/relation.rb', line 129 option :per_page, reader: false, optional: true, default: -> { 10 } |
Class Method Details
.index_settings ⇒ Hash .index_settings(settings) ⇒ Object
Manage the index_settings
This is set by default to:
{ number_of_shards: 1,
index: {
analysis: {
analyzer: {
standard_stopwords: {
type: "standard",
stopwords: "_english_"
}
}
}
} }.freeze
95 |
# File 'lib/rom/elasticsearch/relation.rb', line 95 defines :index_settings |
.multi_index_types ⇒ Array<Symbol> .multi_index_types(types) ⇒ Array<Symbol>
Manage index types for multi-index search
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
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, multi_index_types], **opts, &block) else super(dataset, **opts, &block) end end |
Instance Method Details
#call ⇒ Loaded
Load a relation
162 163 164 |
# File 'lib/rom/elasticsearch/relation.rb', line 162 def call Loaded.new(new(dataset.call)) end |
#count ⇒ Integer
Return count of documents in the index
339 340 341 342 343 344 345 |
# File 'lib/rom/elasticsearch/relation.rb', line 339 def count dataset.client.count( index: dataset.index, type: dataset.type, body: dataset.body )['count'] end |
#create_index ⇒ Hash
Create relation's index in ES
292 293 294 |
# File 'lib/rom/elasticsearch/relation.rb', line 292 def create_index dataset.create_index(index_params) end |
#delete ⇒ Hash
Delete all indexed data from the current relation
310 311 312 |
# File 'lib/rom/elasticsearch/relation.rb', line 310 def delete dataset.delete end |
#delete_index ⇒ Hash
Delete relation's index in ES
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
356 357 358 |
# File 'lib/rom/elasticsearch/relation.rb', line 356 def from(value) new(dataset.from(value)) end |
#get(id) ⇒ Relation
Restrict indexed data by id
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
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
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
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
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
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
283 284 285 |
# File 'lib/rom/elasticsearch/relation.rb', line 283 def query_string(expr) new(dataset.query_string(expr)) end |
#refresh ⇒ Relation
Refresh indexed data
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
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
255 256 257 |
# File 'lib/rom/elasticsearch/relation.rb', line 255 def search() new(dataset.search()) end |
#size(value) ⇒ Integer
Restrict relation data by limit the count of documents
369 370 371 |
# File 'lib/rom/elasticsearch/relation.rb', line 369 def size(value) new(dataset.size(value)) end |