Skip to content

Commit

Permalink
create method to set indexed_name either based on the :as option or f…
Browse files Browse the repository at this point in the history
…rom the @type and name.
  • Loading branch information
jronallo authored and outoftime committed Dec 27, 2010
1 parent cf00a3f commit 681fe35
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
46 changes: 27 additions & 19 deletions sunspot/lib/sunspot/field.rb
Expand Up @@ -6,13 +6,13 @@ class Field #:nodoc:
attr_reader :boost
attr_reader :indexed_name # Name with which this field is indexed internally. Based on public name and type or the +:as+ option.

#
#
#
def initialize(name, type, options = {}) #:nodoc
@name, @type = name.to_sym, type
@stored = !!options.delete(:stored)
@more_like_this = !!options.delete(:more_like_this)
@indexed_name = (options.delete(:as) || @type.indexed_name(@name)).to_s
@indexed_name = set_indexed_name(options)
raise ArgumentError, "Field of type #{type} cannot be used for more_like_this" unless type.accepts_more_like_this? or !@more_like_this
end

Expand Down Expand Up @@ -58,7 +58,7 @@ def cast(value)
@type.cast(value)
end

#
#
# Whether this field accepts multiple values.
#
# ==== Returns
Expand All @@ -69,7 +69,7 @@ def multiple?
!!@multiple
end

#
#
# Whether this field can be used for more_like_this queries.
# If true, the field is configured to store termVectors.
#
Expand All @@ -89,9 +89,27 @@ def eql?(field)
indexed_name == field.indexed_name
end
alias_method :==, :eql?

#
# Determine the indexed name. If the :as option is given use that, otherwise
# create the value based on the indexed_name of the type with additional
# suffixes for multiple, stored, and more_like_this.
#
# ==== Returns
#
# String: The field's indexed name
#
def set_indexed_name(options)
if options[:as]
options.delete(:as)
else
"#{@type.indexed_name(@name).to_s}#{'m' if @multiple and !@as }#{'s' if @stored}#{'v' if more_like_this?}"
end
end

end

#
#
# FulltextField instances represent fields that are indexed as fulltext.
# These fields are tokenized in the index, and can have boost applied to
# them. They also always allow multiple values (since the only downside of
Expand All @@ -111,11 +129,11 @@ def initialize(name, options = {})
end

def indexed_name
"#{super}#{'s' if @stored}#{'v' if more_like_this?}"
"#{super}"
end
end

#
#
# AttributeField instances encapsulate non-tokenized attribute data.
# AttributeFields can have any type except TextType, and can also have
# a reference (for instantiated facets), optionally allow multiple values
Expand All @@ -124,8 +142,8 @@ def indexed_name
#
class AttributeField < Field #:nodoc:
def initialize(name, type, options = {})
super(name, type, options)
@multiple = !!options.delete(:multiple)
super(name, type, options)
@reference =
if (reference = options.delete(:references)).respond_to?(:name)
reference.name
Expand All @@ -135,17 +153,6 @@ def initialize(name, type, options = {})
raise ArgumentError, "Unknown field option #{options.keys.first.inspect} provided for field #{name.inspect}" unless options.empty?
end

# The name of the field as it is indexed in Solr. The indexed name
# contains a suffix that contains information about the type as well as
# whether the field allows multiple values for a document.
#
# ==== Returns
#
# String:: The field's indexed name
#
def indexed_name
"#{super}#{'m' if @multiple}#{'s' if @stored}#{'v' if more_like_this?}"
end
end

class TypeField #:nodoc:
Expand Down Expand Up @@ -180,3 +187,4 @@ def to_indexed(id)
end
end
end

8 changes: 7 additions & 1 deletion sunspot/spec/api/indexer/attributes_spec.rb
Expand Up @@ -135,9 +135,15 @@
Sunspot.setup(Post) { integer :popularity, :more_like_this => true }
end.should raise_error(ArgumentError)
end

it 'should use a specified field name when the :as option is set' do
session.index(post(:title => 'A Title'))
connection.should have_add_with(:legacy_field_s => 'legacy A Title')
end

it 'should use a specified field name when the :as option is set for array values' do
session.index(post(:title => 'Another Title'))
connection.should have_add_with(:legacy_array_field => ['first string', 'second string'])
end
end

7 changes: 6 additions & 1 deletion sunspot/spec/mocks/post.rb
Expand Up @@ -70,11 +70,16 @@ def custom_boolean
1 + (ratings_average - 3.0) / 4.0
end
end

string :legacy, :as => :legacy_field_s do
"legacy #{title}"
end

string :legacy_array, :as => :legacy_array_field, :multiple => true do
['first string', 'second string']
end
end

class PhotoPost < Post
end

0 comments on commit 681fe35

Please sign in to comment.