Commit cb63edde993b7b74f294ce4d65ad9ec7b877d316

Authored by Andrew Kane
1 parent 2e44f5be

Changed option name to model includes

@@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
3 - Added `_all` and `default_fields` options 3 - Added `_all` and `default_fields` options
4 - Added global `index_prefix` option 4 - Added global `index_prefix` option
5 - Added `wait` option to async reindex 5 - Added `wait` option to async reindex
  6 +- Added `model_includes` option
6 - Raise error for `reindex_status` when Redis not configured 7 - Raise error for `reindex_status` when Redis not configured
7 8
8 ## 2.3.1 9 ## 2.3.1
@@ -1636,12 +1636,11 @@ Eager load associations @@ -1636,12 +1636,11 @@ Eager load associations
1636 Product.search "milk", includes: [:brand, :stores] 1636 Product.search "milk", includes: [:brand, :stores]
1637 ``` 1637 ```
1638 1638
1639 -Load associations per model 1639 +Eager load different associations by model [master]
1640 1640
1641 ```ruby 1641 ```ruby
1642 -Searchkick.search("*", index_name: [Product.search_index.name, Store.search_index.name], includes_per_model: {Product => :store, Store => :product}) 1642 +Searchkick.search("*", index_name: [Product, Store], model_includes: {Product => [:store], Store => [:product]})
1643 ``` 1643 ```
1644 -These 2 options above can be combined, but you should make sure that associations specified by `includes` present in all searched models  
1645 1644
1646 Turn off special characters 1645 Turn off special characters
1647 1646
lib/searchkick/query.rb
@@ -17,8 +17,8 @@ module Searchkick @@ -17,8 +17,8 @@ module Searchkick
17 def initialize(klass, term = "*", **options) 17 def initialize(klass, term = "*", **options)
18 unknown_keywords = options.keys - [:aggs, :body, :body_options, :boost, 18 unknown_keywords = options.keys - [:aggs, :body, :body_options, :boost,
19 :boost_by, :boost_by_distance, :boost_where, :conversions, :conversions_term, :debug, :emoji, :exclude, :execute, :explain, 19 :boost_by, :boost_by_distance, :boost_where, :conversions, :conversions_term, :debug, :emoji, :exclude, :execute, :explain,
20 - :fields, :highlight, :includes, :includes_per_model, :index_name, :indices_boost, :limit, :load,  
21 - :match, :misspellings, :offset, :operator, :order, :padding, :page, :per_page, :profile, 20 + :fields, :highlight, :includes, :index_name, :indices_boost, :limit, :load,
  21 + :match, :misspellings, :model_includes, :offset, :operator, :order, :padding, :page, :per_page, :profile,
22 :request_params, :routing, :select, :similar, :smart_aggs, :suggest, :track, :type, :where] 22 :request_params, :routing, :select, :similar, :smart_aggs, :suggest, :track, :type, :where]
23 raise ArgumentError, "unknown keywords: #{unknown_keywords.join(", ")}" if unknown_keywords.any? 23 raise ArgumentError, "unknown keywords: #{unknown_keywords.join(", ")}" if unknown_keywords.any?
24 24
@@ -108,7 +108,7 @@ module Searchkick @@ -108,7 +108,7 @@ module Searchkick
108 padding: @padding, 108 padding: @padding,
109 load: @load, 109 load: @load,
110 includes: options[:includes], 110 includes: options[:includes],
111 - includes_per_model: options[:includes_per_model], 111 + model_includes: options[:model_includes],
112 json: !@json.nil?, 112 json: !@json.nil?,
113 match_suffix: @match_suffix, 113 match_suffix: @match_suffix,
114 highlighted_fields: @highlighted_fields || [], 114 highlighted_fields: @highlighted_fields || [],
lib/searchkick/results.rb
@@ -198,13 +198,10 @@ module Searchkick @@ -198,13 +198,10 @@ module Searchkick
198 198
199 def results_query(records, hits) 199 def results_query(records, hits)
200 ids = hits.map { |hit| hit["_id"] } 200 ids = hits.map { |hit| hit["_id"] }
201 - if (options[:includes] || options[:includes_per_model])  
202 -  
203 - 201 + if options[:includes] || options[:model_includes]
204 included_relations = [] 202 included_relations = []
205 -  
206 - included_relations << options[:includes] if options[:includes]  
207 - included_relations << options[:includes_per_model][records] if (options[:includes_per_model] && options[:includes_per_model][records]) 203 + combine_includes(included_relations, options[:includes])
  204 + combine_includes(included_relations, options[:model_includes][records]) if options[:model_includes]
208 205
209 records = 206 records =
210 if defined?(NoBrainer::Document) && records < NoBrainer::Document 207 if defined?(NoBrainer::Document) && records < NoBrainer::Document
@@ -221,6 +218,16 @@ module Searchkick @@ -221,6 +218,16 @@ module Searchkick
221 Searchkick.load_records(records, ids) 218 Searchkick.load_records(records, ids)
222 end 219 end
223 220
  221 + def combine_includes(result, inc)
  222 + if inc
  223 + if inc.is_a?(Array)
  224 + result.concat(inc)
  225 + else
  226 + result << inc
  227 + end
  228 + end
  229 + end
  230 +
224 def base_field(k) 231 def base_field(k)
225 k.sub(/\.(analyzed|word_start|word_middle|word_end|text_start|text_middle|text_end|exact)\z/, "") 232 k.sub(/\.(analyzed|word_start|word_middle|word_end|text_start|text_middle|text_end|exact)\z/, "")
226 end 233 end
test/sql_test.rb
@@ -196,19 +196,19 @@ class SqlTest &lt; Minitest::Test @@ -196,19 +196,19 @@ class SqlTest &lt; Minitest::Test
196 assert Product.search("product", includes: [:store]).first.association(:store).loaded? 196 assert Product.search("product", includes: [:store]).first.association(:store).loaded?
197 end 197 end
198 198
199 - def test_includes_per_model 199 + def test_model_includes
200 skip unless defined?(ActiveRecord) 200 skip unless defined?(ActiveRecord)
201 - store_names ["Product A"]  
202 - store_names ['Product A Store'], Store  
203 201
204 - associations = { Product => :store, Store => :products } 202 + store_names ["Product A"]
  203 + store_names ["Store A"], Store
205 204
206 - result = Searchkick.search("Product", fields: ['name'], index_name: [Product.search_index.name, Store.search_index.name], includes_per_model: associations) 205 + associations = {Product => [:store], Store => [:products]}
  206 + result = Searchkick.search("*", index_name: [Product, Store], model_includes: associations)
207 207
208 assert_equal 2, result.length 208 assert_equal 2, result.length
209 209
210 result.group_by(&:class).each_pair do |klass, records| 210 result.group_by(&:class).each_pair do |klass, records|
211 - assert records.first.association(associations[klass]).loaded? 211 + assert records.first.association(associations[klass].first).loaded?
212 end 212 end
213 end 213 end
214 end 214 end
test/test_helper.rb
@@ -450,7 +450,6 @@ class Store @@ -450,7 +450,6 @@ class Store
450 searchkick \ 450 searchkick \
451 default_fields: elasticsearch_below60? ? nil : [:name], 451 default_fields: elasticsearch_below60? ? nil : [:name],
452 routing: true, 452 routing: true,
453 - searchable: [:name],  
454 merge_mappings: true, 453 merge_mappings: true,
455 mappings: { 454 mappings: {
456 store: { 455 store: {