Module: ElasticSearchable::Queries
- Defined in:
- lib/elastic_searchable/queries.rb
Constant Summary collapse
- PER_PAGE_DEFAULT =
20
Instance Method Summary collapse
- #per_page ⇒ Object
-
#search(query, options = {}) ⇒ Object
search returns a will_paginate collection of ActiveRecord objects for the search results supported options: :page - page of results to search for :per_page - number of results per page.
Instance Method Details
#per_page ⇒ Object
5 6 7 |
# File 'lib/elastic_searchable/queries.rb', line 5 def per_page PER_PAGE_DEFAULT end |
#search(query, options = {}) ⇒ Object
search returns a will_paginate collection of ActiveRecord objects for the search results supported options: :page - page of results to search for :per_page - number of results per page
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/elastic_searchable/queries.rb', line 15 def search(query, = {}) page = (.delete(:page) || 1).to_i [:fields] ||= '_id' [:size] ||= per_page_for_search() [:from] ||= [:size] * (page - 1) if query.is_a?(Hash) [:query] = query else [:query] = { :query_string => { :query => query, :default_operator => .delete(:default_operator) } } end query = {} case sort = .delete(:sort) when Array,Hash [:sort] = sort when String query[:sort] = sort end response = ElasticSearchable.request :get, index_type_path('_search'), :query => query, :json_body => hits = response['hits'] ids = hits['hits'].collect {|h| h['_id'].to_i } results = self.find(ids).sort_by {|result| ids.index(result.id) } results.each do |result| result.instance_variable_set '@hit', hits['hits'][ids.index(result.id)] end ElasticSearchable::Paginator.handler.new(results, page, [:size], hits['total']) end |