Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Keep given search payload in the body. #292

Closed
wants to merge 4 commits into
from
View
@@ -6,7 +6,8 @@
require 'active_support/core_ext/object/to_param'
require 'active_support/core_ext/object/to_query'
-require 'active_support/core_ext/hash/except.rb'
+require 'active_support/core_ext/hash/slice'
+require 'active_support/core_ext/hash/except'
# Ruby 1.8 compatibility
require 'tire/rubyext/ruby_1_8' if defined?(RUBY_VERSION) && RUBY_VERSION < '1.9'
View
@@ -9,7 +9,9 @@ class Search
def initialize(indices=nil, options={}, &block)
@indices = Array(indices)
@types = Array(options.delete(:type)).map { |type| Utils.escape(type) }
+ @payload = options.delete(:payload)
@options = options
+ @result_options = options.extract!(:load, :wrapper)
@path = ['/', @indices.join(','), @types.join(','), '_search'].compact.join('/').squeeze('/')
@@ -97,7 +99,7 @@ def perform
raise SearchRequestFailed, @response.to_s
end
@json = MultiJson.decode(@response.body)
- @results = Results::Collection.new(@json, @options)
+ @results = Results::Collection.new(@json, @options.merge(@result_options))
return self
ensure
logged
@@ -108,7 +110,7 @@ def to_curl
end
def to_hash
- @options.delete(:payload) || begin
+ @payload || begin
request = {}
request.update( { :query => @query.to_hash } ) if @query
request.update( { :sort => @sort.to_ary } ) if @sort
View
@@ -63,6 +63,28 @@ class SearchTest < Test::Unit::TestCase
assert_equal '', s.params
end
+ should "allow to search with given payload" do
+ given_payload = { 'query' => { 'query_string' => { 'query' => 'foo' } } }
+ Configuration.client.expects(:get).with do |url, got_payload|
+ assert_equal given_payload, MultiJson.decode( got_payload )
+ !url.include?('payload')
+ end.returns mock_response( { 'hits' => { 'hits' => [ {:_id => 1} ] } }.to_json )
+
+ s = Search::Search.new('index', :payload => given_payload)
+ assert_equal given_payload, s.to_hash
+ s.perform
+ end
+
+ should "pass on result collection arguments without encoding them in the request" do
+ Configuration.client.expects(:get).with do |url, payload|
+ !url.include?('load') && !url.include?('wrapper')
+ end.returns mock_response( { 'hits' => { 'hits' => [ {:_id => 1} ] } }.to_json )
+
+ result_options = { :load => true, :wrapper => Hash }
+ s = Search::Search.new('index', result_options.dup) { query { string 'foo' } }.results
+ assert_equal result_options, s.options
+ end
+
should "properly encode namespaced document type" do
Configuration.client.expects(:get).with do |url, payload|
url.match %r|index/my_application%2Farticle/_search|