Module: Elasticsearch::Model::Searching::ClassMethods

Included in:
Proxy::ClassMethodsProxy
Defined in:
lib/elasticsearch/model/searching.rb

Instance Method Summary collapse

Instance Method Details

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

Provides a ‘search` method for the model to easily search within an index/type corresponding to the model settings.

Examples:

Simple search in ‘Article`


Article.search 'foo'

Search using a search definition as a Hash


response = Article.search \
             query: {
               match: {
                 title: 'foo'
               }
             },
             highlight: {
               fields: {
                 title: {}
               }
             },
             size: 50

response.results.first.title
# => "Foo"

response.results.first.highlight.title
# => ["<em>Foo</em>"]

response.records.first.title
#  Article Load (0.2ms)  SELECT "articles".* FROM "articles" WHERE "articles"."id" IN (1, 3)
# => "Foo"

Search using a search definition as a JSON string


Article.search '{"query" : { "match_all" : {} }}'

Parameters:

  • query_or_payload (String, Hash, Object)

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

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

    Optional parameters to be passed to the Elasticsearch client

Returns:



115
116
117
118
119
# File 'lib/elasticsearch/model/searching.rb', line 115

def search(query_or_payload, options={})
  search   = SearchRequest.new(self, query_or_payload, options)

  Response::Response.new(self, search)
end