Module: Elasticsearch::Model::ClassMethods

Included in:
Elasticsearch::Model
Defined in:
lib/elasticsearch/model.rb

Instance Method Summary collapse

Instance Method Details

#clientObject

Get the client common for all models

Examples:

Get the client


Elasticsearch::Model.client
=> #<Elasticsearch::Transport::Client:0x007f96a7d0d000 @transport=... >


142
143
144
# File 'lib/elasticsearch/model.rb', line 142

def client
  @client ||= Elasticsearch::Client.new
end

#client=(client) ⇒ Object

Note:

You have to set the client before you call Elasticsearch methods on the model, or set it directly on the model; see Elasticsearch::Model::Client::ClassMethods#client

Set the client for all models

Examples:

Configure (set) the client for all models


Elasticsearch::Model.client = Elasticsearch::Client.new host: 'http://localhost:9200', tracer: true
=> #<Elasticsearch::Transport::Client:0x007f96a6dd0d80 @transport=... >


156
157
158
# File 'lib/elasticsearch/model.rb', line 156

def client=(client)
  @client = client
end

#inheritance_enabledObject

Note:

Inheritance is disabled by default.

Check if inheritance is enabled



189
190
191
# File 'lib/elasticsearch/model.rb', line 189

def inheritance_enabled
  @settings[:inheritance_enabled] ||= false
end

#inheritance_enabled=(inheritance_enabled) ⇒ Object

Enable inheritance of index_name and document_type

Examples:

Enable inheritance


Elasticsearch::Model.inheritance_enabled = true


199
200
201
202
# File 'lib/elasticsearch/model.rb', line 199

def inheritance_enabled=(inheritance_enabled)
  warn STI_DEPRECATION_WARNING if inheritance_enabled
  @settings[:inheritance_enabled] = inheritance_enabled
end

#search(query_or_payload, models = [], options = {}) ⇒ Elasticsearch::Model::Response::Response

Search across multiple models

By default, all models which include the ‘Elasticsearch::Model` module are searched

Examples:

Search across specific models


Elasticsearch::Model.search('foo', [Author, Article])

Search across all models which include the ‘Elasticsearch::Model` module


Elasticsearch::Model.search('foo')

Parameters:

  • query_or_payload (String, Hash, Object)

    The search request definition (string, JSON, Hash, or object responding to ‘to_hash`)

  • models (Array) (defaults to: [])

    The Array of Model objects to search

  • options (Hash) (defaults to: {})

    Optional parameters to be passed to the Elasticsearch client

Returns:



179
180
181
182
183
# File 'lib/elasticsearch/model.rb', line 179

def search(query_or_payload, models=[], options={})
  models = Multimodel.new(models)
  request = Searching::SearchRequest.new(models, query_or_payload, options)
  Response::Response.new(models, request)
end

#settingsObject

Access the module settings



206
207
208
# File 'lib/elasticsearch/model.rb', line 206

def settings
  @settings ||= {}
end