diff --git a/README.md b/README.md index 5ab60f7..2268364 100644 --- a/README.md +++ b/README.md @@ -1442,10 +1442,8 @@ products = Product.search("milk").body_options(min_score: 1) or ```ruby -products = - Product.search("apples") do |body| - body[:min_score] = 1 - end +products = Product.search("apples") +products.body[:min_score] = 1 ``` ### Elasticsearch Gem diff --git a/lib/searchkick/relation.rb b/lib/searchkick/relation.rb index b08cbf5..48a542c 100644 --- a/lib/searchkick/relation.rb +++ b/lib/searchkick/relation.rb @@ -11,7 +11,7 @@ module Searchkick :offset_value, :previous_page, :prev_page, :next_page, :first_page?, :last_page?, :out_of_range?, :hits, :response, :to_a, :first, :highlights, :group_by, :misspellings?, :with_highlights - def_delegators :query, :body, :params + def_delegators :query, :params def initialize(klass, term = "*", **options) unknown_keywords = options.keys - [:aggs, :block, :body, :body_options, :boost, @@ -298,6 +298,19 @@ module Searchkick self end + def body(*args) + if args.empty? + params.body + else + spawn.body!(*args) + end + end + + def body!(value) + options[:body] = value + self + end + def body_options(value) spawn.body_options!(value) end diff --git a/test/index_test.rb b/test/index_test.rb index 65a59e6..f13f146 100644 --- a/test/index_test.rb +++ b/test/index_test.rb @@ -58,6 +58,7 @@ class IndexTest < Minitest::Test def test_body store_names ["Dollar Tree"], Store assert_equal ["Dollar Tree"], Store.search(body: {query: {match: {name: "dollar"}}}, load: false).map(&:name) + assert_equal ["Dollar Tree"], Store.search.body(query: {match: {name: "dollar"}}).load(false).map(&:name) end def test_body_incompatible_options -- libgit2 0.21.0