Class: ThinkingSphinx::Field

Inherits:
Property 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)

Attributes inherited from Property

#admin, #alias, #associations, #columns, #faceted, #model

Instance Method Summary (collapse)

Methods inherited from Property

#admin?, #available?, #changed?, #public?, #to_facet, #to_group_sql, #unique_name

Constructor Details

- (Field) initialize(source, columns, options = {})

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

  • :file => true

  • :with => :attribute # or :wordcount

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
)


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

def initialize(source, columns, options = {})
  super
  
  @sortable = options[:sortable] || false
  @infixes  = options[:infixes]  || false
  @prefixes = options[:prefixes] || false
  @file     = options[:file]     || false
  @with     = options[:with]
  
  source.fields << self
end

Instance Attribute Details

- (Object) infixes

Returns the value of attribute infixes



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

def infixes
  @infixes
end

- (Object) prefixes

Returns the value of attribute prefixes



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

def prefixes
  @prefixes
end

- (Object) sortable

Returns the value of attribute sortable



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

def sortable
  @sortable
end

Instance Method Details

- (Boolean) file?

Returns:

  • (Boolean)


86
87
88
# File 'lib/thinking_sphinx/field.rb', line 86

def file?
  @file
end

- (Object) to_select_sql

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



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

def to_select_sql
  return nil unless available?
  
  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

- (Boolean) with_attribute?

Returns:

  • (Boolean)


90
91
92
# File 'lib/thinking_sphinx/field.rb', line 90

def with_attribute?
  @with == :attribute
end

- (Boolean) with_wordcount?

Returns:

  • (Boolean)


94
95
96
# File 'lib/thinking_sphinx/field.rb', line 94

def with_wordcount?
  @with == :wordcount
end