From 6916ac83037f3743132c583619c03b681ef3cbab Mon Sep 17 00:00:00 2001 From: Eric Harrison Date: Sun, 28 Jan 2018 14:59:14 -0700 Subject: [PATCH] Added in top_right and bottom_left for posterity (#1064) --- README.md | 4 ++++ lib/searchkick/query.rb | 11 ++++++++++- test/where_test.rb | 16 ++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6355e4d..607826b 100644 --- a/README.md +++ b/README.md @@ -900,6 +900,10 @@ Bounded by a box ```ruby Restaurant.search "sushi", where: {location: {top_left: {lat: 38, lon: -123}, bottom_right: {lat: 37, lon: -122}}} ``` +OR +```ruby +Restaurant.search "sushi", where: {location: {top_right: {lat: 38, lon: -122}, bottom_left: {lat: 37, lon: -123}}} +``` Bounded by a polygon diff --git a/lib/searchkick/query.rb b/lib/searchkick/query.rb index 34b8f75..6f1ea0a 100644 --- a/lib/searchkick/query.rb +++ b/lib/searchkick/query.rb @@ -770,7 +770,7 @@ module Searchkick if value.is_a?(Hash) value.each do |op, op_value| case op - when :within, :bottom_right + when :within, :bottom_right, :bottom_left # do nothing when :near filters << { @@ -805,6 +805,15 @@ module Searchkick } } } + when :top_right + filters << { + geo_bounding_box: { + field => { + top_right: location_value(op_value), + bottom_left: location_value(value[:bottom_left]) + } + } + } when :regexp # support for regexp queries without using a regexp ruby object filters << {regexp: {field => {value: op_value}}} when :not # not equal diff --git a/test/where_test.rb b/test/where_test.rb index 378e455..1e69b55 100644 --- a/test/where_test.rb +++ b/test/where_test.rb @@ -189,6 +189,22 @@ class WhereTest < Minitest::Test assert_search "san", ["San Francisco"], where: {location: {top_left: {lat: 38, lon: -123}, bottom_right: {lat: 37, lon: -122}}} end + def test_top_right_bottom_left + store [ + {name: "San Francisco", latitude: 37.7833, longitude: -122.4167}, + {name: "San Antonio", latitude: 29.4167, longitude: -98.5000} + ] + assert_search "san", ["San Francisco"], where: {location: {top_right: [38, -122], bottom_left: [37, -123]}} + end + + def test_top_right_bottom_left_hash + store [ + {name: "San Francisco", latitude: 37.7833, longitude: -122.4167}, + {name: "San Antonio", latitude: 29.4167, longitude: -98.5000} + ] + assert_search "san", ["San Francisco"], where: {location: {top_right: {lat: 38, lon: -122}, bottom_left: {lat: 37, lon: -123}}} + end + def test_multiple_locations store [ {name: "San Francisco", latitude: 37.7833, longitude: -122.4167}, -- libgit2 0.21.0