Class: ThinkingSphinx::Field

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

Overview

Fields - holding the string data which Sphinx indexes for your searches. 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 field 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 = {}) ⇒ Field

To create a new field, you’ll need to pass in either a single Column or an array of them, and some (optional) options. The columns are references to the data that will make up the field.

Valid options are:

  • :as => :alias_name

  • :sortable => true

  • :infixes => true

  • :prefixes => true

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.

Sortable defaults to false - but is quite useful when set to true, as it creates an attribute with the same string value (which Sphinx converts to an integer value), which can be sorted by. Thinking Sphinx is smart enough to realise that when you specify fields in sort statements, you mean their respective attributes.

If you have partial matching enabled (ie: enable_star), then you can specify certain fields to have their prefixes and infixes indexed. Keep in mind, though, that Sphinx’s default is all fields - so once you highlight a particular field, no other fields in the index will have these partial indexes.

Here’s some examples:

Field.new(
  Column.new(:name)
)

Field.new(
  [Column.new(:first_name), Column.new(:last_name)],
  :as => :name, :sortable => true
)

Field.new(
  [Column.new(:posts, :subject), Column.new(:posts, :content)],
  :as => :posts, :prefixes => true
)


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

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]
  @sortable     = options[:sortable] || false
  @infixes      = options[:infixes]  || false
  @prefixes     = options[:prefixes] || false
end

Instance Attribute Details

#aliasObject

Returns the value of attribute alias.



11
12
13
# File 'lib/thinking_sphinx/field.rb', line 11

def alias
  @alias
end

#associationsObject

Returns the value of attribute associations.



11
12
13
# File 'lib/thinking_sphinx/field.rb', line 11

def associations
  @associations
end

#columnsObject

Returns the value of attribute columns.



11
12
13
# File 'lib/thinking_sphinx/field.rb', line 11

def columns
  @columns
end

#infixesObject

Returns the value of attribute infixes.



11
12
13
# File 'lib/thinking_sphinx/field.rb', line 11

def infixes
  @infixes
end

#modelObject

Returns the value of attribute model.



11
12
13
# File 'lib/thinking_sphinx/field.rb', line 11

def model
  @model
end

#prefixesObject

Returns the value of attribute prefixes.



11
12
13
# File 'lib/thinking_sphinx/field.rb', line 11

def prefixes
  @prefixes
end

#sortableObject

Returns the value of attribute sortable.



11
12
13
# File 'lib/thinking_sphinx/field.rb', line 11

def sortable
  @sortable
end

Instance Method Details

#to_group_sqlObject

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



89
90
91
92
93
94
95
96
97
98
# File 'lib/thinking_sphinx/field.rb', line 89

def to_group_sql
  case
  when is_many?, 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 field. Don’t forget to set your model and associations first though.

This will concatenate strings if there’s more than one data source or multiple data values (has_many or has_and_belongs_to_many associations).



73
74
75
76
77
78
79
80
81
82
# File 'lib/thinking_sphinx/field.rb', line 73

def to_select_sql
  clause = @columns.collect { |column|
    column_with_prefix(column)
  }.join(', ')
  
  clause = concatenate(clause) if concat_ws?
  clause = group_concatenate(clause) if is_many?
  
  "#{cast_to_string clause } AS #{quote_column(unique_name)}"
end

#unique_nameObject

Returns the unique name of the field - which is either the alias of the field, 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.



105
106
107
108
109
110
111
# File 'lib/thinking_sphinx/field.rb', line 105

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