Class ThinkingSphinx::Field
In: lib/thinking_sphinx/field.rb
Parent: ThinkingSphinx::Property

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.

Methods

new   to_select_sql  

Attributes

infixes  [RW] 
prefixes  [RW] 
sortable  [RW] 

Public Class methods

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
  )

Public Instance methods

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).

[Validate]