Commit cb63edde993b7b74f294ce4d65ad9ec7b877d316
1 parent
2e44f5be
Exists in
master
and in
19 other branches
Changed option name to model includes
Showing
6 changed files
with
25 additions
and
19 deletions
Show diff stats
CHANGELOG.md
README.md
... | ... | @@ -1636,12 +1636,11 @@ Eager load associations |
1636 | 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 | 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 | 1645 | Turn off special characters |
1647 | 1646 | ... | ... |
lib/searchkick/query.rb
... | ... | @@ -17,8 +17,8 @@ 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_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 | 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? |
24 | 24 | |
... | ... | @@ -108,7 +108,7 @@ module Searchkick |
108 | 108 | padding: @padding, |
109 | 109 | load: @load, |
110 | 110 | includes: options[:includes], |
111 | - includes_per_model: options[:includes_per_model], | |
111 | + model_includes: options[:model_includes], | |
112 | 112 | json: !@json.nil?, |
113 | 113 | match_suffix: @match_suffix, |
114 | 114 | highlighted_fields: @highlighted_fields || [], | ... | ... |
lib/searchkick/results.rb
... | ... | @@ -198,13 +198,10 @@ 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_model]) | |
202 | - | |
203 | - | |
201 | + if options[:includes] || options[:model_includes] | |
204 | 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 | 206 | records = |
210 | 207 | if defined?(NoBrainer::Document) && records < NoBrainer::Document |
... | ... | @@ -221,6 +218,16 @@ module Searchkick |
221 | 218 | Searchkick.load_records(records, ids) |
222 | 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 | 231 | def base_field(k) |
225 | 232 | k.sub(/\.(analyzed|word_start|word_middle|word_end|text_start|text_middle|text_end|exact)\z/, "") |
226 | 233 | end | ... | ... |
test/sql_test.rb
... | ... | @@ -196,19 +196,19 @@ class SqlTest < Minitest::Test |
196 | 196 | assert Product.search("product", includes: [:store]).first.association(:store).loaded? |
197 | 197 | end |
198 | 198 | |
199 | - def test_includes_per_model | |
199 | + def test_model_includes | |
200 | 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 | 208 | assert_equal 2, result.length |
209 | 209 | |
210 | 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 | 212 | end |
213 | 213 | end |
214 | 214 | end | ... | ... |
test/test_helper.rb