From cb63edde993b7b74f294ce4d65ad9ec7b877d316 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Fri, 8 Sep 2017 19:25:40 -0700 Subject: [PATCH] Changed option name to model includes --- CHANGELOG.md | 1 + README.md | 5 ++--- lib/searchkick/query.rb | 6 +++--- lib/searchkick/results.rb | 19 +++++++++++++------ test/sql_test.rb | 12 ++++++------ test/test_helper.rb | 1 - 6 files changed, 25 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3665854..c445bb9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ - Added `_all` and `default_fields` options - Added global `index_prefix` option - Added `wait` option to async reindex +- Added `model_includes` option - Raise error for `reindex_status` when Redis not configured ## 2.3.1 diff --git a/README.md b/README.md index 24e9734..ac713b7 100644 --- a/README.md +++ b/README.md @@ -1636,12 +1636,11 @@ Eager load associations Product.search "milk", includes: [:brand, :stores] ``` -Load associations per model +Eager load different associations by model [master] ```ruby -Searchkick.search("*", index_name: [Product.search_index.name, Store.search_index.name], includes_per_model: {Product => :store, Store => :product}) +Searchkick.search("*", index_name: [Product, Store], model_includes: {Product => [:store], Store => [:product]}) ``` -These 2 options above can be combined, but you should make sure that associations specified by `includes` present in all searched models Turn off special characters diff --git a/lib/searchkick/query.rb b/lib/searchkick/query.rb index b28c3a2..6c72932 100644 --- a/lib/searchkick/query.rb +++ b/lib/searchkick/query.rb @@ -17,8 +17,8 @@ module Searchkick def initialize(klass, term = "*", **options) unknown_keywords = options.keys - [:aggs, :body, :body_options, :boost, :boost_by, :boost_by_distance, :boost_where, :conversions, :conversions_term, :debug, :emoji, :exclude, :execute, :explain, - :fields, :highlight, :includes, :includes_per_model, :index_name, :indices_boost, :limit, :load, - :match, :misspellings, :offset, :operator, :order, :padding, :page, :per_page, :profile, + :fields, :highlight, :includes, :index_name, :indices_boost, :limit, :load, + :match, :misspellings, :model_includes, :offset, :operator, :order, :padding, :page, :per_page, :profile, :request_params, :routing, :select, :similar, :smart_aggs, :suggest, :track, :type, :where] raise ArgumentError, "unknown keywords: #{unknown_keywords.join(", ")}" if unknown_keywords.any? @@ -108,7 +108,7 @@ module Searchkick padding: @padding, load: @load, includes: options[:includes], - includes_per_model: options[:includes_per_model], + model_includes: options[:model_includes], json: !@json.nil?, match_suffix: @match_suffix, highlighted_fields: @highlighted_fields || [], diff --git a/lib/searchkick/results.rb b/lib/searchkick/results.rb index f9c4649..375c752 100644 --- a/lib/searchkick/results.rb +++ b/lib/searchkick/results.rb @@ -198,13 +198,10 @@ module Searchkick def results_query(records, hits) ids = hits.map { |hit| hit["_id"] } - if (options[:includes] || options[:includes_per_model]) - - + if options[:includes] || options[:model_includes] included_relations = [] - - included_relations << options[:includes] if options[:includes] - included_relations << options[:includes_per_model][records] if (options[:includes_per_model] && options[:includes_per_model][records]) + combine_includes(included_relations, options[:includes]) + combine_includes(included_relations, options[:model_includes][records]) if options[:model_includes] records = if defined?(NoBrainer::Document) && records < NoBrainer::Document @@ -221,6 +218,16 @@ module Searchkick Searchkick.load_records(records, ids) end + def combine_includes(result, inc) + if inc + if inc.is_a?(Array) + result.concat(inc) + else + result << inc + end + end + end + def base_field(k) k.sub(/\.(analyzed|word_start|word_middle|word_end|text_start|text_middle|text_end|exact)\z/, "") end diff --git a/test/sql_test.rb b/test/sql_test.rb index 940b70f..f334336 100644 --- a/test/sql_test.rb +++ b/test/sql_test.rb @@ -196,19 +196,19 @@ class SqlTest < Minitest::Test assert Product.search("product", includes: [:store]).first.association(:store).loaded? end - def test_includes_per_model + def test_model_includes skip unless defined?(ActiveRecord) - store_names ["Product A"] - store_names ['Product A Store'], Store - associations = { Product => :store, Store => :products } + store_names ["Product A"] + store_names ["Store A"], Store - result = Searchkick.search("Product", fields: ['name'], index_name: [Product.search_index.name, Store.search_index.name], includes_per_model: associations) + associations = {Product => [:store], Store => [:products]} + result = Searchkick.search("*", index_name: [Product, Store], model_includes: associations) assert_equal 2, result.length result.group_by(&:class).each_pair do |klass, records| - assert records.first.association(associations[klass]).loaded? + assert records.first.association(associations[klass].first).loaded? end end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 4aa2a45..8de1368 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -450,7 +450,6 @@ class Store searchkick \ default_fields: elasticsearch_below60? ? nil : [:name], routing: true, - searchable: [:name], merge_mappings: true, mappings: { store: { -- libgit2 0.21.0