Class: Sunspot::Field

Inherits:
Object
  • Object
show all
Defined in:
sunspot/lib/sunspot/field.rb

Overview

:nodoc:

Direct Known Subclasses

AttributeField, FulltextField

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Field) initialize(name, type, options = {})

A new instance of Field

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
# File 'sunspot/lib/sunspot/field.rb', line 11

def initialize(name, type, options = {}) #:nodoc
  @name, @type = name.to_sym, type
  @stored = !!options.delete(:stored)
  @more_like_this = !!options.delete(:more_like_this)
  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

Instance Attribute Details

- (Object) boost (readonly)

Returns the value of attribute boost



6
7
8
# File 'sunspot/lib/sunspot/field.rb', line 6

def boost
  @boost
end

- (Object) indexed_name (readonly)

Name with which this field is indexed internally. Based on public name and type or the :as option.



7
8
9
# File 'sunspot/lib/sunspot/field.rb', line 7

def indexed_name
  @indexed_name
end

- (Object) name

Returns the value of attribute name



3
4
5
# File 'sunspot/lib/sunspot/field.rb', line 3

def name
  @name
end

- (Object) reference

Model class that the value of this field refers to



5
6
7
# File 'sunspot/lib/sunspot/field.rb', line 5

def reference
  @reference
end

- (Object) type

The public-facing name of the field The Type of the field



4
5
6
# File 'sunspot/lib/sunspot/field.rb', line 4

def type
  @type
end

Instance Method Details

- (Object) cast(value)

Cast the value into the appropriate Ruby class for the field’s type

Parameters

value

Solr’s representation of the value

Returns

Object

The cast value



57
58
59
# File 'sunspot/lib/sunspot/field.rb', line 57

def cast(value)
  @type.cast(value)
end

- (Boolean) eql?(field) Also known as: ==

Returns:

  • (Boolean)


88
89
90
# File 'sunspot/lib/sunspot/field.rb', line 88

def eql?(field)
  indexed_name == field.indexed_name
end

- (Object) hash



84
85
86
# File 'sunspot/lib/sunspot/field.rb', line 84

def hash
  indexed_name.hash
end

- (Boolean) more_like_this?

Whether this field can be used for more_like_this queries. If true, the field is configured to store termVectors.

Returns

Boolean

True if this field can be used for more_like_this queries.

Returns:

  • (Boolean)


80
81
82
# File 'sunspot/lib/sunspot/field.rb', line 80

def more_like_this?
  !!@more_like_this
end

- (Boolean) multiple?

Whether this field accepts multiple values.

Returns

Boolean

True if this field accepts multiple values.

Returns:

  • (Boolean)


68
69
70
# File 'sunspot/lib/sunspot/field.rb', line 68

def multiple?
  !!@multiple
end

- (Object) to_indexed(value)

Convert a value to its representation for Solr indexing. This delegates to the #to_indexed method on the field’s type.

Parameters

value

Value to convert to Solr representation

Returns

String

Solr representation of the object

Raises

ArgumentError

the value is an array, but this field does not allow multiple values



35
36
37
38
39
40
41
42
43
44
45
# File 'sunspot/lib/sunspot/field.rb', line 35

def to_indexed(value)
  if value.is_a? Array
    if @multiple
      value.map { |val| to_indexed(val) }
    else
      raise ArgumentError, "#{name} is not a multiple-value field, so it cannot index values #{value.inspect}"
    end
  else
    @type.to_indexed(value)
  end
end