Class: ThinkingSphinx::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/thinking_sphinx/attribute.rb

Overview

Attributes - eternally useful when it comes to filtering, sorting or grouping. This class isn’t really useful to you unless you’re hacking around with the internals of Thinking Sphinx - but hey, don’t let that stop you.

One key thing to remember - if you’re using the attribute manually to generate SQL statements, you’ll need to set the base model, and all the associations. Which can get messy. Use Index.link!, it really helps.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(columns, options = {}) ⇒ Attribute

To create a new attribute, you’ll need to pass in either a single Column or an array of them, and some (optional) options.

Valid options are:

  • :as => :alias_name

  • :type => :attribute_type

Alias is only required in three circumstances: when there’s another attribute or field with the same name, when the column name is ‘id’, or when there’s more than one column.

Type is not required, unless you want to force a column to be a certain type (but keep in mind the value will not be CASTed in the SQL statements). The only time you really need to use this is when the type can’t be figured out by the column - ie: when not actually using a database column as your source.

Example usage:

Attribute.new(
  Column.new(:created_at)
)

Attribute.new(
  Column.new(:posts, :id),
  :as => :post_ids
)

Attribute.new(
  [Column.new(:pages, :id), Column.new(:articles, :id)],
  :as => :content_ids
)

Attribute.new(
  Column.new("NOW()"),
  :as   => :indexed_at,
  :type => :datetime
)

If you’re creating attributes for latitude and longitude, don’t forget that Sphinx expects these values to be in radians.



56
57
58
59
60
61
62
63
64
# File 'lib/thinking_sphinx/attribute.rb', line 56

def initialize(columns, options = {})
  @columns      = Array(columns)
  @associations = {}
  
  raise "Cannot define a field with no columns. Maybe you are trying to index a field with a reserved name (id, name). You can fix this error by using a symbol rather than a bare name (:id instead of id)." if @columns.empty? || @columns.any? { |column| !column.respond_to?(:__stack) }
  
  @alias        = options[:as]
  @type         = options[:type]
end

Instance Attribute Details

#aliasObject

Returns the value of attribute alias.



12
13
14
# File 'lib/thinking_sphinx/attribute.rb', line 12

def alias
  @alias
end

#associationsObject

Returns the value of attribute associations.



12
13
14
# File 'lib/thinking_sphinx/attribute.rb', line 12

def associations
  @associations
end

#columnsObject

Returns the value of attribute columns.



12
13
14
# File 'lib/thinking_sphinx/attribute.rb', line 12

def columns
  @columns
end

#modelObject

Returns the value of attribute model.



12
13
14
# File 'lib/thinking_sphinx/attribute.rb', line 12

def model
  @model
end

Instance Method Details

#to_group_sqlObject

Get the part of the GROUP BY clause related to this attribute - if one is needed. If not, all you’ll get back is nil. The latter will happen if there isn’t actually a real column to get data from, or if there’s multiple data values (read: a has_many or has_and_belongs_to_many association).



93
94
95
96
97
98
99
100
101
102
# File 'lib/thinking_sphinx/attribute.rb', line 93

def to_group_sql
  case
  when is_many?, is_string?, ThinkingSphinx.use_group_by_shortcut?
    nil
  else
    @columns.collect { |column|
      column_with_prefix(column)
    }
  end
end

#to_select_sqlObject

Get the part of the SELECT clause related to this attribute. Don’t forget to set your model and associations first though.

This will concatenate strings and arrays of integers, and convert datetimes to timestamps, as needed.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/thinking_sphinx/attribute.rb', line 72

def to_select_sql
  clause = @columns.collect { |column|
    column_with_prefix(column)
  }.join(', ')
  
  separator = all_ints? ? ',' : ' '
  
  clause = concatenate(clause, separator)       if concat_ws?
  clause = group_concatenate(clause, separator) if is_many?
  clause = cast_to_datetime(clause)             if type == :datetime
  clause = convert_nulls(clause)                if type == :string
  
  "#{clause} AS #{quote_column(unique_name)}"
end

#to_sphinx_clauseObject

Generates the appropriate attribute statement for a Sphinx configuration file, depending on the attribute’s type.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/thinking_sphinx/attribute.rb', line 107

def to_sphinx_clause
  case type
  when :multi
    "sql_attr_multi       = uint #{unique_name} from field"
  when :datetime
    "sql_attr_timestamp   = #{unique_name}"
  when :string
    "sql_attr_str2ordinal = #{unique_name}"
  when :float
    "sql_attr_float       = #{unique_name}"
  when :boolean
    "sql_attr_bool        = #{unique_name}"
  else
    "sql_attr_uint        = #{unique_name}"
  end
end

#unique_nameObject

Returns the unique name of the attribute - which is either the alias of the attribute, or the name of the only column - if there is only one. If there isn’t, there should be an alias. Else things probably won’t work. Consider yourself warned.



129
130
131
132
133
134
135
# File 'lib/thinking_sphinx/attribute.rb', line 129

def unique_name
  if @columns.length == 1
    @alias || @columns.first.__name
  else
    @alias
  end
end