Commit 34ee92e0427234ad46e1fef59ae1414b032e3d9e

Authored by Andrew Kane
1 parent a55e01bf
Exists in relation

Added support for body [skip ci]

@@ -1442,10 +1442,8 @@ products = Product.search("milk").body_options(min_score: 1) @@ -1442,10 +1442,8 @@ products = Product.search("milk").body_options(min_score: 1)
1442 or 1442 or
1443 1443
1444 ```ruby 1444 ```ruby
1445 -products =  
1446 - Product.search("apples") do |body|  
1447 - body[:min_score] = 1  
1448 - end 1445 +products = Product.search("apples")
  1446 +products.body[:min_score] = 1
1449 ``` 1447 ```
1450 1448
1451 ### Elasticsearch Gem 1449 ### Elasticsearch Gem
lib/searchkick/relation.rb
@@ -11,7 +11,7 @@ module Searchkick @@ -11,7 +11,7 @@ module Searchkick
11 :offset_value, :previous_page, :prev_page, :next_page, :first_page?, :last_page?, 11 :offset_value, :previous_page, :prev_page, :next_page, :first_page?, :last_page?,
12 :out_of_range?, :hits, :response, :to_a, :first, :highlights, :group_by, :misspellings?, :with_highlights 12 :out_of_range?, :hits, :response, :to_a, :first, :highlights, :group_by, :misspellings?, :with_highlights
13 13
14 - def_delegators :query, :body, :params 14 + def_delegators :query, :params
15 15
16 def initialize(klass, term = "*", **options) 16 def initialize(klass, term = "*", **options)
17 unknown_keywords = options.keys - [:aggs, :block, :body, :body_options, :boost, 17 unknown_keywords = options.keys - [:aggs, :block, :body, :body_options, :boost,
@@ -298,6 +298,19 @@ module Searchkick @@ -298,6 +298,19 @@ module Searchkick
298 self 298 self
299 end 299 end
300 300
  301 + def body(*args)
  302 + if args.empty?
  303 + params.body
  304 + else
  305 + spawn.body!(*args)
  306 + end
  307 + end
  308 +
  309 + def body!(value)
  310 + options[:body] = value
  311 + self
  312 + end
  313 +
301 def body_options(value) 314 def body_options(value)
302 spawn.body_options!(value) 315 spawn.body_options!(value)
303 end 316 end
test/index_test.rb
@@ -58,6 +58,7 @@ class IndexTest < Minitest::Test @@ -58,6 +58,7 @@ class IndexTest < Minitest::Test
58 def test_body 58 def test_body
59 store_names ["Dollar Tree"], Store 59 store_names ["Dollar Tree"], Store
60 assert_equal ["Dollar Tree"], Store.search(body: {query: {match: {name: "dollar"}}}, load: false).map(&:name) 60 assert_equal ["Dollar Tree"], Store.search(body: {query: {match: {name: "dollar"}}}, load: false).map(&:name)
  61 + assert_equal ["Dollar Tree"], Store.search.body(query: {match: {name: "dollar"}}).load(false).map(&:name)
61 end 62 end
62 63
63 def test_body_incompatible_options 64 def test_body_incompatible_options