Module: Chewy::Search::ClassMethods

Defined in:
lib/chewy/search.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Delegates methods from the request class to the index or type class

Examples:

PlacesIndex.query(match: {name: 'Moscow'})
PlacesIndex::City.query(match: {name: 'Moscow'})


74
75
76
77
78
79
80
# File 'lib/chewy/search.rb', line 74

def method_missing(name, *args, &block)
  if search_class::DELEGATED_METHODS.include?(name)
    all.send(name, *args, &block)
  else
    super
  end
end

Instance Method Details

#allChewy::Search::Request

This is the entry point for the request composition, however, most of the Request methods are delegated directly as well.

This method also provides an ability to use names scopes.

Examples:

PlacesIndex.all.limit(10)
# is basically the same as:
PlacesIndex.limit(10)

Returns:

See Also:



52
53
54
# File 'lib/chewy/search.rb', line 52

def all
  search_class.scopes.last || search_class.new(self)
end

#respond_to_missing?(name, _) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/chewy/search.rb', line 82

def respond_to_missing?(name, _)
  search_class::DELEGATED_METHODS.include?(name) || super
end

#search_string(query, options = {}) ⇒ Hash

A simple way to execute search string query.



60
61
62
63
64
65
66
67
# File 'lib/chewy/search.rb', line 60

def search_string(query, options = {})
  options = options.merge(
    index: all._indexes.map(&:index_name),
    type: all._types.map(&:type_name),
    q: query
  )
  Chewy.client.search(options)
end