Skip to content

Commit

Permalink
Restored _script
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Sep 3, 2024
1 parent 3441a57 commit bf4d4e7
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 23 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

- Added experimental `knn` option
- Added experimental `rrf` method
- Added experimental support for `_raw` 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
- Fixed `per_page` method when paginating beyond `max_result_window`
Expand Down
8 changes: 4 additions & 4 deletions lib/searchkick.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
require_relative "searchkick/relation_indexer"
require_relative "searchkick/reranking"
require_relative "searchkick/results"
require_relative "searchkick/raw"
require_relative "searchkick/script"
require_relative "searchkick/version"
require_relative "searchkick/where"

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

# raw
# script

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

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

filters << value.value
filters << {script: {script: {source: value.source, lang: value.lang, params: value.params}}}
else
# expand ranges
if value.is_a?(Range)
Expand Down
9 changes: 0 additions & 9 deletions lib/searchkick/raw.rb

This file was deleted.

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
10 changes: 5 additions & 5 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: {_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}}}})}
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: {_raw: {script: {script: "doc['store_id'].value < 10"}}}
assert_search "product", ["Product A"], where: {_script: "doc['store_id'].value < 10"}
end
assert_equal "Use Searchkick.raw for raw filters", error.message
assert_equal "expected Searchkick::Script", error.message
end

def test_where_string
Expand Down

0 comments on commit bf4d4e7

Please sign in to comment.