Skip to content

Commit

Permalink
Added support for scripting to where option - closes #1600 and closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Jun 17, 2024
1 parent aea85bf commit d5869a4
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 5.4.0 (unreleased)

- Added support for scripting to `where` option
- Added warning for full reindex and `:queue` mode
- Dropped support for Ruby < 3.1

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ where: {
store_id: {exists: true}, # exists
_not: {store_id: 1}, # negate a condition
_or: [{in_stock: true}, {backordered: true}],
_and: [{in_stock: true}, {backordered: true}]
_and: [{in_stock: true}, {backordered: true}],
_script: Searchkick.script("doc['a'].value > doc['b'].value") # [unreleased]
}
```

Expand Down
7 changes: 7 additions & 0 deletions lib/searchkick.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
require_relative "searchkick/relation"
require_relative "searchkick/relation_indexer"
require_relative "searchkick/results"
require_relative "searchkick/script"
require_relative "searchkick/version"
require_relative "searchkick/where"

Expand Down Expand Up @@ -189,6 +190,12 @@ def self.multi_search(queries)
end
end

# script

def self.script(source, **options)
Script.new(source, **options)
end

# callbacks

def self.enable_callbacks
Expand Down
8 changes: 6 additions & 2 deletions lib/searchkick/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -909,8 +909,12 @@ def where_filters(where)
filters << {bool: {must_not: where_filters(value)}}
elsif field == :_and
filters << {bool: {must: value.map { |or_statement| {bool: {filter: where_filters(or_statement)}} }}}
# elsif field == :_script
# filters << {script: {script: {source: value, lang: "painless"}}}
elsif field == :_script
unless value.is_a?(Script)
raise TypeError, "expected Searchkick::Script"
end

filters << {script: {script: {source: value.source, lang: value.lang, params: value.params}}}
else
# expand ranges
if value.is_a?(Range)
Expand Down
11 changes: 11 additions & 0 deletions lib/searchkick/script.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Searchkick
class Script
attr_reader :source, :lang, :params

def initialize(source, lang: "painless", params: {})
@source = source
@lang = lang
@params = params
end
end
end
23 changes: 16 additions & 7 deletions test/where_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,22 @@ def test_ilike_optional_operators
assert_search "product", ["Product @Home"], where: {name: {ilike: "%@home%"}}
end

# def test_script
# store [
# {name: "Product A", store_id: 1},
# {name: "Product B", store_id: 10}
# ]
# assert_search "product", ["Product A"], where: {_script: "doc['store_id'].value < 10"}
# end
def test_script
store [
{name: "Product A", store_id: 1},
{name: "Product B", store_id: 10}
]
assert_search "product", ["Product A"], where: {_script: Searchkick.script("doc['store_id'].value < 10")}
assert_search "product", ["Product A"], where: {_script: Searchkick.script("doc['store_id'].value < 10", lang: "expression")}
assert_search "product", ["Product A"], where: {_script: Searchkick.script("doc['store_id'].value < params['value']", params: {value: 10})}
end

def test_script_string
error = assert_raises(TypeError) do
assert_search "product", ["Product A"], where: {_script: "doc['store_id'].value < 10"}
end
assert_equal "expected Searchkick::Script", error.message
end

def test_where_string
store [
Expand Down

0 comments on commit d5869a4

Please sign in to comment.