Several months later... *ahem*
I've just added :sortable => :insensitive (ie: case-insensitive) for fields,
which automatically downcases strings. Check out 1.4.0 (Rails 1 & 2) or Rails
2.0 (Rails 3).
Thanks for the inspiration code Eric.
Cheers
--
Pat
On 12/02/2010, at 3:06 AM, Eric wrote:
> One thing sphinx doesn't do very well is sort strings. In general
> when sorting, you need to upper or lowercase the strings in the sphinx
> config file. I created a config extension that will do this
> automatically that injects itself when a string sort attribute is
> added. Does this seem like a reasonable approach?
>
> module ThinkingSphinx
> class Index
> class Builder
> private
> def add_sort_attribute(field, options)
> add_internal_attribute field, options.merge({:perform_upper =>
> true}), "_sort"
> end
>
> end
> end
>
> class Attribute < ThinkingSphinx::Property
>
> def initialize(source, columns, options = {})
> super
>
> @type = options[:type]
> @query_source = options[:source]
> @crc = options[:crc]
> @perform_upper = options[:perform_upper]
>
> @type ||= :multi unless @query_source.nil?
> if @type == :string && @crc
> @type = is_many? ? :multi : :integer
> end
>
> source.attributes << self
> end
>
> def to_select_sql
> return nil unless include_as_association?
>
> separator = all_ints? || all_datetimes? || @crc ? ',' : ' '
>
> clause = @columns.collect { |column|
> part = column_with_prefix(column)
> case type
> when :string
> adapter.convert_nulls(part)
> when :datetime
> adapter.cast_to_datetime(part)
> when :multi
> part = adapter.cast_to_datetime(part) if
> is_many_datetimes?
> part = adapter.convert_nulls(part, '0') if is_many_ints?
> part
> else
> part
> end
> }.join(', ')
>
> clause = adapter.crc(clause) if @crc
> clause = adapter.concatenate(clause, separator) if
> concat_ws?
> clause = adapter.group_concatenate(clause, separator) if
> is_many?
> clause = "UPPER(#{clause})" if @perform_upper
>
> "#{clause} AS #{quote_column(unique_name)}"
> end
> end
> end
>
> --
> You received this message because you are subscribed to the Google Groups
> "Thinking Sphinx" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/thinking-sphinx?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"Thinking Sphinx" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/thinking-sphinx?hl=en.