Module: Mongoid::Elasticsearch

Extended by:
ActiveSupport::Concern
Defined in:
lib/mongoid/elasticsearch.rb,
lib/mongoid/elasticsearch/es.rb,
lib/mongoid/elasticsearch/index.rb,
lib/mongoid/elasticsearch/utils.rb,
lib/mongoid/elasticsearch/version.rb,
lib/mongoid/elasticsearch/indexing.rb,
lib/mongoid/elasticsearch/response.rb,
lib/mongoid/elasticsearch/callbacks.rb,
lib/mongoid/elasticsearch/pagination.rb

Defined Under Namespace

Modules: Callbacks, Indexing, Pagination, Utils Classes: Es, Index, Response

Constant Summary collapse

VERSION =
"0.5.1"

Class Method Summary collapse

Class Method Details

.search(query, options = {}) ⇒ Object

search multiple models



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/mongoid/elasticsearch.rb', line 83

def self.search(query, options = {})
  if query.is_a?(String)
    query = {q: Utils.clean(query)}
  end
  # use `_all` or empty string to perform the operation on all indices
  # regardless whether they are managed by Mongoid::Elasticsearch or not
  unless query.key?(:index)
    query.merge!(index: Mongoid::Elasticsearch.registered_indexes.join(','), ignore_indices: 'missing')
  end

  page = options[:page]
  per_page = options[:per_page]

  query[:size] = ( per_page.to_i ) if per_page
  query[:from] = ( page.to_i <= 1 ? 0 : (per_page.to_i * (page.to_i-1)) ) if page && per_page

  options[:wrapper] ||= :model

  client = ::Elasticsearch::Client.new Mongoid::Elasticsearch.client_options
  Response.new(client, query, true, nil, options)
end