Skip to content
Browse files

Index should bulk store to its own URL endpoint (fixes #327)

  • Loading branch information...
1 parent e5a0a1c commit 256e6e823ed21dab62e1d6de9a77acd2e6d5eeec @nz committed Apr 30, 2012
Showing with 48 additions and 39 deletions.
  1. +35 −31 lib/tire/index.rb
  2. +13 −8 test/unit/index_test.rb
View
66 lib/tire/index.rb
@@ -8,31 +8,35 @@ def initialize(name, &block)
instance_eval(&block) if block_given?
end
+ def url
+ "#{Configuration.url}/#{@name}"
+ end
+
def exists?
- @response = Configuration.client.head("#{Configuration.url}/#{@name}")
+ @response = Configuration.client.head(url)
@response.success?
ensure
- curl = %Q|curl -I "#{Configuration.url}/#{@name}"|
+ curl = %Q|curl -I "#{url}"|
logged('HEAD', curl)
end
def delete
- @response = Configuration.client.delete "#{Configuration.url}/#{@name}"
+ @response = Configuration.client.delete(url)
@response.success?
ensure
- curl = %Q|curl -X DELETE "#{Configuration.url}/#{@name}"|
+ curl = %Q|curl -X DELETE "#{url}"|
logged('DELETE', curl)
end
def create(options={})
@options = options
- @response = Configuration.client.post "#{Configuration.url}/#{@name}", MultiJson.encode(options)
+ @response = Configuration.client.post(url, MultiJson.encode(options))
@response.success? ? @response : false
ensure
- curl = %Q|curl -X POST "#{Configuration.url}/#{@name}" -d '#{MultiJson.encode(options)}'|
+ curl = %Q|curl -X POST "#{url}" -d '#{MultiJson.encode(options)}'|
logged('CREATE', curl)
end
@@ -57,25 +61,25 @@ def remove_alias(alias_name)
end
def aliases(alias_name = nil)
- @response = Configuration.client.get "#{Configuration.url}/#{@name}/_aliases"
+ @response = Configuration.client.get "#{url}/_aliases"
if alias_name
MultiJson.decode(@response.body)[@name]['aliases'][alias_name]
else
MultiJson.decode(@response.body)[@name]['aliases'].keys
end
ensure
- curl = %Q|curl "#{Configuration.url}/#{@name}/_aliases?pretty"|
+ curl = %Q|curl "#{url}/_aliases?pretty"|
logged('GET', curl)
end
def mapping
- @response = Configuration.client.get("#{Configuration.url}/#{@name}/_mapping")
+ @response = Configuration.client.get("#{url}/_mapping")
MultiJson.decode(@response.body)[@name]
end
def settings
- @response = Configuration.client.get("#{Configuration.url}/#{@name}/_settings")
+ @response = Configuration.client.get("#{url}/_settings")
MultiJson.decode(@response.body)[@name]['settings']
end
@@ -91,14 +95,14 @@ def store(*args)
id = get_id_from_document(document)
document = convert_document_to_json(document)
- url = id ? "#{Configuration.url}/#{@name}/#{type}/#{id}" : "#{Configuration.url}/#{@name}/#{type}/"
- url += "?percolate=#{percolate}" if percolate
+ u = id ? "#{url}/#{type}/#{id}" : "#{url}/#{type}/"
+ u += "?percolate=#{percolate}" if percolate
- @response = Configuration.client.post url, document
+ @response = Configuration.client.post(u, document)
MultiJson.decode(@response.body)
ensure
- curl = %Q|curl -X POST "#{url}" -d '#{document}'|
+ curl = %Q|curl -X POST "#{u}" -d '#{document}'|
logged([type, id].join('/'), curl)
end
@@ -120,7 +124,7 @@ def bulk_store(documents, options={})
count = 0
begin
- response = Configuration.client.post("#{Configuration.url}/_bulk", payload.join("\n"))
+ response = Configuration.client.post("#{url}/_bulk", payload.join("\n"))
raise RuntimeError, "#{response.code} > #{response.body}" if response.failure?
response
rescue StandardError => error
@@ -185,21 +189,21 @@ def remove(*args)
end
raise ArgumentError, "Please pass a document ID" unless id
- url = "#{Configuration.url}/#{@name}/#{type}/#{id}"
- result = Configuration.client.delete url
+ u = "#{url}/#{type}/#{id}"
+ result = Configuration.client.delete(u)
MultiJson.decode(result.body) if result.success?
ensure
- curl = %Q|curl -X DELETE "#{url}"|
+ curl = %Q|curl -X DELETE "#{u}"|
logged(id, curl)
end
def retrieve(type, id)
raise ArgumentError, "Please pass a document ID" unless id
type = Utils.escape(type)
- url = "#{Configuration.url}/#{@name}/#{type}/#{id}"
- @response = Configuration.client.get url
+ u = "#{url}/#{type}/#{id}"
+ @response = Configuration.client.get(u)
h = MultiJson.decode(@response.body)
if Configuration.wrapper == Hash then h
@@ -211,45 +215,45 @@ def retrieve(type, id)
end
ensure
- curl = %Q|curl -X GET "#{url}"|
+ curl = %Q|curl -X GET "#{u}"|
logged(id, curl)
end
def refresh
- @response = Configuration.client.post "#{Configuration.url}/#{@name}/_refresh", ''
+ @response = Configuration.client.post "#{url}/_refresh", ''
ensure
- curl = %Q|curl -X POST "#{Configuration.url}/#{@name}/_refresh"|
+ curl = %Q|curl -X POST "#{url}/_refresh"|
logged('_refresh', curl)
end
def open(options={})
# TODO: Remove the duplication in the execute > rescue > ensure chain
- @response = Configuration.client.post "#{Configuration.url}/#{@name}/_open", MultiJson.encode(options)
+ @response = Configuration.client.post "#{url}/_open", MultiJson.encode(options)
MultiJson.decode(@response.body)['ok']
ensure
- curl = %Q|curl -X POST "#{Configuration.url}/#{@name}/_open"|
+ curl = %Q|curl -X POST "#{url}/_open"|
logged('_open', curl)
end
def close(options={})
- @response = Configuration.client.post "#{Configuration.url}/#{@name}/_close", MultiJson.encode(options)
+ @response = Configuration.client.post "#{url}/_close", MultiJson.encode(options)
MultiJson.decode(@response.body)['ok']
ensure
- curl = %Q|curl -X POST "#{Configuration.url}/#{@name}/_close"|
+ curl = %Q|curl -X POST "#{url}/_close"|
logged('_close', curl)
end
def analyze(text, options={})
options = {:pretty => true}.update(options)
params = options.to_param
- @response = Configuration.client.get "#{Configuration.url}/#{@name}/_analyze?#{params}", text
+ @response = Configuration.client.get "#{url}/_analyze?#{params}", text
@response.success? ? MultiJson.decode(@response.body) : false
ensure
- curl = %Q|curl -X GET "#{Configuration.url}/#{@name}/_analyze?#{params}" -d '#{text}'|
+ curl = %Q|curl -X GET "#{url}/_analyze?#{params}" -d '#{text}'|
logged('_analyze', curl)
end
@@ -284,11 +288,11 @@ def percolate(*args, &block)
payload = { :doc => document }
payload.update( :query => query ) if query
- @response = Configuration.client.get "#{Configuration.url}/#{@name}/#{type}/_percolate", MultiJson.encode(payload)
+ @response = Configuration.client.get "#{url}/#{type}/_percolate", MultiJson.encode(payload)
MultiJson.decode(@response.body)['matches']
ensure
- curl = %Q|curl -X GET "#{Configuration.url}/#{@name}/#{type}/_percolate?pretty=1" -d '#{payload.to_json}'|
+ curl = %Q|curl -X GET "#{url}/#{type}/_percolate?pretty=1" -d '#{payload.to_json}'|
logged('_percolate', curl)
end
View
21 test/unit/index_test.rb
@@ -428,7 +428,7 @@ class MyDocument;end; document = MyDocument.new
should "serialize Hashes" do
Configuration.client.expects(:post).with do |url, json|
- url == "#{Configuration.url}/_bulk" &&
+ url == "#{@index.url}/_bulk" &&
json =~ /"_index":"dummy"/ &&
json =~ /"_type":"document"/ &&
json =~ /"_id":"1"/ &&
@@ -444,8 +444,10 @@ class MyDocument;end; document = MyDocument.new
end
should "serialize ActiveModel instances" do
+ index = ActiveModelArticle.index
+
Configuration.client.expects(:post).with do |url, json|
- url == "#{Configuration.url}/_bulk" &&
+ url == "#{index.url}/_bulk" &&
json =~ /"_index":"active_model_articles"/ &&
json =~ /"_type":"active_model_article"/ &&
json =~ /"_id":"1"/ &&
@@ -457,15 +459,16 @@ class MyDocument;end; document = MyDocument.new
one = ActiveModelArticle.new 'title' => 'One'; one.id = '1'
two = ActiveModelArticle.new 'title' => 'Two'; two.id = '2'
- ActiveModelArticle.index.bulk_store [ one, two ]
-
+ index.bulk_store [ one, two ]
end
context "namespaced models" do
should "not URL-escape the document_type" do
+ index = Tire.index('my_namespace_my_models')
+
Configuration.client.expects(:post).with do |url, json|
puts url, json
- url == "#{Configuration.url}/_bulk" &&
+ url == "#{index.url}/_bulk" &&
json =~ %r|"_index":"my_namespace_my_models"| &&
json =~ %r|"_type":"my_namespace/my_model"|
end.returns(mock_response('{}', 200))
@@ -477,7 +480,7 @@ def to_indexed_json; "{}"; end
end
end
- Tire.index('my_namespace_my_models').bulk_store [ MyNamespace::MyModel.new ]
+ index.bulk_store [ MyNamespace::MyModel.new ]
end
end
@@ -510,11 +513,13 @@ def to_indexed_json; "{}"; end
end
should "display error message when collection item does not have ID" do
- Configuration.client.expects(:post).with{ |url, json| url == "#{Configuration.url}/_bulk" }.returns(mock_response('success', 200))
+ index = ActiveModelArticle.index
+
+ Configuration.client.expects(:post).with{ |url, json| url == "#{index.url}/_bulk" }.returns(mock_response('success', 200))
STDERR.expects(:puts).once
documents = [ { :title => 'Bogus' }, { :title => 'Real', :id => 1 } ]
- ActiveModelArticle.index.bulk_store documents
+ index.bulk_store documents
end
end

0 comments on commit 256e6e8

Please sign in to comment.
Something went wrong with that request. Please try again.