Skip to content

Commit

Permalink
add support for conjunctions with bounding box
Browse files Browse the repository at this point in the history
Allow `with().in_bounding_box()` restrictions to be added to conjunctions
such as `any_of`, e.g.

    Model.search do
      any_of do
        with(:city, city)
        with(:location).in_bounding_box(se, nw)
      end
    end
  • Loading branch information
Jan Hecking authored and bragboy committed Jan 21, 2017
1 parent 8632576 commit 1c6351f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
6 changes: 5 additions & 1 deletion sunspot/lib/sunspot/query/bbox.rb
Expand Up @@ -5,8 +5,12 @@ def initialize(field, first_corner, second_corner)
@field, @first_corner, @second_corner = field, first_corner, second_corner
end

def to_solr_conditional
"[#{@first_corner.join(",")} TO #{@second_corner.join(",")}]"
end

def to_params
filter = "#{@field.indexed_name}:[#{@first_corner.join(",")} TO #{@second_corner.join(",")}]"
filter = "#{@field.indexed_name}:#{to_solr_conditional}"

{:fq => filter}
end
Expand Down
11 changes: 11 additions & 0 deletions sunspot/lib/sunspot/query/restriction.rb
Expand Up @@ -169,6 +169,17 @@ def to_positive_boolean_phrase
end
end

class InBoundingBox < Base
def initialize(negated, field, first_corner, second_corner)
@bbox = Sunspot::Query::Bbox.new(field, first_corner, second_corner)
super negated, field, [first_corner, second_corner]
end

def to_solr_conditional
@bbox.to_solr_conditional
end
end

#
# Results must have field with value equal to given value. If the value
# is nil, results must have no value for the given field.
Expand Down
12 changes: 12 additions & 0 deletions sunspot/spec/api/query/connectives_examples.rb
Expand Up @@ -198,4 +198,16 @@
:fq, '(_query_:"{!geofilt sfield=coordinates_new_ll pt=23,-46 d=100}" OR _query_:"{!geofilt sfield=coordinates_new_ll pt=42,56 d=50}")'
)
end

it 'creates a conjunction of in_bounding_box queries' do
search do
any_of do
with(:coordinates_new).in_bounding_box([23, -46], [25, -44])
with(:coordinates_new).in_bounding_box([42, 56], [43, 58])
end
end
connection.should have_last_search_including(
:fq, '(coordinates_new_ll:[23,-46 TO 25,-44] OR coordinates_new_ll:[42,56 TO 43,58])'
)
end
end
13 changes: 12 additions & 1 deletion sunspot/spec/integration/geospatial_spec.rb
Expand Up @@ -26,7 +26,7 @@
results.should_not include(@post)
end

it "allows conjunction queries" do
it "allows conjunction queries with radius" do
results = Sunspot.search(Post) {
any_of do
with(:coordinates_new).in_radius(32, -68, 1)
Expand All @@ -36,6 +36,17 @@

results.should include(@post)
end

it "allows conjunction queries with bounding box" do
results = Sunspot.search(Post) {
any_of do
with(:coordinates_new).in_bounding_box([31, -69], [33, -67])
with(:coordinates_new).in_bounding_box([35, 68], [36, 69])
end
}.results

results.should include(@post)
end
end

describe "filtering by bounding box" do
Expand Down

0 comments on commit 1c6351f

Please sign in to comment.