Class: ThinkingSphinx::Field
- 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
-
#infixes ⇒ Object
Returns the value of attribute infixes.
-
#prefixes ⇒ Object
Returns the value of attribute prefixes.
-
#sortable ⇒ Object
Returns the value of attribute sortable.
Attributes inherited from Property
#admin, #alias, #associations, #columns, #faceted, #model
Instance Method Summary collapse
-
#initialize(source, columns, options = {}) ⇒ Field
constructor
To create a new field, you’ll need to pass in either a single Column or an array of them, and some (optional) options.
-
#to_select_sql ⇒ Object
Get the part of the SELECT clause related to this field.
Methods inherited from Property
#admin?, #changed?, #public?, #to_facet, #to_group_sql, #unique_name
Constructor Details
#initialize(source, 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 |
# File 'lib/thinking_sphinx/field.rb', line 55 def initialize(source, columns, = {}) super @sortable = [:sortable] || false @infixes = [:infixes] || false @prefixes = [:prefixes] || false source.fields << self end |
Instance Attribute Details
#infixes ⇒ Object
Returns the value of attribute infixes.
11 12 13 |
# File 'lib/thinking_sphinx/field.rb', line 11 def infixes @infixes end |
#prefixes ⇒ Object
Returns the value of attribute prefixes.
11 12 13 |
# File 'lib/thinking_sphinx/field.rb', line 11 def prefixes @prefixes end |
#sortable ⇒ Object
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_select_sql ⇒ Object
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).
71 72 73 74 75 76 77 78 |
# File 'lib/thinking_sphinx/field.rb', line 71 def to_select_sql clause = columns_with_prefixes.join(', ') clause = adapter.concatenate(clause) if concat_ws? #~ clause = adapter.group_concatenate(clause) if is_many? "#{clause} AS #{quote_column(unique_name)}" end |