Skip to content
Browse files

[ACTIVEMODEL] Support any level of mappings in `mapping`

Previously, only two levels of mappings were supported, ex:

    mapping do
      indexes :title, :type => 'string'
      indexes :author do
        indexes :first_name, :type => 'string'
        indexes :last_name,  :type => 'string', :boost => 100
      end
    end

This commit adds support for any level of mappings, ex:

    mapping do
      indexes :title, :type => 'string'
      indexes :author do
        indexes :first_name, :type => 'string'
        indexes :last_name,  :type => 'string', :boost => 100

        indexes :posts do
          indexes :title, :type => 'string', :boost => 10
        end
      end
    end

Closes #210.
  • Loading branch information...
1 parent 5739059 commit 039cc091309a11363593281f5ff405b4ca974b65 @woahdae woahdae committed with karmi Jan 18, 2012
Showing with 33 additions and 14 deletions.
  1. +21 −13 lib/tire/model/indexing.rb
  2. +12 −1 test/unit/model_search_test.rb
View
34 lib/tire/model/indexing.rb
@@ -41,6 +41,14 @@ def settings(*args)
# indexes :id, :index => :not_analyzed
# indexes :title, :analyzer => 'snowball', :boost => 100
# indexes :words, :as => 'content.split(/\W/).length'
+ #
+ # indexes :comments do
+ # indexes :body
+ # indexes :author do
+ # indexes :name
+ # end
+ # end
+ #
# # ...
# end
# end
@@ -76,21 +84,21 @@ def mapping(*args)
# for more information.
#
def indexes(name, options = {}, &block)
+ mapping[name] = options
+
if block_given?
- mapping[name] ||= { :type => 'object', :properties => {} }.update(options)
- @_nested_mapping = name
- nested = yield
- @_nested_mapping = nil
- self
- else
- options[:type] ||= 'string'
- if @_nested_mapping
- mapping[@_nested_mapping][:properties][name] = options
- else
- mapping[name] = options
- end
- self
+ mapping[name][:type] ||= 'object'
+ mapping[name][:properties] ||= {}
+
+ previous = @mapping
+ @mapping = mapping[name][:properties]
+ yield
+ @mapping = previous
end
+
+ mapping[name][:type] ||= 'string'
+
+ self
end
# Creates the corresponding index with desired settings and mappings, when it does not exists yet.
View
13 test/unit/model_search_test.rb
@@ -357,7 +357,13 @@ class ::ModelWithCustomMapping
:type => 'object',
:properties => {
:first_name => { :type => 'string' },
- :last_name => { :type => 'string', :boost => 100 }
+ :last_name => { :type => 'string', :boost => 100 },
+ :posts => {
+ :type => 'object',
+ :properties => {
+ :title => { :type => 'string', :boost => 10 }
+ }
+ }
}
}
}
@@ -378,13 +384,18 @@ class ::ModelWithNestedMapping
indexes :author do
indexes :first_name, :type => 'string'
indexes :last_name, :type => 'string', :boost => 100
+
+ indexes :posts do
+ indexes :title, :type => 'string', :boost => 10
+ end
end
end
end
assert_not_nil ModelWithNestedMapping.mapping[:author][:properties][:last_name]
assert_equal 100, ModelWithNestedMapping.mapping[:author][:properties][:last_name][:boost]
+ assert_equal 10, ModelWithNestedMapping.mapping[:author][:properties][:posts][:properties][:title][:boost]
end
should "define mapping for nested documents" do

0 comments on commit 039cc09

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