Commit c2128652bd6abf2618a8c94838207c98f35febc9
Exists in
master
and in
21 other branches
Merge branch 'meetrajesh-raj_fix_sql_select'
Showing
4 changed files
with
68 additions
and
4 deletions
Show diff stats
lib/searchkick/model.rb
... | ... | @@ -73,7 +73,7 @@ module Searchkick |
73 | 73 | callback_name = callbacks == :async ? :reindex_async : :reindex |
74 | 74 | if respond_to?(:after_commit) |
75 | 75 | after_commit callback_name, if: proc { self.class.search_callbacks? } |
76 | - else | |
76 | + elsif respond_to?(:after_save) | |
77 | 77 | after_save callback_name, if: proc { self.class.search_callbacks? } |
78 | 78 | after_destroy callback_name, if: proc { self.class.search_callbacks? } |
79 | 79 | end | ... | ... |
lib/searchkick/query.rb
... | ... | @@ -567,10 +567,18 @@ module Searchkick |
567 | 567 | end |
568 | 568 | |
569 | 569 | # An empty array will cause only the _id and _type for each hit to be returned |
570 | - # http://www.elasticsearch.org/guide/reference/api/search/fields/ | |
570 | + # doc for :select - http://www.elasticsearch.org/guide/reference/api/search/fields/ | |
571 | + # doc for :select_v2 - https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-source-filtering.html | |
571 | 572 | if options[:select] |
572 | 573 | payload[:fields] = options[:select] if options[:select] != true |
574 | + elsif options[:select_v2] | |
575 | + if options[:select_v2] == [] | |
576 | + payload[:fields] = [] # intuitively [] makes sense to return no fields, but ES by default returns all fields | |
577 | + else | |
578 | + payload[:_source] = options[:select_v2] | |
579 | + end | |
573 | 580 | elsif load |
581 | + # don't need any fields since we're going to load them from the DB anyways | |
574 | 582 | payload[:fields] = [] |
575 | 583 | end |
576 | 584 | |
... | ... | @@ -650,7 +658,7 @@ module Searchkick |
650 | 658 | when :lte |
651 | 659 | {to: op_value, include_upper: true} |
652 | 660 | else |
653 | - raise "Unknown where operator" | |
661 | + raise "Unknown where operator: #{op.inspect}" | |
654 | 662 | end |
655 | 663 | # issue 132 |
656 | 664 | if (existing = filters.find { |f| f[:range] && f[:range][field] }) | ... | ... |
lib/searchkick/results.rb
test/sql_test.rb
... | ... | @@ -81,6 +81,7 @@ class SqlTest < Minitest::Test |
81 | 81 | result = Product.search("product", load: false, select: [:name, :store_id]).first |
82 | 82 | assert_equal %w(id name store_id), result.keys.reject { |k| k.start_with?("_") }.sort |
83 | 83 | assert_equal ["Product A"], result.name # this is not great |
84 | + assert_equal [1], result.store_id | |
84 | 85 | end |
85 | 86 | |
86 | 87 | def test_select_array |
... | ... | @@ -89,6 +90,14 @@ class SqlTest < Minitest::Test |
89 | 90 | assert_equal [1, 2], result.user_ids |
90 | 91 | end |
91 | 92 | |
93 | + def test_select_single_field | |
94 | + store [{name: "Product A", store_id: 1}] | |
95 | + result = Product.search("product", load: false, select: :name).first | |
96 | + assert_equal %w(id name), result.keys.reject { |k| k.start_with?("_") }.sort | |
97 | + assert_equal ["Product A"], result.name | |
98 | + assert_nil result.store_id | |
99 | + end | |
100 | + | |
92 | 101 | def test_select_all |
93 | 102 | store [{name: "Product A", user_ids: [1, 2]}] |
94 | 103 | hit = Product.search("product", select: true).hits.first |
... | ... | @@ -96,6 +105,51 @@ class SqlTest < Minitest::Test |
96 | 105 | assert_equal hit["_source"]["user_ids"], [1, 2] |
97 | 106 | end |
98 | 107 | |
108 | + def test_select_none | |
109 | + store [{name: "Product A", user_ids: [1, 2]}] | |
110 | + hit = Product.search("product", select: []).hits.first | |
111 | + assert_nil hit["_source"] | |
112 | + end | |
113 | + | |
114 | + # select_v2 | |
115 | + | |
116 | + def test_select_v2 | |
117 | + store [{name: "Product A", store_id: 1}] | |
118 | + result = Product.search("product", load: false, select_v2: [:name, :store_id]).first | |
119 | + assert_equal %w(id name store_id), result.keys.reject { |k| k.start_with?("_") }.sort | |
120 | + assert_equal "Product A", result.name | |
121 | + assert_equal 1, result.store_id | |
122 | + end | |
123 | + | |
124 | + def test_select_v2_array | |
125 | + store [{name: "Product A", user_ids: [1, 2]}] | |
126 | + result = Product.search("product", load: false, select_v2: [:user_ids]).first | |
127 | + assert_equal [1, 2], result.user_ids | |
128 | + end | |
129 | + | |
130 | + def test_select_v2_single_field | |
131 | + store [{name: "Product A", store_id: 1}] | |
132 | + result = Product.search("product", load: false, select_v2: :name).first | |
133 | + assert_equal %w(id name), result.keys.reject { |k| k.start_with?("_") }.sort | |
134 | + assert_equal "Product A", result.name | |
135 | + assert_nil result.store_id | |
136 | + end | |
137 | + | |
138 | + def test_select_v2_all | |
139 | + store [{name: "Product A", user_ids: [1, 2]}] | |
140 | + hit = Product.search("product", select_v2: true).hits.first | |
141 | + assert_equal hit["_source"]["name"], "Product A" | |
142 | + assert_equal hit["_source"]["user_ids"], [1, 2] | |
143 | + end | |
144 | + | |
145 | + def test_select_v2_none | |
146 | + store [{name: "Product A", user_ids: [1, 2]}] | |
147 | + hit = Product.search("product", select_v2: []).hits.first | |
148 | + assert_nil hit["_source"] | |
149 | + end | |
150 | + | |
151 | + # other tests | |
152 | + | |
99 | 153 | def test_nested_object |
100 | 154 | aisle = {"id" => 1, "name" => "Frozen"} |
101 | 155 | store [{name: "Product A", aisle: aisle}] | ... | ... |