Commit bb4fa27cc2c49d0bf27eb06959d94bbe82bdceb0
1 parent
7b6be02b
Exists in
master
and in
19 other branches
added per-model includes support
Showing
3 changed files
with
81 additions
and
5 deletions
Show diff stats
lib/searchkick/results.rb
... | ... | @@ -198,17 +198,23 @@ module Searchkick |
198 | 198 | |
199 | 199 | def results_query(records, hits) |
200 | 200 | ids = hits.map { |hit| hit["_id"] } |
201 | - | |
202 | 201 | if options[:includes] |
202 | + | |
203 | + included_relations = if options[:includes].is_a? Hash | |
204 | + options[:includes][records] | |
205 | + else | |
206 | + options[:includes] | |
207 | + end | |
208 | + | |
203 | 209 | records = |
204 | 210 | if defined?(NoBrainer::Document) && records < NoBrainer::Document |
205 | 211 | if Gem.loaded_specs["nobrainer"].version >= Gem::Version.new("0.21") |
206 | - records.eager_load(options[:includes]) | |
212 | + records.eager_load(included_relations) | |
207 | 213 | else |
208 | - records.preload(options[:includes]) | |
214 | + records.preload(included_relations) | |
209 | 215 | end |
210 | 216 | else |
211 | - records.includes(options[:includes]) | |
217 | + records.includes(included_relations) | |
212 | 218 | end |
213 | 219 | end |
214 | 220 | ... | ... |
test/sql_test.rb
1 | 1 | require_relative "test_helper" |
2 | - | |
3 | 2 | class SqlTest < Minitest::Test |
4 | 3 | def test_operator |
5 | 4 | store_names ["Honey"] |
... | ... | @@ -195,4 +194,20 @@ class SqlTest < Minitest::Test |
195 | 194 | store_names ["Product A"] |
196 | 195 | assert Product.search("product", includes: [:store]).first.association(:store).loaded? |
197 | 196 | end |
197 | + | |
198 | + def test_includes_per_model | |
199 | + skip unless defined?(ActiveRecord) | |
200 | + store_names ["Product A"] | |
201 | + store_names ['Product A Discount'], Discount | |
202 | + | |
203 | + associations = {Product => :store, Discount => :product} | |
204 | + | |
205 | + result = Searchkick.search("product", fields: [:name], index_name: [Product.search_index.name, Discount.search_index.name], includes: associations) | |
206 | + | |
207 | + assert_equal 2, result.length | |
208 | + | |
209 | + result.group_by(&:class).each_pair do |klass, records| | |
210 | + assert records.first.association(associations[klass]).loaded? | |
211 | + end | |
212 | + end | |
198 | 213 | end | ... | ... |
test/test_helper.rb
... | ... | @@ -84,6 +84,13 @@ if defined?(Mongoid) |
84 | 84 | field :alt_description |
85 | 85 | end |
86 | 86 | |
87 | + class Discount | |
88 | + include Mongoid::Document | |
89 | + belongs_to :product | |
90 | + | |
91 | + field :name | |
92 | + end | |
93 | + | |
87 | 94 | class Store |
88 | 95 | include Mongoid::Document |
89 | 96 | has_many :products |
... | ... | @@ -147,6 +154,16 @@ elsif defined?(NoBrainer) |
147 | 154 | belongs_to :store, validates: false |
148 | 155 | end |
149 | 156 | |
157 | + class Discount | |
158 | + include NoBrainer::Document | |
159 | + | |
160 | + field :id, type: Object | |
161 | + field :name, type: String | |
162 | + | |
163 | + belongs_to :product, validates: false | |
164 | + end | |
165 | + | |
166 | + | |
150 | 167 | class Store |
151 | 168 | include NoBrainer::Document |
152 | 169 | |
... | ... | @@ -220,6 +237,13 @@ elsif defined?(Cequel) |
220 | 237 | column :created_at, :timestamp |
221 | 238 | end |
222 | 239 | |
240 | + class Discount | |
241 | + include Cequel::Record | |
242 | + | |
243 | + key :id, :uuid, auto: true | |
244 | + column :name, :text | |
245 | + end | |
246 | + | |
223 | 247 | class Store |
224 | 248 | include Cequel::Record |
225 | 249 | |
... | ... | @@ -345,6 +369,11 @@ else |
345 | 369 | t.timestamps null: true |
346 | 370 | end |
347 | 371 | |
372 | + ActiveRecord::Migration.create_table :discounts do |t| | |
373 | + t.string :name | |
374 | + t.integer :product_id | |
375 | + end | |
376 | + | |
348 | 377 | ActiveRecord::Migration.create_table :stores do |t| |
349 | 378 | t.string :name |
350 | 379 | end |
... | ... | @@ -369,6 +398,12 @@ else |
369 | 398 | |
370 | 399 | class Product < ActiveRecord::Base |
371 | 400 | belongs_to :store |
401 | + | |
402 | + has_many :discounts | |
403 | + end | |
404 | + | |
405 | + class Discount < ActiveRecord::Base | |
406 | + belongs_to :product | |
372 | 407 | end |
373 | 408 | |
374 | 409 | class Store < ActiveRecord::Base |
... | ... | @@ -446,6 +481,25 @@ class Product |
446 | 481 | end |
447 | 482 | end |
448 | 483 | |
484 | +class Discount | |
485 | + searchkick \ | |
486 | + searchable: [:name], | |
487 | + merge_mappings: true, | |
488 | + mappings: { | |
489 | + discount: { | |
490 | + properties: { | |
491 | + name: elasticsearch_below50? ? {type: "string", analyzer: "keyword"} : {type: "keyword"} | |
492 | + } | |
493 | + } | |
494 | + } | |
495 | + | |
496 | + def search_data | |
497 | + { | |
498 | + name: name | |
499 | + } | |
500 | + end | |
501 | +end | |
502 | + | |
449 | 503 | class Store |
450 | 504 | searchkick \ |
451 | 505 | default_fields: elasticsearch_below60? ? nil : [:name], |
... | ... | @@ -521,6 +575,7 @@ Product.reindex |
521 | 575 | Product.reindex # run twice for both index paths |
522 | 576 | Product.create!(name: "Set mapping") |
523 | 577 | |
578 | +Discount.reindex | |
524 | 579 | Store.reindex |
525 | 580 | Animal.reindex |
526 | 581 | Speaker.reindex | ... | ... |