Skip to content

Commit

Permalink
Added support for endless ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Jan 23, 2019
1 parent 2ca8cbd commit e50570a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 3.1.3 [unreleased]

- Added support for endless ranges
- Added `prefix` to `where`
- Fixed error with some language stemmers and Elasticsearch 6.5

Expand Down
7 changes: 6 additions & 1 deletion lib/searchkick/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,12 @@ def where_filters(where)
else
# expand ranges
if value.is_a?(Range)
value = {gte: value.first, (value.exclude_end? ? :lt : :lte) => value.last}
# infinite? added in Ruby 2.4
if value.end.nil? || (value.end.respond_to?(:infinite?) && value.end.infinite?)
value = {gte: value.first}
else
value = {gte: value.first, (value.exclude_end? ? :lt : :lte) => value.last}
end
end

value = {in: value} if value.is_a?(Array)
Expand Down
5 changes: 5 additions & 0 deletions test/where_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ def test_where
assert_search "product", ["Product C", "Product D"], where: {store_id: {not: [1, 2]}}
assert_search "product", ["Product C", "Product D"], where: {store_id: {_not: [1, 2]}}
assert_search "product", ["Product A"], where: {user_ids: {lte: 2, gte: 2}}
assert_search "product", ["Product C", "Product D"], where: {store_id: 3..Float::INFINITY}
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.6.0")
# use eval to prevent parse error
assert_search "product", ["Product C", "Product D"], where: {store_id: 3..eval("0..")}
end

# or
assert_search "product", ["Product A", "Product B", "Product C"], where: {or: [[{in_stock: true}, {store_id: 3}]]}
Expand Down

0 comments on commit e50570a

Please sign in to comment.