diff --git a/lib/searchkick.rb b/lib/searchkick.rb index 46b0efa..824fd94 100644 --- a/lib/searchkick.rb +++ b/lib/searchkick.rb @@ -1,5 +1,5 @@ require "active_model" -require "elasticsearch/model" +require "elasticsearch" require "searchkick/version" require "searchkick/index" require "searchkick/reindex" diff --git a/lib/searchkick/query.rb b/lib/searchkick/query.rb index cd8849b..4192feb 100644 --- a/lib/searchkick/query.rb +++ b/lib/searchkick/query.rb @@ -337,14 +337,13 @@ module Searchkick response["facets"][field]["other"] = facet["total"] - facet["terms"].sum{|term| term["count"] } end - results = Searchkick::Results.new(searchkick_klass, nil) - results.response = response - results.current_page = @page - results.per_page = @per_page - results.options = { + opts = { load: @load, includes: options[:include] || options[:includes] } + results = Searchkick::Results.new(searchkick_klass, response, opts) + results.current_page = @page + results.per_page = @per_page results end diff --git a/lib/searchkick/results.rb b/lib/searchkick/results.rb index d7d01cb..a39e3e1 100644 --- a/lib/searchkick/results.rb +++ b/lib/searchkick/results.rb @@ -1,16 +1,39 @@ module Searchkick - class Results < Elasticsearch::Model::Response::Response - attr_writer :response - attr_accessor :current_page, :per_page, :options + class Results + include Enumerable + extend Forwardable - delegate :each, :empty?, :size, :slice, :[], :to_ary, to: :results_or_records + attr_reader :klass, :response, :options + attr_accessor :current_page, :per_page + + def_delegators :results_or_records, :each, :empty?, :size, :slice, :[], :to_ary + + def initialize(klass, response, options = {}) + @klass = klass + @response = response + @options = options + end def results_or_records - options[:load] ? records.to_a : results.to_a + options[:load] ? records : results + end + + def results + results = @response["hits"]["hits"] end def records - options[:includes] ? super.includes(options[:includes]) : super + @records ||= begin + hits = results + hit_ids = hits.map{|hit| hit["_id"] } + records = klass + if options[:includes] + records = records.includes(options[:includes]) + end + records = records.find(hit_ids) + hit_ids = hit_ids.map(&:to_s) + records.sort_by{|r| hit_ids.index(r.id.to_s) } + end end def suggestions @@ -21,8 +44,12 @@ module Searchkick end end + def each_with_hit(&block) + records.zip(results).each(&block) + end + def with_details - records.each_with_hit.map do |model, hit| + each_with_hit.map do |model, hit| details = {} if hit["highlight"] details[:highlight] = Hash[ hit["highlight"].map{|k, v| [k.sub(/\.analyzed\z/, "").to_sym, v.first] } ] diff --git a/searchkick.gemspec b/searchkick.gemspec index bedfcfc..d4d70c0 100644 --- a/searchkick.gemspec +++ b/searchkick.gemspec @@ -19,7 +19,7 @@ Gem::Specification.new do |spec| spec.require_paths = ["lib"] spec.add_dependency "activemodel" - spec.add_dependency "elasticsearch-model" + spec.add_dependency "elasticsearch" spec.add_development_dependency "bundler", "~> 1.3" spec.add_development_dependency "rake" diff --git a/test/sql_test.rb b/test/sql_test.rb index f2117b6..9aad0ce 100644 --- a/test/sql_test.rb +++ b/test/sql_test.rb @@ -247,12 +247,12 @@ class TestSql < Minitest::Unit::TestCase def test_load_false store_names ["Product A"] - assert_kind_of Elasticsearch::Model::Response::Result, Product.search("product", load: false).first + assert_kind_of Hash, Product.search("product", load: false).first end def test_load_false_with_include store_names ["Product A"] - assert_kind_of Elasticsearch::Model::Response::Result, Product.search("product", load: false, include: [:store]).first + assert_kind_of Hash, Product.search("product", load: false, include: [:store]).first end # TODO see if Mongoid is loaded -- libgit2 0.21.0