Skip to content

Commit

Permalink
Replaced script with raw
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Jul 11, 2024
1 parent 4ce7be6 commit 8db72b4
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 24 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## 5.4.0 (unreleased)

- Added support for scripting to `where` option
- Added experimental support for scripting to `where` option
- Added warning for `exists` with non-`true` values
- Added warning for full reindex and `:queue` mode
- Dropped support for Ruby < 3.1
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ where: {
_not: {store_id: 1}, # negate a condition
_or: [{in_stock: true}, {backordered: true}],
_and: [{in_stock: true}, {backordered: true}],
_script: Searchkick.script("doc['a'].value > doc['b'].value") # [unreleased]
_script: Searchkick.raw("doc['a'].value > doc['b'].value") # experimental [unreleased]
}
```

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

Expand Down Expand Up @@ -190,10 +190,11 @@ def self.multi_search(queries)
end
end

# script
# raw

def self.script(source, **options)
Script.new(source, **options)
# experimental
def self.raw(value)
Raw.new(value)
end

# callbacks
Expand Down
6 changes: 3 additions & 3 deletions lib/searchkick/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -910,11 +910,11 @@ def where_filters(where)
elsif field == :_and
filters << {bool: {must: value.map { |or_statement| {bool: {filter: where_filters(or_statement)}} }}}
elsif field == :_script
unless value.is_a?(Script)
raise TypeError, "expected Searchkick::Script"
unless value.is_a?(Raw)
raise TypeError, "expected Searchkick::Raw"
end

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

def initialize(value)
@value = value
end
end
end
11 changes: 0 additions & 11 deletions lib/searchkick/script.rb

This file was deleted.

8 changes: 4 additions & 4 deletions test/where_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,16 +261,16 @@ def test_script
{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})}
assert_search "product", ["Product A"], where: {_script: Searchkick.raw("doc['store_id'].value < 10")}
assert_search "product", ["Product A"], where: {_script: Searchkick.raw({source: "doc['store_id'].value < 10", lang: "expression"})}
assert_search "product", ["Product A"], where: {_script: Searchkick.raw({source: "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
assert_equal "expected Searchkick::Raw", error.message
end

def test_where_string
Expand Down

0 comments on commit 8db72b4

Please sign in to comment.