Commit 66a39aafd7ef90269de05325647585a3672c12dc

Authored by Andrew Kane
1 parent afcdd876

Fixed reversed coordinates and added bounded box queries

README.md
... ... @@ -367,6 +367,12 @@ Reindex and search with:
367 367 City.search "san", where: {location: {near: [37, -114], within: "100mi"}} # or 160km
368 368 ```
369 369  
  370 +Bounded by a box [master]
  371 +
  372 +```ruby
  373 +City.search "san", where: {location: {top_right: [38, -123], bottom_right: [37, -122]}}
  374 +```
  375 +
370 376 ## Deployment
371 377  
372 378 Searchkick uses `ENV["ELASTICSEARCH_URL"]` for the Elasticsearch server. This defaults to `http://localhost:9200`.
... ...
lib/searchkick/model.rb
... ... @@ -43,6 +43,11 @@ module Searchkick
43 43 source[field] = "a" if !source[field]
44 44 end
45 45  
  46 + # locations
  47 + (options[:locations] || []).map(&:to_s).each do |field|
  48 + source[field] = source[field].reverse if source[field]
  49 + end
  50 +
46 51 source.to_json
47 52 end
48 53 end
... ...
lib/searchkick/search.rb
... ... @@ -177,12 +177,23 @@ module Searchkick
177 177 if value[:near]
178 178 filters << {
179 179 geo_distance: {
180   - field => value.delete(:near),
  180 + field => value.delete(:near).reverse,
181 181 distance: value.delete(:within) || "50mi"
182 182 }
183 183 }
184 184 end
185 185  
  186 + if value[:top_left]
  187 + filters << {
  188 + geo_bounding_box: {
  189 + field => {
  190 + top_left: value.delete(:top_left).reverse,
  191 + bottom_right: value.delete(:bottom_right).reverse
  192 + }
  193 + }
  194 + }
  195 + end
  196 +
186 197 value.each do |op, op_value|
187 198 if op == :not # not equal
188 199 if op_value.is_a?(Array)
... ...
test/sql_test.rb
... ... @@ -77,7 +77,7 @@ class TestSql &lt; Minitest::Unit::TestCase
77 77 {name: "San Francisco", latitude: 37.7833, longitude: -122.4167},
78 78 {name: "San Antonio", latitude: 29.4167, longitude: -98.5000}
79 79 ]
80   - assert_search "san", ["San Francisco"], where: {location: {near: [37, -122]}}
  80 + assert_search "san", ["San Francisco"], where: {location: {near: [37.5, -122.5]}}
81 81 end
82 82  
83 83 def test_near_within
... ... @@ -89,6 +89,14 @@ class TestSql &lt; Minitest::Unit::TestCase
89 89 assert_search "san", ["San Francisco", "San Antonio"], where: {location: {near: [37, -122], within: "2000mi"}}
90 90 end
91 91  
  92 + def test_top_left_bottom_right
  93 + store [
  94 + {name: "San Francisco", latitude: 37.7833, longitude: -122.4167},
  95 + {name: "San Antonio", latitude: 29.4167, longitude: -98.5000}
  96 + ]
  97 + assert_search "san", ["San Francisco"], where: {location: {top_left: [38, -123], bottom_right: [37, -122]}}
  98 + end
  99 +
92 100 def test_order_hash
93 101 store_names ["Product A", "Product B", "Product C", "Product D"]
94 102 assert_order "product", ["Product D", "Product C", "Product B", "Product A"], order: {name: :desc}
... ...