Permalink
Browse files

[#449] Added support for the meta parameters such as routing, version…

…, etc in the Index#bulk API
  • Loading branch information...
1 parent d2ed980 commit 91f18b57632850c279eb40f720cc00264ff1d071 @karmi committed Nov 11, 2012
Showing with 61 additions and 6 deletions.
  1. +15 −1 lib/tire/index.rb
  2. +46 −5 test/unit/index_test.rb
View
@@ -105,8 +105,22 @@ def bulk(action, documents, options={})
STDERR.puts "[ERROR] Document #{document.inspect} does not have ID" unless id
+ header = { action.to_sym => { :_index => name, :_type => type, :_id => id } }
+
+ if document.respond_to?(:to_hash) && hash = document.to_hash
+ meta = {}
+ meta[:_version] = hash.delete(:_version)
+ meta[:_routing] = hash.delete(:_routing)
+ meta[:_percolate] = hash.delete(:_percolate)
+ meta[:_parent] = hash.delete(:_parent)
+ meta[:_timestamp] = hash.delete(:_timestamp)
+ meta[:_ttl] = hash.delete(:_ttl)
+ meta = meta.reject { |name,value| !value || value.empty? }
+ header[action.to_sym].update(meta)
+ end
+
output = []
- output << %Q|{"#{action}":{"_index":"#{@name}","_type":"#{type}","_id":"#{id}"}}|
+ output << MultiJson.encode(header)
output << convert_document_to_json(document) unless action.to_s == 'delete'
output.join("\n")
end
@@ -556,6 +556,28 @@ class MyDocument;end; document = MyDocument.new
@index.bulk :delete, [ {:id => '1', :title => 'One'}, {:id => '2', :title => 'Two'} ]
end
+ should "serialize meta parameters such as routing into payload header" do
+ Configuration.client.
+ expects(:post).
+ with do |url, payload|
+ # print payload
+ lines = payload.split("\n")
+ assert_match /"_routing":"A"/, lines[0]
+ assert_match /"_routing":"B"/, lines[2]
+ assert_match /"_ttl":"1d"/, lines[2]
+ assert ! lines[4].include?('"_routing"')
+ end.
+ returns(mock_response('{}'), 200)
+
+ @index.bulk :index,
+ [
+ {:id => '1', :title => 'One', :_routing => 'A'},
+ {:id => '2', :title => 'Two', :_routing => 'B', :_ttl => '1d'},
+ {:id => '3', :title => 'Three'}
+ ]
+
+ end
+
should "serialize ActiveModel instances as payload" do
Configuration.client.
expects(:post).
@@ -577,14 +599,33 @@ class MyDocument;end; document = MyDocument.new
ActiveModelArticle.index.bulk :index, [ one, two ]
end
+ should "extract meta information from document objects" do
+ Configuration.client.
+ expects(:post).
+ with do |url, payload|
+ print payload
+ lines = payload.split("\n")
+ assert_match /"_routing":"A"/, lines[0]
+ end.
+ returns(mock_response('{}'), 200)
+
+ class MyModel
+ def document_type; "my_model"; end
+ def to_hash; { :id => 1, :title => 'Foo', :_routing => 'A' }; end
+ def to_indexed_json; MultiJson.encode(to_hash); end
+ end
+
+ Tire.index('my_models').bulk_store [ MyModel.new ]
+ end
+
context "with namespaced models" do
should "not URL-escape the document_type" do
- Configuration.client.expects(:post).with do |url, json|
- # puts url, json
- url == "#{Configuration.url}/my_namespace_my_models/_bulk" &&
- json =~ %r|"_index":"my_namespace_my_models"| &&
- json =~ %r|"_type":"my_namespace/my_model"|
+ Configuration.client.expects(:post).with do |url, payload|
+ # puts url, payload
+ assert_equal "#{Configuration.url}/my_namespace_my_models/_bulk", url
+ assert_match %r|"_index":"my_namespace_my_models"|, payload
+ assert_match %r|"_type":"my_namespace/my_model"|, payload
end.returns(mock_response('{}', 200))
module MyNamespace

0 comments on commit 91f18b5

Please sign in to comment.