Commit 1c4f8f2f5e59e99212d77d323d0dbd0f12e69e5e
1 parent
2a9334e8
Exists in
master
and in
21 other branches
Removed dependency on elasticsearch-rails
Showing
5 changed files
with
42 additions
and
16 deletions
Show diff stats
lib/searchkick.rb
lib/searchkick/query.rb
... | ... | @@ -337,14 +337,13 @@ module Searchkick |
337 | 337 | response["facets"][field]["other"] = facet["total"] - facet["terms"].sum{|term| term["count"] } |
338 | 338 | end |
339 | 339 | |
340 | - results = Searchkick::Results.new(searchkick_klass, nil) | |
341 | - results.response = response | |
342 | - results.current_page = @page | |
343 | - results.per_page = @per_page | |
344 | - results.options = { | |
340 | + opts = { | |
345 | 341 | load: @load, |
346 | 342 | includes: options[:include] || options[:includes] |
347 | 343 | } |
344 | + results = Searchkick::Results.new(searchkick_klass, response, opts) | |
345 | + results.current_page = @page | |
346 | + results.per_page = @per_page | |
348 | 347 | results |
349 | 348 | end |
350 | 349 | ... | ... |
lib/searchkick/results.rb
1 | 1 | module Searchkick |
2 | - class Results < Elasticsearch::Model::Response::Response | |
3 | - attr_writer :response | |
4 | - attr_accessor :current_page, :per_page, :options | |
2 | + class Results | |
3 | + include Enumerable | |
4 | + extend Forwardable | |
5 | 5 | |
6 | - delegate :each, :empty?, :size, :slice, :[], :to_ary, to: :results_or_records | |
6 | + attr_reader :klass, :response, :options | |
7 | + attr_accessor :current_page, :per_page | |
8 | + | |
9 | + def_delegators :results_or_records, :each, :empty?, :size, :slice, :[], :to_ary | |
10 | + | |
11 | + def initialize(klass, response, options = {}) | |
12 | + @klass = klass | |
13 | + @response = response | |
14 | + @options = options | |
15 | + end | |
7 | 16 | |
8 | 17 | def results_or_records |
9 | - options[:load] ? records.to_a : results.to_a | |
18 | + options[:load] ? records : results | |
19 | + end | |
20 | + | |
21 | + def results | |
22 | + results = @response["hits"]["hits"] | |
10 | 23 | end |
11 | 24 | |
12 | 25 | def records |
13 | - options[:includes] ? super.includes(options[:includes]) : super | |
26 | + @records ||= begin | |
27 | + hits = results | |
28 | + hit_ids = hits.map{|hit| hit["_id"] } | |
29 | + records = klass | |
30 | + if options[:includes] | |
31 | + records = records.includes(options[:includes]) | |
32 | + end | |
33 | + records = records.find(hit_ids) | |
34 | + hit_ids = hit_ids.map(&:to_s) | |
35 | + records.sort_by{|r| hit_ids.index(r.id.to_s) } | |
36 | + end | |
14 | 37 | end |
15 | 38 | |
16 | 39 | def suggestions |
... | ... | @@ -21,8 +44,12 @@ module Searchkick |
21 | 44 | end |
22 | 45 | end |
23 | 46 | |
47 | + def each_with_hit(&block) | |
48 | + records.zip(results).each(&block) | |
49 | + end | |
50 | + | |
24 | 51 | def with_details |
25 | - records.each_with_hit.map do |model, hit| | |
52 | + each_with_hit.map do |model, hit| | |
26 | 53 | details = {} |
27 | 54 | if hit["highlight"] |
28 | 55 | details[:highlight] = Hash[ hit["highlight"].map{|k, v| [k.sub(/\.analyzed\z/, "").to_sym, v.first] } ] | ... | ... |
searchkick.gemspec
... | ... | @@ -19,7 +19,7 @@ Gem::Specification.new do |spec| |
19 | 19 | spec.require_paths = ["lib"] |
20 | 20 | |
21 | 21 | spec.add_dependency "activemodel" |
22 | - spec.add_dependency "elasticsearch-model" | |
22 | + spec.add_dependency "elasticsearch" | |
23 | 23 | |
24 | 24 | spec.add_development_dependency "bundler", "~> 1.3" |
25 | 25 | spec.add_development_dependency "rake" | ... | ... |
test/sql_test.rb
... | ... | @@ -247,12 +247,12 @@ class TestSql < Minitest::Unit::TestCase |
247 | 247 | |
248 | 248 | def test_load_false |
249 | 249 | store_names ["Product A"] |
250 | - assert_kind_of Elasticsearch::Model::Response::Result, Product.search("product", load: false).first | |
250 | + assert_kind_of Hash, Product.search("product", load: false).first | |
251 | 251 | end |
252 | 252 | |
253 | 253 | def test_load_false_with_include |
254 | 254 | store_names ["Product A"] |
255 | - assert_kind_of Elasticsearch::Model::Response::Result, Product.search("product", load: false, include: [:store]).first | |
255 | + assert_kind_of Hash, Product.search("product", load: false, include: [:store]).first | |
256 | 256 | end |
257 | 257 | |
258 | 258 | # TODO see if Mongoid is loaded | ... | ... |