Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactored the "Put Mapping" API support

* Cleaned up and simplified code
* Made the return values more compatible with the rest of the API (return `false` for failures, users should check `index.response`)
* Removed `put_mapping!`
* Amended, completed and polished tests
* Updated documentation

Related: #194, #490, #520, #645
  • Loading branch information...
commit 8fc50608a60373bf7e1a551365c879f24e9424d0 1 parent 79ad6f0
@karmi authored
View
1  .gitignore
@@ -12,4 +12,3 @@ examples/*.html
.rvmrc
.rbenv-version
tmp/
-vendor/bundle
View
58 lib/tire/index.rb
@@ -52,67 +52,57 @@ def aliases(alias_name=nil)
alias_name ? Alias.all(@name).select { |a| a.name == alias_name }.first : Alias.all(@name)
end
- # :call-seq:
- # mapping
- # mapping(type, mapping)
+ # Get or update the index mapping
#
- # With no arguments, retrieves all the mappings for this index.
- # With a _type_ and _mapping_ hash, issues a "put mapping" for that type.
- # You can specify the _ignore_conflicts_ option in the _mapping_ hash.
+ # Without arguments, returns the index mapping as a Hash
+ #
+ # When passed arguments, attempts to update the index mapping:
+ #
+ # index.mapping 'article', properties: { body: { type: "string" } }
+ #
+ # You can pass the `ignore_conflicts` option as a part of the Hash:
+ #
+ # index.mapping 'article', properties: { body: { type: "string" } }, ignore_conflicts: true
#
- # Ex:
- # mapping("tweet", :ignore_conflicts => true, :properties => { :message => { :type => "string", :store => "yes" } } )
def mapping(*args)
- if args.empty?
- get_mapping
- else
- put_mapping(*args)
- end
+ args.empty? ? get_mapping : put_mapping(*args)
end
- # Same as mapping, but raises any errors as exceptions.
+ # Raises an exception for unsuccessful responses
+ #
def mapping!(*args)
- mapping(*args).tap do |response|
- raise RuntimeError, response["error"] unless response["ok"]
- end
+ mapping(*args)
+ raise RuntimeError, response.body unless response.success?
end
def get_mapping
@response = Configuration.client.get("#{url}/_mapping")
- MultiJson.decode(@response.body)[@name]
+ result = MultiJson.decode(@response.body)[@name]
+ @response.success? ? result : false
ensure
- curl = %Q|curl -X GET "#{url}/_mapping"|
+ curl = %Q|curl -X GET "#{url}/_mapping?pretty"|
logged("GET MAPPING", curl)
end
def put_mapping(type, mapping)
-
- # Figure out the params
params = {}
- ignore_conflicts = mapping.delete(:ignore_conflicts) || mapping.delete("ignore_conflicts")
- params[:ignore_conflicts] = ignore_conflicts unless ignore_conflicts.nil?
+ if ignore_conflicts = mapping.delete(:ignore_conflicts) || mapping.delete("ignore_conflicts")
+ params[:ignore_conflicts] = ignore_conflicts
+ end
- # Figure out the url
- url = "#{self.url}/#{type}/_mapping"
+ url = "#{self.url}/#{type}/_mapping"
url += "?#{params.to_param}" unless params.empty?
- # Figure out the payload
payload = { type => mapping }.to_json
- # Do it
@response = Configuration.client.put url, payload
- MultiJson.decode(@response.body)
-
+ result = MultiJson.decode(@response.body)
+ @response.success? ? result : false
ensure
curl = %Q|curl -X PUT "#{url}" -d '#{payload}'|
logged("PUT MAPPING #{type}", curl)
end
- # Same as put_mapping but raises any errors as exceptions.
- def put_mapping!(type, mapping)
- mapping!(type, mapping)
- end
-
def settings
@response = Configuration.client.get("#{url}/_settings")
MultiJson.decode(@response.body)[@name]['settings']
View
71 test/integration/index_mapping_test.rb
@@ -8,7 +8,7 @@ class IndexMappingIntegrationTest < Test::Unit::TestCase
context "Default mapping" do
teardown { Tire.index('mapped-index').delete; sleep 0.1 }
- should "create and return the default mapping" do
+ should "create and return the default mapping as a Hash" do
index = Tire.index 'mapped-index' do
create
@@ -24,47 +24,76 @@ class IndexMappingIntegrationTest < Test::Unit::TestCase
context "Creating index with mapping" do
teardown { Tire.index('mapped-index').delete; sleep 0.1 }
-
+
should "create the specified mapping" do
-
index = Tire.index 'mapped-index' do
- create :mappings => { :article => { :properties => { :title => { :type => 'string', :boost => 2.0, :store => 'yes' } } } }
+ create mappings: {
+ article: {
+ _all: { enabled: false },
+ properties: {
+ title: { type: 'string', boost: 2.0, store: 'yes' }
+ }
+ }
+ }
end
- # p index.mapping
+ # p index.mapping
+ assert_equal false, index.mapping['article']['_all']['enabled'], index.mapping.inspect
assert_equal 2.0, index.mapping['article']['properties']['title']['boost'], index.mapping.inspect
- assert_equal 'yes', index.mapping['article']['properties']['title']['store'], index.mapping.inspect
-
end
end
- context "Put mapping" do
- setup{ Tire.index("mapped-index").create; sleep 1 }
- teardown{ Tire.index("mapped-index").delete; sleep 0.1 }
+ context "Update mapping" do
+ setup { Tire.index("mapped-index").create; sleep 1 }
+ teardown { Tire.index("mapped-index").delete; sleep 0.1 }
should "update the mapping for a given type" do
index = Tire.index("mapped-index")
- index.mapping("article", :properties => { :body => { :type => "string" } })
+ index.mapping "article", :properties => { :body => { :type => "string" } }
assert_equal({ "type" => "string" }, index.mapping["article"]["properties"]["body"])
- index.mapping("article", :properties => { :title => { :type => "string" } })
+ assert index.mapping("article", :properties => { :title => { :type => "string" } })
+
mapping = index.mapping
- assert_equal mapping["article"]["properties"]["body"], { "type" => "string" }
- assert_equal mapping["article"]["properties"]["title"], { "type" => "string" }
+
+ # Verify return value
+ assert mapping, index.response.inspect
+
+ # Verify response
+ assert_equal( { "type" => "string" }, mapping["article"]["properties"]["body"] )
+ assert_equal( { "type" => "string" }, mapping["article"]["properties"]["title"] )
end
- should "honor the ignore conflicts option" do
+ should "fail to update the mapping in an incompatible way" do
index = Tire.index("mapped-index")
- index.mapping("article", :properties => { :body => { :type => "string" } })
- assert_equal({ "type" => "string" }, index.mapping["article"]["properties"]["body"])
+ # 1. Update initial index mapping
+ assert index.mapping "article", properties: { body: { type: "string" } }
+ assert_equal( { "type" => "string" }, index.mapping["article"]["properties"]["body"] )
+
+ # 2. Attempt to update the mapping in incompatible way (change property type)
+ mapping = index.mapping "article", :properties => { :body => { :type => "integer" } }
+
+ # Verify return value
+ assert !mapping, index.response.inspect
+ #
+ # Verify response
+ assert_match /MergeMappingException/, index.response.body
+ end
+
+ should "honor the `ignore_conflicts` option" do
+ index = Tire.index("mapped-index")
+
+ # 1. Update initial index mapping
+ assert index.mapping "article", properties: { body: { type: "string" } }
+ assert_equal( { "type" => "string" }, index.mapping["article"]["properties"]["body"] )
- response = index.mapping("article", :properties => { :body => { :type => "integer" } })
- assert response["error"] =~ /^MergeMappingException/
+ # 2. Attempt to update the mapping in incompatible way and ignore conflicts
+ mapping = index.mapping "article", ignore_conflicts: true, properties: { body: { type: "integer" } }
- response = index.mapping("article", :ignore_conflicts => true, :properties => { :body => { :type => "integer" } })
- assert response["ok"]
+ # Verify return value (true since we ignore conflicts)
+ assert mapping, index.response.inspect
end
end
View
16 test/unit/index_test.rb
@@ -170,7 +170,7 @@ class IndexTest < Test::Unit::TestCase
}
end
- should "return the mapping" do
+ should "return the mapping as a Hash" do
json =<<-JSON
{
"dummy" : {
@@ -189,8 +189,18 @@ class IndexTest < Test::Unit::TestCase
assert_equal 2.0, @index.mapping['article']['properties']['title']['boost']
end
- should "raise an exception when used with the bang method" do
- Configuration.client.expects(:put).returns(mock_response('{"error":"MergeMappingException","status":400}'))
+ should "update the mapping" do
+ Configuration.client.expects(:put).returns(mock_response('{"ok":true,"acknowledged":true}', 200))
+ assert @index.mapping( 'document', { properties: { body: {type: 'string', analyzer: 'english'} } } )
+ end
+
+ should "fail to update the mapping when conflicts occur" do
+ Configuration.client.expects(:put).returns(mock_response('{"error":"MergeMappingException","status":400}', 400))
+ assert ! @index.mapping( 'document', { properties: { body: {type: 'string', analyzer: 'english'} } } )
+ end
+
+ should "raise an exception for the bang method" do
+ Configuration.client.expects(:put).returns(mock_response('{"error":"MergeMappingException","status":400}', 400))
assert_raise(RuntimeError) do
@index.mapping!("blah", {})
end
Please sign in to comment.
Something went wrong with that request. Please try again.