Commit 0fc0957223e7f4bd729e6edcf613c9836096ee0b
1 parent
94581e53
Exists in
master
and in
1 other branch
Added order and reorder methods on relation
Showing
3 changed files
with
42 additions
and
0 deletions
Show diff stats
lib/searchkick/relation.rb
... | ... | @@ -117,6 +117,37 @@ module Searchkick |
117 | 117 | end |
118 | 118 | |
119 | 119 | # experimental |
120 | + def order(value) | |
121 | + clone.order!(value) | |
122 | + end | |
123 | + | |
124 | + # experimental | |
125 | + def order!(value) | |
126 | + check_loaded | |
127 | + if @options[:order] | |
128 | + order = @options[:order] | |
129 | + order = [order] unless order.is_a?(Array) | |
130 | + order << value | |
131 | + @options[:order] = order | |
132 | + else | |
133 | + @options[:order] = value | |
134 | + end | |
135 | + self | |
136 | + end | |
137 | + | |
138 | + # experimental | |
139 | + def reorder(value) | |
140 | + clone.reorder!(value) | |
141 | + end | |
142 | + | |
143 | + # experimental | |
144 | + def reorder!(value) | |
145 | + check_loaded | |
146 | + @options[:order] = value | |
147 | + self | |
148 | + end | |
149 | + | |
150 | + # experimental | |
120 | 151 | def only(*keys) |
121 | 152 | Relation.new(@model, @term, **@options.slice(*keys)) |
122 | 153 | end | ... | ... |
test/order_test.rb
... | ... | @@ -4,11 +4,13 @@ class OrderTest < Minitest::Test |
4 | 4 | def test_hash |
5 | 5 | store_names ["Product A", "Product B", "Product C", "Product D"] |
6 | 6 | assert_order "product", ["Product D", "Product C", "Product B", "Product A"], order: {name: :desc} |
7 | + assert_order_relation ["Product D", "Product C", "Product B", "Product A"], Product.search("product").order(name: :desc) | |
7 | 8 | end |
8 | 9 | |
9 | 10 | def test_string |
10 | 11 | store_names ["Product A", "Product B", "Product C", "Product D"] |
11 | 12 | assert_order "product", ["Product A", "Product B", "Product C", "Product D"], order: "name" |
13 | + assert_order_relation ["Product A", "Product B", "Product C", "Product D"], Product.search("product").order("name") | |
12 | 14 | end |
13 | 15 | |
14 | 16 | def test_multiple |
... | ... | @@ -18,15 +20,20 @@ class OrderTest < Minitest::Test |
18 | 20 | {name: "Product C", color: "red", store_id: 2} |
19 | 21 | ] |
20 | 22 | assert_order "product", ["Product A", "Product B", "Product C"], order: {color: :asc, store_id: :desc} |
23 | + assert_order_relation ["Product A", "Product B", "Product C"], Product.search("product").order(color: :asc, store_id: :desc) | |
24 | + assert_order_relation ["Product A", "Product B", "Product C"], Product.search("product").order(color: :asc).order(store_id: :desc) | |
25 | + assert_order_relation ["Product B", "Product C", "Product A"], Product.search("product").order(color: :asc).reorder(store_id: :desc) | |
21 | 26 | end |
22 | 27 | |
23 | 28 | def test_unmapped_type |
24 | 29 | Product.search_index.refresh |
25 | 30 | assert_order "product", [], order: {not_mapped: {unmapped_type: "long"}} |
31 | + assert_order_relation [], Product.search("product").order(not_mapped: {unmapped_type: "long"}) | |
26 | 32 | end |
27 | 33 | |
28 | 34 | def test_array |
29 | 35 | store [{name: "San Francisco", latitude: 37.7833, longitude: -122.4167}] |
30 | 36 | assert_order "francisco", ["San Francisco"], order: [{_geo_distance: {location: "0,0"}}] |
37 | + assert_order_relation ["San Francisco"], Product.search("francisco").order([{_geo_distance: {location: "0,0"}}]) | |
31 | 38 | end |
32 | 39 | end | ... | ... |
test/support/helpers.rb
... | ... | @@ -61,6 +61,10 @@ class Minitest::Test |
61 | 61 | assert_equal expected, model.search(term, **options).map(&:name) |
62 | 62 | end |
63 | 63 | |
64 | + def assert_order_relation(expected, relation) | |
65 | + assert_equal expected, relation.map(&:name) | |
66 | + end | |
67 | + | |
64 | 68 | def assert_equal_scores(term, options = {}, model = default_model) |
65 | 69 | assert_equal 1, model.search(term, **options).hits.map { |a| a["_score"] }.uniq.size |
66 | 70 | end | ... | ... |