Commit 2e44f5be893fa4b9eb41dc575ad20e29de2de0d4
1 parent
4c2d92bd
Exists in
master
and in
19 other branches
rename includes_per to includes_per_model, get rid of unnecessary Discount model in tests
Showing
5 changed files
with
13 additions
and
88 deletions
Show diff stats
README.md
... | ... | @@ -1639,7 +1639,7 @@ Product.search "milk", includes: [:brand, :stores] |
1639 | 1639 | Load associations per model |
1640 | 1640 | |
1641 | 1641 | ```ruby |
1642 | -Searchkick.search("*", index_name: [Product.search_index.name, Discount.search_index.name], includes_per: {Product => :store, Discount => :product}) | |
1642 | +Searchkick.search("*", index_name: [Product.search_index.name, Store.search_index.name], includes_per_model: {Product => :store, Store => :product}) | |
1643 | 1643 | ``` |
1644 | 1644 | These 2 options above can be combined, but you should make sure that associations specified by `includes` present in all searched models |
1645 | 1645 | ... | ... |
lib/searchkick/query.rb
... | ... | @@ -17,7 +17,7 @@ module Searchkick |
17 | 17 | def initialize(klass, term = "*", **options) |
18 | 18 | unknown_keywords = options.keys - [:aggs, :body, :body_options, :boost, |
19 | 19 | :boost_by, :boost_by_distance, :boost_where, :conversions, :conversions_term, :debug, :emoji, :exclude, :execute, :explain, |
20 | - :fields, :highlight, :includes, :includes_per, :index_name, :indices_boost, :limit, :load, | |
20 | + :fields, :highlight, :includes, :includes_per_model, :index_name, :indices_boost, :limit, :load, | |
21 | 21 | :match, :misspellings, :offset, :operator, :order, :padding, :page, :per_page, :profile, |
22 | 22 | :request_params, :routing, :select, :similar, :smart_aggs, :suggest, :track, :type, :where] |
23 | 23 | raise ArgumentError, "unknown keywords: #{unknown_keywords.join(", ")}" if unknown_keywords.any? |
... | ... | @@ -108,7 +108,7 @@ module Searchkick |
108 | 108 | padding: @padding, |
109 | 109 | load: @load, |
110 | 110 | includes: options[:includes], |
111 | - includes_per: options[:includes_per], | |
111 | + includes_per_model: options[:includes_per_model], | |
112 | 112 | json: !@json.nil?, |
113 | 113 | match_suffix: @match_suffix, |
114 | 114 | highlighted_fields: @highlighted_fields || [], | ... | ... |
lib/searchkick/results.rb
... | ... | @@ -198,13 +198,13 @@ module Searchkick |
198 | 198 | |
199 | 199 | def results_query(records, hits) |
200 | 200 | ids = hits.map { |hit| hit["_id"] } |
201 | - if (options[:includes] || options[:includes_per]) | |
201 | + if (options[:includes] || options[:includes_per_model]) | |
202 | 202 | |
203 | 203 | |
204 | 204 | included_relations = [] |
205 | 205 | |
206 | 206 | included_relations << options[:includes] if options[:includes] |
207 | - included_relations << options[:includes_per][records] if (options[:includes_per] && options[:includes_per][records]) | |
207 | + included_relations << options[:includes_per_model][records] if (options[:includes_per_model] && options[:includes_per_model][records]) | |
208 | 208 | |
209 | 209 | records = |
210 | 210 | if defined?(NoBrainer::Document) && records < NoBrainer::Document | ... | ... |
test/sql_test.rb
1 | 1 | require_relative "test_helper" |
2 | + | |
2 | 3 | class SqlTest < Minitest::Test |
3 | 4 | def test_operator |
4 | 5 | store_names ["Honey"] |
... | ... | @@ -195,35 +196,19 @@ class SqlTest < Minitest::Test |
195 | 196 | assert Product.search("product", includes: [:store]).first.association(:store).loaded? |
196 | 197 | end |
197 | 198 | |
198 | - def test_includes_per | |
199 | + def test_includes_per_model | |
199 | 200 | skip unless defined?(ActiveRecord) |
200 | 201 | store_names ["Product A"] |
201 | - store_names ['Product A Discount'], Discount | |
202 | + store_names ['Product A Store'], Store | |
203 | + | |
204 | + associations = { Product => :store, Store => :products } | |
202 | 205 | |
203 | - associations = { Product => :store, Discount => :product } | |
206 | + result = Searchkick.search("Product", fields: ['name'], index_name: [Product.search_index.name, Store.search_index.name], includes_per_model: associations) | |
204 | 207 | |
205 | - result = Searchkick.search("product", fields: [:name], index_name: [Product.search_index.name, Discount.search_index.name], includes_per: associations) | |
208 | + assert_equal 2, result.length | |
206 | 209 | |
207 | 210 | result.group_by(&:class).each_pair do |klass, records| |
208 | 211 | assert records.first.association(associations[klass]).loaded? |
209 | 212 | end |
210 | 213 | end |
211 | - | |
212 | - def test_both_includes | |
213 | - skip unless defined?(ActiveRecord) | |
214 | - store_names ["Product A"] | |
215 | - store_names ['Product A Discount'], Discount | |
216 | - | |
217 | - associations = { Discount => :product } | |
218 | - | |
219 | - result = Searchkick.search("product", fields: [:name], index_name: [Product.search_index.name, Discount.search_index.name], includes: [:store], includes_per: associations) | |
220 | - | |
221 | - assert_equal 2, result.length | |
222 | - | |
223 | - assert result.find{|record| record.class == Product}.association(:store).loaded? | |
224 | - assert result.find{|record| record.class == Discount}.association(:store).loaded? | |
225 | - assert result.find{|record| record.class == Discount}.association(:product).loaded? | |
226 | - end | |
227 | - | |
228 | - | |
229 | 214 | end | ... | ... |
test/test_helper.rb
... | ... | @@ -84,14 +84,6 @@ 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 | - belongs_to :store | |
91 | - | |
92 | - field :name | |
93 | - end | |
94 | - | |
95 | 87 | class Store |
96 | 88 | include Mongoid::Document |
97 | 89 | has_many :products |
... | ... | @@ -155,18 +147,6 @@ elsif defined?(NoBrainer) |
155 | 147 | belongs_to :store, validates: false |
156 | 148 | end |
157 | 149 | |
158 | - class Discount | |
159 | - include NoBrainer::Document | |
160 | - | |
161 | - field :id, type: Object | |
162 | - field :name, type: String | |
163 | - | |
164 | - belongs_to :product, validates: false | |
165 | - belongs_to :store, validates: false | |
166 | - | |
167 | - end | |
168 | - | |
169 | - | |
170 | 150 | class Store |
171 | 151 | include NoBrainer::Document |
172 | 152 | |
... | ... | @@ -240,13 +220,6 @@ elsif defined?(Cequel) |
240 | 220 | column :created_at, :timestamp |
241 | 221 | end |
242 | 222 | |
243 | - class Discount | |
244 | - include Cequel::Record | |
245 | - | |
246 | - key :id, :uuid, auto: true | |
247 | - column :name, :text | |
248 | - end | |
249 | - | |
250 | 223 | class Store |
251 | 224 | include Cequel::Record |
252 | 225 | |
... | ... | @@ -372,12 +345,6 @@ else |
372 | 345 | t.timestamps null: true |
373 | 346 | end |
374 | 347 | |
375 | - ActiveRecord::Migration.create_table :discounts do |t| | |
376 | - t.string :name | |
377 | - t.integer :product_id | |
378 | - t.integer :store_id | |
379 | - end | |
380 | - | |
381 | 348 | ActiveRecord::Migration.create_table :stores do |t| |
382 | 349 | t.string :name |
383 | 350 | end |
... | ... | @@ -402,13 +369,6 @@ else |
402 | 369 | |
403 | 370 | class Product < ActiveRecord::Base |
404 | 371 | belongs_to :store |
405 | - | |
406 | - has_many :discounts | |
407 | - end | |
408 | - | |
409 | - class Discount < ActiveRecord::Base | |
410 | - belongs_to :product | |
411 | - belongs_to :store | |
412 | 372 | end |
413 | 373 | |
414 | 374 | class Store < ActiveRecord::Base |
... | ... | @@ -486,29 +446,11 @@ class Product |
486 | 446 | end |
487 | 447 | end |
488 | 448 | |
489 | -class Discount | |
490 | - searchkick \ | |
491 | - searchable: [:name], | |
492 | - merge_mappings: true, | |
493 | - mappings: { | |
494 | - discount: { | |
495 | - properties: { | |
496 | - name: elasticsearch_below50? ? {type: "string", analyzer: "keyword"} : {type: "keyword"} | |
497 | - } | |
498 | - } | |
499 | - } | |
500 | - | |
501 | - def search_data | |
502 | - { | |
503 | - name: name | |
504 | - } | |
505 | - end | |
506 | -end | |
507 | - | |
508 | 449 | class Store |
509 | 450 | searchkick \ |
510 | 451 | default_fields: elasticsearch_below60? ? nil : [:name], |
511 | 452 | routing: true, |
453 | + searchable: [:name], | |
512 | 454 | merge_mappings: true, |
513 | 455 | mappings: { |
514 | 456 | store: { |
... | ... | @@ -580,7 +522,6 @@ Product.reindex |
580 | 522 | Product.reindex # run twice for both index paths |
581 | 523 | Product.create!(name: "Set mapping") |
582 | 524 | |
583 | -Discount.reindex | |
584 | 525 | Store.reindex |
585 | 526 | Animal.reindex |
586 | 527 | Speaker.reindex |
... | ... | @@ -590,7 +531,6 @@ class Minitest::Test |
590 | 531 | def setup |
591 | 532 | Product.destroy_all |
592 | 533 | Store.destroy_all |
593 | - Discount.destroy_all | |
594 | 534 | Animal.destroy_all |
595 | 535 | Speaker.destroy_all |
596 | 536 | Sku.destroy_all | ... | ... |