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 8db72b4 commit f395435
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 9 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 experimental support for scripting to `where` option
- Added experimental support for `_raw` 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
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ 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}],
_script: Searchkick.raw("doc['a'].value > doc['b'].value") # experimental [unreleased]
_and: [{in_stock: true}, {backordered: true}]
}
```

Expand Down
4 changes: 2 additions & 2 deletions lib/searchkick/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -909,12 +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
elsif field == :_raw
unless value.is_a?(Raw)
raise TypeError, "expected Searchkick::Raw"
end

filters << {script: {script: value.value}}
filters << value.value
else
# expand ranges
if value.is_a?(Range)
Expand Down
8 changes: 4 additions & 4 deletions test/where_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,14 @@ def test_script
{name: "Product A", store_id: 1},
{name: "Product B", store_id: 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}})}
assert_search "product", ["Product A"], where: {_raw: Searchkick.raw({script: {script: "doc['store_id'].value < 10"}})}
assert_search "product", ["Product A"], where: {_raw: Searchkick.raw({script: {script: {source: "doc['store_id'].value < 10", lang: "expression"}}})}
assert_search "product", ["Product A"], where: {_raw: Searchkick.raw({script: {script: {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"}
assert_search "product", ["Product A"], where: {_raw: {script: {script: "doc['store_id'].value < 10"}}}
end
assert_equal "expected Searchkick::Raw", error.message
end
Expand Down

0 comments on commit f395435

Please sign in to comment.