Module: SearchFlip::Index::ClassMethods
- Extended by:
- Forwardable
- Defined in:
- lib/search_flip/index.rb
Instance Method Summary collapse
-
#analyze(request, params = {}) ⇒ Hash
Sends an analyze request to Elasticsearch.
-
#bulk(options = {}) ⇒ Object
Initiates and yields a bulk object, such that index, import, create, update and delete requests can be appended to the bulk request.
-
#close_index ⇒ Boolean
Closes the index within Elasticsearch.
-
#connection ⇒ SearchFlip::Connection
Returns the SearchFlip::Connection for the index.
-
#create(scope, options = {}, additional_index_options = {}) ⇒ Object
Indexes the given record set, array of records or individual record using Elasticsearch’s create operation via the Bulk API, such that the request will fail if a record with a particular primary key already exists in Elasticsearch.
-
#create_index ⇒ Boolean
Creates the index within Elasticsearch and applies index settings, if specified.
-
#criteria ⇒ SearchFlip::Criteria
private
Creates an SearchFlip::Criteria for the current index, which is used as a base for chaining criteria methods.
-
#delete(scope, options = {}, additional_index_options = {}) ⇒ Object
Deletes the given record set, array of records or individual record from Elasticsearch using the Bulk API.
-
#delete_index ⇒ Object
Deletes the index from Elasticsearch.
-
#each_record(scope, index_scope: false) ⇒ Object
private
Used to iterate a record set.
-
#fetch_records(ids) ⇒ Object
Returns a record set, usually an ActiveRecord::Relation, for the specified ids, ie primary keys.
-
#freeze_index ⇒ Boolean
Freezes the index within Elasticsearch.
-
#get(id, params = {}) ⇒ Hash
Retrieves the document specified by id from Elasticsearch.
-
#get_index_settings ⇒ Hash
Fetches the index settings from Elasticsearch.
-
#get_mapping ⇒ Hash
Retrieves the current type mapping from Elasticsearch.
-
#include_type_name? ⇒ Boolean
Returns whether or not to include a type name.
-
#index(scope, options = {}, additional_index_options = {}) ⇒ Object
Indexes the given record set, array of records or individual record.
-
#index_exists? ⇒ Boolean
Returns whether or not the associated Elasticsearch index already exists.
-
#index_name ⇒ String
Returns the base name of the index within Elasticsearch, ie the index name without prefix.
-
#index_name_with_prefix ⇒ String
private
Returns the full name of the index within Elasticsearch, ie with prefix specified via SearchFlip::Config.
-
#index_options(record) ⇒ Hash
Override this method to automatically pass index options for a record at index-time, like routing or versioning.
-
#index_scope(scope) ⇒ Object
Override this method to specify an index scope, which will automatically be applied to scopes, eg.
-
#index_settings ⇒ Hash
Override to specify index settings like number of shards, analyzers, refresh interval, etc.
-
#index_url ⇒ String
Returns the Elasticsearch index URL, ie base URL and index name with prefix.
-
#mapping ⇒ Object
Specifies a type mapping.
-
#mget(request, params = {}) ⇒ Hash
Retrieves the documents specified by ids from elasticsearch.
-
#open_index ⇒ Boolean
Opens the index within Elasticsearch.
-
#record_id(record) ⇒ String, Fixnum
Returns the record’s id, ie the unique identifier or primary key of a record.
-
#refresh ⇒ Object
Sends a index refresh request to Elasticsearch.
-
#scope(name, &block) ⇒ Object
Adds a named scope to the index.
-
#serialize(record) ⇒ Hash
abstract
Override this method to generate a hash representation of a record, used to generate the JSON representation of it.
-
#type_name ⇒ String
Override to specify the type name used within Elasticsearch.
-
#type_url ⇒ String
Returns the full Elasticsearch type URL, ie base URL, index name with prefix and type name.
-
#unfreeze_index ⇒ Boolean
Unfreezes the index within Elasticsearch.
-
#update(scope, options = {}, additional_index_options = {}) ⇒ Object
Indexes the given record set, array of records or individual record using Elasticsearch’s update operation via the Bulk API, such that the request will fail if a record you want to update does not already exist in Elasticsearch.
-
#update_index_settings ⇒ Boolean
Updates the index settings within Elasticsearch according to the index settings specified.
-
#update_mapping ⇒ Object
Updates the type mapping within Elasticsearch according to the mapping currently specified.
-
#with_settings(index_name: nil, connection: nil) ⇒ Class
Creates an anonymous class inheriting from the current index with a custom index name and/or connection.
Instance Method Details
#analyze(request, params = {}) ⇒ Hash
Sends an analyze request to Elasticsearch. Raises SearchFlip::ResponseError in case any errors occur.
491 492 493 494 495 |
# File 'lib/search_flip/index.rb', line 491 def analyze(request, params = {}) response = connection.http_client.headers(accept: "application/json").post("#{index_url}/_analyze", json: request, params: params) SearchFlip::JSON.parse(response.to_s) end |
#bulk(options = {}) ⇒ Object
Initiates and yields a bulk object, such that index, import, create, update and delete requests can be appended to the bulk request. Sends a refresh request afterwards if auto_refresh is enabled.
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 |
# File 'lib/search_flip/index.rb', line 629 def bulk( = {}) = { http_client: connection.http_client, bulk_limit: connection.bulk_limit, bulk_max_mb: connection.bulk_max_mb } url = connection.distribution.nil? && connection.version.to_i < 8 ? type_url : index_url SearchFlip::Bulk.new("#{url}/_bulk", .merge()) do |indexer| yield indexer end refresh if SearchFlip::Config[:auto_refresh] end |
#close_index ⇒ Boolean
Closes the index within Elasticsearch. Raises SearchFlip::ResponseError in case any errors occur.
343 344 345 |
# File 'lib/search_flip/index.rb', line 343 def close_index connection.close_index(index_name_with_prefix) end |
#connection ⇒ SearchFlip::Connection
Returns the SearchFlip::Connection for the index. Override to specify a custom connection.
668 669 670 671 672 |
# File 'lib/search_flip/index.rb', line 668 def connection ConnectionMutex.synchronize do @connection ||= SearchFlip::Connection.new(base_url: SearchFlip::Config[:base_url]) end end |
#create(scope, options = {}, additional_index_options = {}) ⇒ Object
Indexes the given record set, array of records or individual record using Elasticsearch’s create operation via the Bulk API, such that the request will fail if a record with a particular primary key already exists in Elasticsearch.
560 561 562 563 564 565 566 567 568 |
# File 'lib/search_flip/index.rb', line 560 def create(scope, = {}, = {}) bulk do |indexer| each_record(scope, index_scope: true) do |object| indexer.create record_id(object), serialize(object), (object).merge() end end scope end |
#create_index ⇒ Boolean
Creates the index within Elasticsearch and applies index settings, if specified. Raises SearchFlip::ResponseError in case any errors occur.
334 335 336 |
# File 'lib/search_flip/index.rb', line 334 def create_index connection.create_index(index_name_with_prefix, index_settings) end |
#criteria ⇒ SearchFlip::Criteria
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates an SearchFlip::Criteria for the current index, which is used as a base for chaining criteria methods.
246 247 248 |
# File 'lib/search_flip/index.rb', line 246 def criteria SearchFlip::Criteria.new(target: self) end |
#delete(scope, options = {}, additional_index_options = {}) ⇒ Object
Deletes the given record set, array of records or individual record from Elasticsearch using the Bulk API.
594 595 596 597 598 599 600 601 602 |
# File 'lib/search_flip/index.rb', line 594 def delete(scope, = {}, = {}) bulk do |indexer| each_record(scope) do |object| indexer.delete record_id(object), (object).merge() end end scope end |
#delete_index ⇒ Object
Deletes the index from Elasticsearch. Raises SearchFlip::ResponseError in case any errors occur.
387 388 389 |
# File 'lib/search_flip/index.rb', line 387 def delete_index connection.delete_index(index_name_with_prefix) end |
#each_record(scope, index_scope: false) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Used to iterate a record set. Here, a record set may be a) an ActiveRecord::Relation or anything responding to #find_each, b) an Array of records or anything responding to #each or c) a single record.
155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/search_flip/index.rb', line 155 def each_record(scope, index_scope: false) return enum_for(:each_record, scope, index_scope: index_scope) unless block_given? if scope.respond_to?(:find_each) (index_scope ? self.index_scope(scope) : scope).find_each do |record| yield record end else (scope.respond_to?(:each) && !scope.is_a?(SearchFlip::Result) ? scope : Array(scope)).each do |record| yield record end end end |
#fetch_records(ids) ⇒ Object
Returns a record set, usually an ActiveRecord::Relation, for the specified ids, ie primary keys. Override this method for custom primary keys and/or ORMs.
199 200 201 |
# File 'lib/search_flip/index.rb', line 199 def fetch_records(ids) model.where(id: ids) end |
#freeze_index ⇒ Boolean
Freezes the index within Elasticsearch. Raises SearchFlip::ResponseError in case any errors occur.
361 362 363 |
# File 'lib/search_flip/index.rb', line 361 def freeze_index connection.freeze_index(index_name_with_prefix) end |
#get(id, params = {}) ⇒ Hash
Retrieves the document specified by id from Elasticsearch. Raises SearchFlip::ResponseError specific exceptions in case any errors occur.
457 458 459 460 461 462 |
# File 'lib/search_flip/index.rb', line 457 def get(id, params = {}) url = connection.distribution.nil? && connection.version.to_i < 8 ? type_url : "#{index_url}/_doc" response = connection.http_client.headers(accept: "application/json").get("#{url}/#{id}", params: params) SearchFlip::JSON.parse(response.to_s) end |
#get_index_settings ⇒ Hash
Fetches the index settings from Elasticsearch. Sends a GET request to index_url/_settings. Raises SearchFlip::ResponseError in case any errors occur.
324 325 326 |
# File 'lib/search_flip/index.rb', line 324 def get_index_settings connection.get_index_settings(index_name_with_prefix) end |
#get_mapping ⇒ Hash
Retrieves the current type mapping from Elasticsearch. Raises SearchFlip::ResponseError in case any errors occur.
426 427 428 429 430 431 432 |
# File 'lib/search_flip/index.rb', line 426 def get_mapping if include_type_name? connection.get_mapping(index_name_with_prefix, type_name: type_name) else connection.get_mapping(index_name_with_prefix) end end |
#include_type_name? ⇒ Boolean
Returns whether or not to include a type name. As types are deprecated in Elasticsearch 7, this method controls whether or not to include the type name. By default, the method returns true for Elasticsearch versions before 7 or if the specified type name for the index is not equal to _doc.
440 441 442 |
# File 'lib/search_flip/index.rb', line 440 def include_type_name? type_name != "_doc" || (connection.distribution.nil? && connection.version.to_i < 7) end |
#index(scope, options = {}, additional_index_options = {}) ⇒ Object
Indexes the given record set, array of records or individual record. A record set usually is an ActiveRecord::Relation, but can be any other ORM as well. Uses the Elasticsearch bulk API no matter what is provided. Refreshes the index if auto_refresh is enabled. Raises SearchFlip::ResponseError in case any errors occur.
542 543 544 545 546 547 548 549 550 |
# File 'lib/search_flip/index.rb', line 542 def index(scope, = {}, = {}) bulk do |indexer| each_record(scope, index_scope: true) do |object| indexer.index record_id(object), serialize(object), (object).merge() end end scope end |
#index_exists? ⇒ Boolean
Returns whether or not the associated Elasticsearch index already exists.
314 315 316 |
# File 'lib/search_flip/index.rb', line 314 def index_exists? connection.index_exists?(index_name_with_prefix) end |
#index_name ⇒ String
Returns the base name of the index within Elasticsearch, ie the index name without prefix.
275 276 277 |
# File 'lib/search_flip/index.rb', line 275 def index_name raise SearchFlip::MethodNotImplemented, "You must implement #{name}::index_name" end |
#index_name_with_prefix ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the full name of the index within Elasticsearch, ie with prefix specified via SearchFlip::Config.
286 287 288 |
# File 'lib/search_flip/index.rb', line 286 def index_name_with_prefix "#{SearchFlip::Config[:index_prefix]}#{index_name}" end |
#index_options(record) ⇒ Hash
Override this method to automatically pass index options for a record at index-time, like routing or versioning.
68 69 70 |
# File 'lib/search_flip/index.rb', line 68 def (record) {} end |
#index_scope(scope) ⇒ Object
Override this method to specify an index scope, which will automatically be applied to scopes, eg. ActiveRecord::Relation objects, passed to #import or #index. This can be used to preload associations that are used when serializing records or to restrict the records you want to index.
235 236 237 |
# File 'lib/search_flip/index.rb', line 235 def index_scope(scope) scope end |
#index_settings ⇒ Hash
Override to specify index settings like number of shards, analyzers, refresh interval, etc.
305 306 307 |
# File 'lib/search_flip/index.rb', line 305 def index_settings {} end |
#index_url ⇒ String
Returns the Elasticsearch index URL, ie base URL and index name with prefix.
659 660 661 |
# File 'lib/search_flip/index.rb', line 659 def index_url connection.index_url(index_name_with_prefix) end |
#mapping ⇒ Object
Specifies a type mapping. Override to specify a custom mapping. Please note that you don’t have to include the type name, even for Elasticsearch versions before 7, as SearchFlip automatically adds the type name if neccessary.
405 406 407 |
# File 'lib/search_flip/index.rb', line 405 def mapping {} end |
#mget(request, params = {}) ⇒ Hash
Retrieves the documents specified by ids from elasticsearch.
476 477 478 479 480 481 |
# File 'lib/search_flip/index.rb', line 476 def mget(request, params = {}) url = connection.distribution.nil? && connection.version.to_i < 8 ? type_url : index_url response = connection.http_client.headers(accept: "application/json").post("#{url}/_mget", json: request, params: params) SearchFlip::JSON.parse(response.to_s) end |
#open_index ⇒ Boolean
Opens the index within Elasticsearch. Raises SearchFlip::ResponseError in case any errors occur.
352 353 354 |
# File 'lib/search_flip/index.rb', line 352 def open_index connection.open_index(index_name_with_prefix) end |
#record_id(record) ⇒ String, Fixnum
Returns the record’s id, ie the unique identifier or primary key of a record. Override this method for custom primary keys, but return a String or Fixnum.
187 188 189 |
# File 'lib/search_flip/index.rb', line 187 def record_id(record) record.id end |
#refresh ⇒ Object
Sends a index refresh request to Elasticsearch. Raises SearchFlip::ResponseError in case any errors occur.
500 501 502 |
# File 'lib/search_flip/index.rb', line 500 def refresh connection.refresh(index_name_with_prefix) end |
#scope(name, &block) ⇒ Object
Adds a named scope to the index.
141 142 143 |
# File 'lib/search_flip/index.rb', line 141 def scope(name, &block) define_singleton_method(name, &block) end |
#serialize(record) ⇒ Hash
Override this method to generate a hash representation of a record, used to generate the JSON representation of it.
121 122 123 |
# File 'lib/search_flip/index.rb', line 121 def serialize(record) raise SearchFlip::MethodNotImplemented, "You must implement #{name}::serialize(record)" end |
#type_name ⇒ String
Override to specify the type name used within Elasticsearch. Recap, this gem uses an individual index for each index class, because Elasticsearch requires to have the same mapping for the same field name, even if the field is living in different types of the same index.
266 267 268 |
# File 'lib/search_flip/index.rb', line 266 def type_name "_doc" end |
#type_url ⇒ String
Returns the full Elasticsearch type URL, ie base URL, index name with prefix and type name.
650 651 652 |
# File 'lib/search_flip/index.rb', line 650 def type_url connection.type_url(index_name_with_prefix, type_name) end |
#unfreeze_index ⇒ Boolean
Unfreezes the index within Elasticsearch. Raises SearchFlip::ResponseError in case any errors occur.
370 371 372 |
# File 'lib/search_flip/index.rb', line 370 def unfreeze_index connection.unfreeze_index(index_name_with_prefix) end |
#update(scope, options = {}, additional_index_options = {}) ⇒ Object
Indexes the given record set, array of records or individual record using Elasticsearch’s update operation via the Bulk API, such that the request will fail if a record you want to update does not already exist in Elasticsearch.
578 579 580 581 582 583 584 585 586 |
# File 'lib/search_flip/index.rb', line 578 def update(scope, = {}, = {}) bulk do |indexer| each_record(scope, index_scope: true) do |object| indexer.update record_id(object), { doc: serialize(object) }, (object).merge() end end scope end |
#update_index_settings ⇒ Boolean
Updates the index settings within Elasticsearch according to the index settings specified. Raises SearchFlip::ResponseError in case any errors occur.
380 381 382 |
# File 'lib/search_flip/index.rb', line 380 def update_index_settings connection.update_index_settings(index_name_with_prefix, index_settings) end |
#update_mapping ⇒ Object
Updates the type mapping within Elasticsearch according to the mapping currently specified. Raises SearchFlip::ResponseError in case any errors occur.
413 414 415 416 417 418 419 |
# File 'lib/search_flip/index.rb', line 413 def update_mapping if include_type_name? connection.update_mapping(index_name_with_prefix, { type_name => mapping }, type_name: type_name) else connection.update_mapping(index_name_with_prefix, mapping) end end |
#with_settings(index_name: nil, connection: nil) ⇒ Class
Creates an anonymous class inheriting from the current index with a custom index name and/or connection. This is e.g. useful when working with aliases or proxies.
94 95 96 97 98 99 |
# File 'lib/search_flip/index.rb', line 94 def with_settings(index_name: nil, connection: nil) Class.new(self).tap do |klass| klass.define_singleton_method(:index_name) { index_name } if index_name klass.define_singleton_method(:connection) { connection } if connection end end |