sayso-thinking-sphinx 2.0.3.001
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENCE +20 -0
- data/README.textile +251 -0
- data/VERSION +1 -0
- data/features/abstract_inheritance.feature +10 -0
- data/features/alternate_primary_key.feature +27 -0
- data/features/attribute_transformation.feature +22 -0
- data/features/attribute_updates.feature +77 -0
- data/features/deleting_instances.feature +67 -0
- data/features/direct_attributes.feature +11 -0
- data/features/excerpts.feature +21 -0
- data/features/extensible_delta_indexing.feature +9 -0
- data/features/facets.feature +88 -0
- data/features/facets_across_model.feature +29 -0
- data/features/field_sorting.feature +18 -0
- data/features/handling_edits.feature +94 -0
- data/features/retry_stale_indexes.feature +24 -0
- data/features/searching_across_models.feature +20 -0
- data/features/searching_by_index.feature +40 -0
- data/features/searching_by_model.feature +168 -0
- data/features/searching_with_find_arguments.feature +56 -0
- data/features/sphinx_detection.feature +25 -0
- data/features/sphinx_scopes.feature +68 -0
- data/features/step_definitions/alpha_steps.rb +16 -0
- data/features/step_definitions/beta_steps.rb +7 -0
- data/features/step_definitions/common_steps.rb +197 -0
- data/features/step_definitions/extensible_delta_indexing_steps.rb +7 -0
- data/features/step_definitions/facet_steps.rb +96 -0
- data/features/step_definitions/find_arguments_steps.rb +36 -0
- data/features/step_definitions/gamma_steps.rb +15 -0
- data/features/step_definitions/scope_steps.rb +19 -0
- data/features/step_definitions/search_steps.rb +94 -0
- data/features/step_definitions/sphinx_steps.rb +35 -0
- data/features/sti_searching.feature +19 -0
- data/features/support/env.rb +27 -0
- data/features/support/lib/generic_delta_handler.rb +8 -0
- data/features/thinking_sphinx/database.example.yml +3 -0
- data/features/thinking_sphinx/db/fixtures/alphas.rb +10 -0
- data/features/thinking_sphinx/db/fixtures/authors.rb +1 -0
- data/features/thinking_sphinx/db/fixtures/betas.rb +11 -0
- data/features/thinking_sphinx/db/fixtures/boxes.rb +9 -0
- data/features/thinking_sphinx/db/fixtures/categories.rb +1 -0
- data/features/thinking_sphinx/db/fixtures/cats.rb +3 -0
- data/features/thinking_sphinx/db/fixtures/comments.rb +24 -0
- data/features/thinking_sphinx/db/fixtures/developers.rb +31 -0
- data/features/thinking_sphinx/db/fixtures/dogs.rb +3 -0
- data/features/thinking_sphinx/db/fixtures/extensible_betas.rb +10 -0
- data/features/thinking_sphinx/db/fixtures/foxes.rb +3 -0
- data/features/thinking_sphinx/db/fixtures/gammas.rb +10 -0
- data/features/thinking_sphinx/db/fixtures/music.rb +4 -0
- data/features/thinking_sphinx/db/fixtures/people.rb +1001 -0
- data/features/thinking_sphinx/db/fixtures/posts.rb +6 -0
- data/features/thinking_sphinx/db/fixtures/robots.rb +14 -0
- data/features/thinking_sphinx/db/fixtures/tags.rb +27 -0
- data/features/thinking_sphinx/db/migrations/create_alphas.rb +8 -0
- data/features/thinking_sphinx/db/migrations/create_animals.rb +5 -0
- data/features/thinking_sphinx/db/migrations/create_authors.rb +3 -0
- data/features/thinking_sphinx/db/migrations/create_authors_posts.rb +6 -0
- data/features/thinking_sphinx/db/migrations/create_betas.rb +5 -0
- data/features/thinking_sphinx/db/migrations/create_boxes.rb +5 -0
- data/features/thinking_sphinx/db/migrations/create_categories.rb +3 -0
- data/features/thinking_sphinx/db/migrations/create_comments.rb +10 -0
- data/features/thinking_sphinx/db/migrations/create_developers.rb +7 -0
- data/features/thinking_sphinx/db/migrations/create_extensible_betas.rb +5 -0
- data/features/thinking_sphinx/db/migrations/create_gammas.rb +3 -0
- data/features/thinking_sphinx/db/migrations/create_genres.rb +3 -0
- data/features/thinking_sphinx/db/migrations/create_music.rb +6 -0
- data/features/thinking_sphinx/db/migrations/create_people.rb +13 -0
- data/features/thinking_sphinx/db/migrations/create_posts.rb +5 -0
- data/features/thinking_sphinx/db/migrations/create_robots.rb +4 -0
- data/features/thinking_sphinx/db/migrations/create_taggings.rb +5 -0
- data/features/thinking_sphinx/db/migrations/create_tags.rb +4 -0
- data/features/thinking_sphinx/models/alpha.rb +23 -0
- data/features/thinking_sphinx/models/andrew.rb +17 -0
- data/features/thinking_sphinx/models/animal.rb +5 -0
- data/features/thinking_sphinx/models/author.rb +3 -0
- data/features/thinking_sphinx/models/beta.rb +13 -0
- data/features/thinking_sphinx/models/box.rb +8 -0
- data/features/thinking_sphinx/models/cat.rb +3 -0
- data/features/thinking_sphinx/models/category.rb +4 -0
- data/features/thinking_sphinx/models/comment.rb +10 -0
- data/features/thinking_sphinx/models/developer.rb +20 -0
- data/features/thinking_sphinx/models/dog.rb +3 -0
- data/features/thinking_sphinx/models/extensible_beta.rb +9 -0
- data/features/thinking_sphinx/models/fox.rb +5 -0
- data/features/thinking_sphinx/models/gamma.rb +5 -0
- data/features/thinking_sphinx/models/genre.rb +3 -0
- data/features/thinking_sphinx/models/medium.rb +5 -0
- data/features/thinking_sphinx/models/music.rb +8 -0
- data/features/thinking_sphinx/models/person.rb +24 -0
- data/features/thinking_sphinx/models/post.rb +21 -0
- data/features/thinking_sphinx/models/robot.rb +12 -0
- data/features/thinking_sphinx/models/tag.rb +3 -0
- data/features/thinking_sphinx/models/tagging.rb +4 -0
- data/lib/cucumber/thinking_sphinx/external_world.rb +12 -0
- data/lib/cucumber/thinking_sphinx/internal_world.rb +127 -0
- data/lib/cucumber/thinking_sphinx/sql_logger.rb +20 -0
- data/lib/thinking-sphinx.rb +1 -0
- data/lib/thinking_sphinx.rb +301 -0
- data/lib/thinking_sphinx/action_controller.rb +31 -0
- data/lib/thinking_sphinx/active_record.rb +352 -0
- data/lib/thinking_sphinx/active_record/attribute_updates.rb +52 -0
- data/lib/thinking_sphinx/active_record/delta.rb +92 -0
- data/lib/thinking_sphinx/active_record/has_many_association.rb +36 -0
- data/lib/thinking_sphinx/active_record/has_many_association_with_scopes.rb +21 -0
- data/lib/thinking_sphinx/active_record/log_subscriber.rb +61 -0
- data/lib/thinking_sphinx/active_record/scopes.rb +93 -0
- data/lib/thinking_sphinx/adapters/abstract_adapter.rb +87 -0
- data/lib/thinking_sphinx/adapters/mysql_adapter.rb +58 -0
- data/lib/thinking_sphinx/adapters/postgresql_adapter.rb +153 -0
- data/lib/thinking_sphinx/association.rb +169 -0
- data/lib/thinking_sphinx/attribute.rb +389 -0
- data/lib/thinking_sphinx/auto_version.rb +38 -0
- data/lib/thinking_sphinx/bundled_search.rb +44 -0
- data/lib/thinking_sphinx/class_facet.rb +16 -0
- data/lib/thinking_sphinx/configuration.rb +355 -0
- data/lib/thinking_sphinx/context.rb +76 -0
- data/lib/thinking_sphinx/core/string.rb +15 -0
- data/lib/thinking_sphinx/deltas.rb +28 -0
- data/lib/thinking_sphinx/deltas/default_delta.rb +62 -0
- data/lib/thinking_sphinx/deploy/capistrano.rb +101 -0
- data/lib/thinking_sphinx/excerpter.rb +23 -0
- data/lib/thinking_sphinx/facet.rb +127 -0
- data/lib/thinking_sphinx/facet_search.rb +166 -0
- data/lib/thinking_sphinx/field.rb +82 -0
- data/lib/thinking_sphinx/index.rb +157 -0
- data/lib/thinking_sphinx/index/builder.rb +312 -0
- data/lib/thinking_sphinx/index/faux_column.rb +118 -0
- data/lib/thinking_sphinx/join.rb +37 -0
- data/lib/thinking_sphinx/property.rb +185 -0
- data/lib/thinking_sphinx/railtie.rb +46 -0
- data/lib/thinking_sphinx/search.rb +950 -0
- data/lib/thinking_sphinx/search_methods.rb +439 -0
- data/lib/thinking_sphinx/source.rb +163 -0
- data/lib/thinking_sphinx/source/internal_properties.rb +46 -0
- data/lib/thinking_sphinx/source/sql.rb +148 -0
- data/lib/thinking_sphinx/tasks.rb +139 -0
- data/lib/thinking_sphinx/test.rb +55 -0
- data/spec/thinking_sphinx/active_record/delta_spec.rb +128 -0
- data/spec/thinking_sphinx/active_record/has_many_association_spec.rb +72 -0
- data/spec/thinking_sphinx/active_record/scopes_spec.rb +176 -0
- data/spec/thinking_sphinx/active_record_spec.rb +576 -0
- data/spec/thinking_sphinx/adapters/abstract_adapter_spec.rb +145 -0
- data/spec/thinking_sphinx/association_spec.rb +216 -0
- data/spec/thinking_sphinx/attribute_spec.rb +560 -0
- data/spec/thinking_sphinx/auto_version_spec.rb +63 -0
- data/spec/thinking_sphinx/configuration_spec.rb +288 -0
- data/spec/thinking_sphinx/context_spec.rb +128 -0
- data/spec/thinking_sphinx/core/array_spec.rb +9 -0
- data/spec/thinking_sphinx/core/string_spec.rb +9 -0
- data/spec/thinking_sphinx/excerpter_spec.rb +49 -0
- data/spec/thinking_sphinx/facet_search_spec.rb +170 -0
- data/spec/thinking_sphinx/facet_spec.rb +359 -0
- data/spec/thinking_sphinx/field_spec.rb +127 -0
- data/spec/thinking_sphinx/index/builder_spec.rb +508 -0
- data/spec/thinking_sphinx/index/faux_column_spec.rb +36 -0
- data/spec/thinking_sphinx/index_spec.rb +183 -0
- data/spec/thinking_sphinx/search_methods_spec.rb +156 -0
- data/spec/thinking_sphinx/search_spec.rb +1387 -0
- data/spec/thinking_sphinx/source_spec.rb +253 -0
- data/spec/thinking_sphinx/test_spec.rb +20 -0
- data/spec/thinking_sphinx_spec.rb +203 -0
- data/tasks/distribution.rb +33 -0
- data/tasks/testing.rb +80 -0
- metadata +509 -0
@@ -0,0 +1,169 @@
|
|
1
|
+
module ThinkingSphinx
|
2
|
+
# Association tracks a specific reflection and join to reference data that
|
3
|
+
# isn't in the base model. Very much an internal class for Thinking Sphinx -
|
4
|
+
# perhaps because I feel it's not as strong (or simple) as most of the rest.
|
5
|
+
#
|
6
|
+
class Association
|
7
|
+
attr_accessor :parent, :reflection, :join
|
8
|
+
|
9
|
+
# Create a new association by passing in the parent association, and the
|
10
|
+
# corresponding reflection instance. If there is no parent, pass in nil.
|
11
|
+
#
|
12
|
+
# top = Association.new nil, top_reflection
|
13
|
+
# child = Association.new top, child_reflection
|
14
|
+
#
|
15
|
+
def initialize(parent, reflection)
|
16
|
+
@parent, @reflection = parent, reflection
|
17
|
+
@children = {}
|
18
|
+
end
|
19
|
+
|
20
|
+
# Get the children associations for a given association name. The only time
|
21
|
+
# that there'll actually be more than one association is when the
|
22
|
+
# relationship is polymorphic. To keep things simple though, it will always
|
23
|
+
# be an Array that gets returned (an empty one if no matches).
|
24
|
+
#
|
25
|
+
# # where pages is an association on the class tied to the reflection.
|
26
|
+
# association.children(:pages)
|
27
|
+
#
|
28
|
+
def children(assoc)
|
29
|
+
@children[assoc] ||= Association.children(@reflection.klass, assoc, self)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Get the children associations for a given class, association name and
|
33
|
+
# parent association. Much like the instance method of the same name, it
|
34
|
+
# will return an empty array if no associations have the name, and only
|
35
|
+
# have multiple association instances if the underlying relationship is
|
36
|
+
# polymorphic.
|
37
|
+
#
|
38
|
+
# Association.children(User, :pages, user_association)
|
39
|
+
#
|
40
|
+
def self.children(klass, assoc, parent=nil)
|
41
|
+
ref = klass.reflect_on_association(assoc)
|
42
|
+
|
43
|
+
return [] if ref.nil?
|
44
|
+
return [Association.new(parent, ref)] unless ref.options[:polymorphic]
|
45
|
+
|
46
|
+
# association is polymorphic - create associations for each
|
47
|
+
# non-polymorphic reflection.
|
48
|
+
polymorphic_classes(ref).collect { |poly_class|
|
49
|
+
reflection = depolymorphic_reflection(ref, poly_class)
|
50
|
+
klass.reflections[reflection.name] = reflection
|
51
|
+
Association.new parent, reflection
|
52
|
+
}
|
53
|
+
end
|
54
|
+
|
55
|
+
# Link up the join for this model from a base join - and set parent
|
56
|
+
# associations' joins recursively.
|
57
|
+
#
|
58
|
+
def join_to(base_join)
|
59
|
+
parent.join_to(base_join) if parent && parent.join.nil?
|
60
|
+
|
61
|
+
@join ||= ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation.new(
|
62
|
+
@reflection, base_join, parent ? parent.join : base_join.joins.first
|
63
|
+
)
|
64
|
+
end
|
65
|
+
|
66
|
+
def arel_join
|
67
|
+
@join.join_type = Arel::OuterJoin
|
68
|
+
@join.options[:conditions].gsub!(/::ts_join_alias::/,
|
69
|
+
"#{@reflection.klass.connection.quote_table_name(@join.parent.aliased_table_name)}"
|
70
|
+
) if @join.options[:conditions].is_a?(String)
|
71
|
+
|
72
|
+
@join
|
73
|
+
end
|
74
|
+
|
75
|
+
# Returns true if the association - or a parent - is a has_many or
|
76
|
+
# has_and_belongs_to_many.
|
77
|
+
#
|
78
|
+
def is_many?
|
79
|
+
case @reflection.macro
|
80
|
+
when :has_many, :has_and_belongs_to_many
|
81
|
+
true
|
82
|
+
else
|
83
|
+
@parent ? @parent.is_many? : false
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
# Returns an array of all the associations that lead to this one - starting
|
88
|
+
# with the top level all the way to the current association object.
|
89
|
+
#
|
90
|
+
def ancestors
|
91
|
+
(parent ? parent.ancestors : []) << self
|
92
|
+
end
|
93
|
+
|
94
|
+
def has_column?(column)
|
95
|
+
@reflection.klass.column_names.include?(column.to_s)
|
96
|
+
end
|
97
|
+
|
98
|
+
def primary_key_from_reflection
|
99
|
+
if @reflection.options[:through]
|
100
|
+
@reflection.source_reflection.options[:foreign_key] ||
|
101
|
+
@reflection.source_reflection.primary_key_name
|
102
|
+
elsif @reflection.macro == :has_and_belongs_to_many
|
103
|
+
@reflection.association_foreign_key
|
104
|
+
else
|
105
|
+
nil
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def table
|
110
|
+
if @reflection.options[:through] ||
|
111
|
+
@reflection.macro == :has_and_belongs_to_many
|
112
|
+
@join.aliased_join_table_name
|
113
|
+
else
|
114
|
+
@join.aliased_table_name
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
private
|
119
|
+
|
120
|
+
def self.depolymorphic_reflection(reflection, klass)
|
121
|
+
::ActiveRecord::Reflection::AssociationReflection.new(
|
122
|
+
reflection.macro,
|
123
|
+
"#{reflection.name}_#{klass.name}".to_sym,
|
124
|
+
casted_options(klass, reflection),
|
125
|
+
reflection.active_record
|
126
|
+
)
|
127
|
+
end
|
128
|
+
|
129
|
+
# Returns all the objects that could be currently instantiated from a
|
130
|
+
# polymorphic association. This is pretty damn fast if there's an index on
|
131
|
+
# the foreign type column - but if there isn't, it can take a while if you
|
132
|
+
# have a lot of data.
|
133
|
+
#
|
134
|
+
def self.polymorphic_classes(ref)
|
135
|
+
ref.active_record.connection.select_all(
|
136
|
+
"SELECT DISTINCT #{ref.options[:foreign_type]} " +
|
137
|
+
"FROM #{ref.active_record.table_name} " +
|
138
|
+
"WHERE #{ref.options[:foreign_type]} IS NOT NULL"
|
139
|
+
).collect { |row|
|
140
|
+
row[ref.options[:foreign_type]].constantize
|
141
|
+
}
|
142
|
+
end
|
143
|
+
|
144
|
+
# Returns a new set of options for an association that mimics an existing
|
145
|
+
# polymorphic relationship for a specific class. It adds a condition to
|
146
|
+
# filter by the appropriate object.
|
147
|
+
#
|
148
|
+
def self.casted_options(klass, ref)
|
149
|
+
options = ref.options.clone
|
150
|
+
options[:polymorphic] = nil
|
151
|
+
options[:class_name] = klass.name
|
152
|
+
options[:foreign_key] ||= "#{ref.name}_id"
|
153
|
+
|
154
|
+
quoted_foreign_type = klass.connection.quote_column_name ref.options[:foreign_type]
|
155
|
+
case options[:conditions]
|
156
|
+
when nil
|
157
|
+
options[:conditions] = "::ts_join_alias::.#{quoted_foreign_type} = '#{klass.name}'"
|
158
|
+
when Array
|
159
|
+
options[:conditions] << "::ts_join_alias::.#{quoted_foreign_type} = '#{klass.name}'"
|
160
|
+
when Hash
|
161
|
+
options[:conditions].merge!(ref.options[:foreign_type] => klass.name)
|
162
|
+
else
|
163
|
+
options[:conditions] << " AND ::ts_join_alias::.#{quoted_foreign_type} = '#{klass.name}'"
|
164
|
+
end
|
165
|
+
|
166
|
+
options
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
@@ -0,0 +1,389 @@
|
|
1
|
+
module ThinkingSphinx
|
2
|
+
# Attributes - eternally useful when it comes to filtering, sorting or
|
3
|
+
# grouping. This class isn't really useful to you unless you're hacking
|
4
|
+
# around with the internals of Thinking Sphinx - but hey, don't let that
|
5
|
+
# stop you.
|
6
|
+
#
|
7
|
+
# One key thing to remember - if you're using the attribute manually to
|
8
|
+
# generate SQL statements, you'll need to set the base model, and all the
|
9
|
+
# associations. Which can get messy. Use Index.link!, it really helps.
|
10
|
+
#
|
11
|
+
class Attribute < ThinkingSphinx::Property
|
12
|
+
attr_accessor :query_source
|
13
|
+
|
14
|
+
# To create a new attribute, you'll need to pass in either a single Column
|
15
|
+
# or an array of them, and some (optional) options.
|
16
|
+
#
|
17
|
+
# Valid options are:
|
18
|
+
# - :as => :alias_name
|
19
|
+
# - :type => :attribute_type
|
20
|
+
# - :source => :field, :query, :ranged_query
|
21
|
+
#
|
22
|
+
# Alias is only required in three circumstances: when there's
|
23
|
+
# another attribute or field with the same name, when the column name is
|
24
|
+
# 'id', or when there's more than one column.
|
25
|
+
#
|
26
|
+
# Type is not required, unless you want to force a column to be a certain
|
27
|
+
# type (but keep in mind the value will not be CASTed in the SQL
|
28
|
+
# statements). The only time you really need to use this is when the type
|
29
|
+
# can't be figured out by the column - ie: when not actually using a
|
30
|
+
# database column as your source.
|
31
|
+
#
|
32
|
+
# Source is only used for multi-value attributes (MVA). By default this will
|
33
|
+
# use a left-join and a group_concat to obtain the values. For better performance
|
34
|
+
# during indexing it can be beneficial to let Sphinx use a separate query to retrieve
|
35
|
+
# all document,value-pairs.
|
36
|
+
# Either :query or :ranged_query will enable this feature, where :ranged_query will cause
|
37
|
+
# the query to be executed incremental.
|
38
|
+
#
|
39
|
+
# Example usage:
|
40
|
+
#
|
41
|
+
# Attribute.new(
|
42
|
+
# Column.new(:created_at)
|
43
|
+
# )
|
44
|
+
#
|
45
|
+
# Attribute.new(
|
46
|
+
# Column.new(:posts, :id),
|
47
|
+
# :as => :post_ids
|
48
|
+
# )
|
49
|
+
#
|
50
|
+
# Attribute.new(
|
51
|
+
# Column.new(:posts, :id),
|
52
|
+
# :as => :post_ids,
|
53
|
+
# :source => :ranged_query
|
54
|
+
# )
|
55
|
+
#
|
56
|
+
# Attribute.new(
|
57
|
+
# [Column.new(:pages, :id), Column.new(:articles, :id)],
|
58
|
+
# :as => :content_ids
|
59
|
+
# )
|
60
|
+
#
|
61
|
+
# Attribute.new(
|
62
|
+
# Column.new("NOW()"),
|
63
|
+
# :as => :indexed_at,
|
64
|
+
# :type => :datetime
|
65
|
+
# )
|
66
|
+
#
|
67
|
+
# If you're creating attributes for latitude and longitude, don't forget
|
68
|
+
# that Sphinx expects these values to be in radians.
|
69
|
+
#
|
70
|
+
def initialize(source, columns, options = {})
|
71
|
+
super
|
72
|
+
|
73
|
+
@type = options[:type]
|
74
|
+
@query_source = options[:source]
|
75
|
+
@crc = options[:crc]
|
76
|
+
|
77
|
+
@type ||= :multi unless @query_source.nil?
|
78
|
+
if @type == :string && @crc
|
79
|
+
@type = is_many? ? :multi : :integer
|
80
|
+
end
|
81
|
+
|
82
|
+
source.attributes << self
|
83
|
+
end
|
84
|
+
|
85
|
+
# Get the part of the SELECT clause related to this attribute. Don't forget
|
86
|
+
# to set your model and associations first though.
|
87
|
+
#
|
88
|
+
# This will concatenate strings and arrays of integers, and convert
|
89
|
+
# datetimes to timestamps, as needed.
|
90
|
+
#
|
91
|
+
def to_select_sql
|
92
|
+
return nil unless include_as_association? && available?
|
93
|
+
|
94
|
+
separator = all_ints? || all_datetimes? || @crc ? ',' : ' '
|
95
|
+
|
96
|
+
clause = columns_with_prefixes.collect { |column|
|
97
|
+
case type
|
98
|
+
when :string
|
99
|
+
adapter.convert_nulls(column)
|
100
|
+
when :datetime
|
101
|
+
adapter.cast_to_datetime(column)
|
102
|
+
when :multi
|
103
|
+
column = adapter.cast_to_datetime(column) if is_many_datetimes?
|
104
|
+
column = adapter.convert_nulls(column, '0') if is_many_ints?
|
105
|
+
column
|
106
|
+
else
|
107
|
+
column
|
108
|
+
end
|
109
|
+
}.join(', ')
|
110
|
+
|
111
|
+
clause = adapter.crc(clause) if @crc
|
112
|
+
clause = adapter.concatenate(clause, separator) if concat_ws?
|
113
|
+
clause = adapter.group_concatenate(clause, separator) if is_many?
|
114
|
+
clause = adapter.downcase(clause) if insensitive?
|
115
|
+
|
116
|
+
"#{clause} AS #{quote_column(unique_name)}"
|
117
|
+
end
|
118
|
+
|
119
|
+
def type_to_config
|
120
|
+
{
|
121
|
+
:multi => :sql_attr_multi,
|
122
|
+
:datetime => :sql_attr_timestamp,
|
123
|
+
:string => :sql_attr_str2ordinal,
|
124
|
+
:float => :sql_attr_float,
|
125
|
+
:boolean => :sql_attr_bool,
|
126
|
+
:integer => :sql_attr_uint,
|
127
|
+
:bigint => :sql_attr_bigint
|
128
|
+
}[type]
|
129
|
+
end
|
130
|
+
|
131
|
+
def include_as_association?
|
132
|
+
! (type == :multi && (query_source == :query || query_source == :ranged_query))
|
133
|
+
end
|
134
|
+
|
135
|
+
# Returns the configuration value that should be used for
|
136
|
+
# the attribute.
|
137
|
+
# Special case is the multi-valued attribute that needs some
|
138
|
+
# extra configuration.
|
139
|
+
#
|
140
|
+
def config_value(offset = nil, delta = false)
|
141
|
+
if type == :multi
|
142
|
+
multi_config = include_as_association? ? "field" :
|
143
|
+
source_value(offset, delta).gsub(/\s+/m, " ").strip
|
144
|
+
"uint #{unique_name} from #{multi_config}"
|
145
|
+
else
|
146
|
+
unique_name
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
# Returns the type of the column. If that's not already set, it returns
|
151
|
+
# :multi if there's the possibility of more than one value, :string if
|
152
|
+
# there's more than one association, otherwise it figures out what the
|
153
|
+
# actual column's datatype is and returns that.
|
154
|
+
#
|
155
|
+
def type
|
156
|
+
@type ||= begin
|
157
|
+
base_type = case
|
158
|
+
when is_many?, is_many_ints?
|
159
|
+
:multi
|
160
|
+
when @associations.values.flatten.length > 1
|
161
|
+
:string
|
162
|
+
else
|
163
|
+
translated_type_from_database
|
164
|
+
end
|
165
|
+
|
166
|
+
if base_type == :string && @crc
|
167
|
+
base_type = :integer
|
168
|
+
else
|
169
|
+
@crc = false unless base_type == :multi && is_many_strings? && @crc
|
170
|
+
end
|
171
|
+
|
172
|
+
base_type
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
def updatable?
|
177
|
+
[:integer, :datetime, :boolean].include?(type) && !is_string?
|
178
|
+
end
|
179
|
+
|
180
|
+
def live_value(instance)
|
181
|
+
object = instance
|
182
|
+
column = @columns.first
|
183
|
+
column.__stack.each { |method|
|
184
|
+
object = object.send(method)
|
185
|
+
return sphinx_value(nil) if object.nil?
|
186
|
+
}
|
187
|
+
|
188
|
+
sphinx_value object.send(column.__name)
|
189
|
+
end
|
190
|
+
|
191
|
+
def all_ints?
|
192
|
+
all_of_type?(:integer)
|
193
|
+
end
|
194
|
+
|
195
|
+
def all_datetimes?
|
196
|
+
all_of_type?(:datetime, :date, :timestamp)
|
197
|
+
end
|
198
|
+
|
199
|
+
def all_strings?
|
200
|
+
all_of_type?(:string, :text)
|
201
|
+
end
|
202
|
+
|
203
|
+
private
|
204
|
+
|
205
|
+
def source_value(offset, delta)
|
206
|
+
if is_string?
|
207
|
+
return "#{query_source.to_s.dasherize}; #{columns.first.__name}"
|
208
|
+
end
|
209
|
+
|
210
|
+
query = query(offset)
|
211
|
+
|
212
|
+
if query_source == :ranged_query
|
213
|
+
query += query_clause
|
214
|
+
query += " AND #{query_delta.strip}" if delta
|
215
|
+
"ranged-query; #{query}; #{range_query}"
|
216
|
+
else
|
217
|
+
query += " WHERE #{query_delta.strip}" if delta
|
218
|
+
"query; #{query}"
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
def query(offset)
|
223
|
+
base_assoc = base_association_for_mva
|
224
|
+
end_assoc = end_association_for_mva
|
225
|
+
raise "Could not determine SQL for MVA" if base_assoc.nil?
|
226
|
+
|
227
|
+
relation = Arel::Table.new(base_assoc.table)
|
228
|
+
|
229
|
+
association_joins.each do |join|
|
230
|
+
relation = relation.join(join.relation, Arel::OuterJoin).
|
231
|
+
on(*join.association_join)
|
232
|
+
end
|
233
|
+
|
234
|
+
relation = relation.project "#{foreign_key_for_mva base_assoc} #{ThinkingSphinx.unique_id_expression(offset)} AS #{quote_column('id')}, #{primary_key_for_mva(end_assoc)} AS #{quote_column(unique_name)}"
|
235
|
+
|
236
|
+
relation.to_sql
|
237
|
+
end
|
238
|
+
|
239
|
+
def query_clause
|
240
|
+
foreign_key = foreign_key_for_mva base_association_for_mva
|
241
|
+
" WHERE #{foreign_key} >= $start AND #{foreign_key} <= $end"
|
242
|
+
end
|
243
|
+
|
244
|
+
def query_delta
|
245
|
+
foreign_key = foreign_key_for_mva base_association_for_mva
|
246
|
+
<<-SQL
|
247
|
+
#{foreign_key} IN (SELECT #{quote_column model.primary_key}
|
248
|
+
FROM #{model.quoted_table_name}
|
249
|
+
WHERE #{@source.index.delta_object.clause(model, true)})
|
250
|
+
SQL
|
251
|
+
end
|
252
|
+
|
253
|
+
def range_query
|
254
|
+
assoc = base_association_for_mva
|
255
|
+
foreign_key = foreign_key_for_mva assoc
|
256
|
+
"SELECT MIN(#{foreign_key}), MAX(#{foreign_key}) FROM #{quote_table_name assoc.table}"
|
257
|
+
end
|
258
|
+
|
259
|
+
def primary_key_for_mva(assoc)
|
260
|
+
quote_with_table(
|
261
|
+
assoc.table, assoc.primary_key_from_reflection || columns.first.__name
|
262
|
+
)
|
263
|
+
end
|
264
|
+
|
265
|
+
def foreign_key_for_mva(assoc)
|
266
|
+
quote_with_table assoc.table, assoc.reflection.primary_key_name
|
267
|
+
end
|
268
|
+
|
269
|
+
def end_association_for_mva
|
270
|
+
@association_for_mva ||= associations[columns.first].detect { |assoc|
|
271
|
+
assoc.has_column?(columns.first.__name)
|
272
|
+
}
|
273
|
+
end
|
274
|
+
|
275
|
+
def base_association_for_mva
|
276
|
+
@first_association_for_mva ||= begin
|
277
|
+
assoc = end_association_for_mva
|
278
|
+
while !assoc.parent.nil?
|
279
|
+
assoc = assoc.parent
|
280
|
+
end
|
281
|
+
|
282
|
+
assoc
|
283
|
+
end
|
284
|
+
end
|
285
|
+
|
286
|
+
def association_joins
|
287
|
+
joins = []
|
288
|
+
assoc = end_association_for_mva
|
289
|
+
while assoc != base_association_for_mva
|
290
|
+
joins << assoc.join
|
291
|
+
assoc = assoc.parent
|
292
|
+
end
|
293
|
+
|
294
|
+
joins
|
295
|
+
end
|
296
|
+
|
297
|
+
def is_many_ints?
|
298
|
+
concat_ws? && all_ints?
|
299
|
+
end
|
300
|
+
|
301
|
+
def is_many_datetimes?
|
302
|
+
is_many? && all_datetimes?
|
303
|
+
end
|
304
|
+
|
305
|
+
def is_many_strings?
|
306
|
+
is_many? && all_strings?
|
307
|
+
end
|
308
|
+
|
309
|
+
def translated_type_from_database
|
310
|
+
case type_from_db = type_from_database
|
311
|
+
when :integer
|
312
|
+
integer_type_from_db
|
313
|
+
when :datetime, :string, :float, :boolean
|
314
|
+
type_from_db
|
315
|
+
when :decimal
|
316
|
+
:float
|
317
|
+
when :timestamp, :date
|
318
|
+
:datetime
|
319
|
+
else
|
320
|
+
raise <<-MESSAGE
|
321
|
+
|
322
|
+
Cannot automatically map attribute #{unique_name} in #{@model.name} to an
|
323
|
+
equivalent Sphinx type (integer, float, boolean, datetime, string as ordinal).
|
324
|
+
You could try to explicitly convert the column's value in your define_index
|
325
|
+
block:
|
326
|
+
has "CAST(column AS INT)", :type => :integer, :as => :column
|
327
|
+
MESSAGE
|
328
|
+
end
|
329
|
+
end
|
330
|
+
|
331
|
+
def type_from_database
|
332
|
+
column = column_from_db
|
333
|
+
column.nil? ? nil : column.type
|
334
|
+
end
|
335
|
+
|
336
|
+
def integer_type_from_db
|
337
|
+
column = column_from_db
|
338
|
+
return nil if column.nil?
|
339
|
+
|
340
|
+
case column.sql_type
|
341
|
+
when adapter.bigint_pattern
|
342
|
+
:bigint
|
343
|
+
else
|
344
|
+
:integer
|
345
|
+
end
|
346
|
+
end
|
347
|
+
|
348
|
+
def column_from_db
|
349
|
+
klass = @associations.values.flatten.first ?
|
350
|
+
@associations.values.flatten.first.reflection.klass : @model
|
351
|
+
|
352
|
+
klass.columns.detect { |col|
|
353
|
+
@columns.collect { |c| c.__name.to_s }.include? col.name
|
354
|
+
}
|
355
|
+
end
|
356
|
+
|
357
|
+
def all_of_type?(*column_types)
|
358
|
+
@columns.all? { |col|
|
359
|
+
klasses = @associations[col].empty? ? [@model] :
|
360
|
+
@associations[col].collect { |assoc| assoc.reflection.klass }
|
361
|
+
klasses.all? { |klass|
|
362
|
+
column = klass.columns.detect { |column| column.name == col.__name.to_s }
|
363
|
+
!column.nil? && column_types.include?(column.type)
|
364
|
+
}
|
365
|
+
}
|
366
|
+
end
|
367
|
+
|
368
|
+
def sphinx_value(value)
|
369
|
+
case value
|
370
|
+
when TrueClass
|
371
|
+
1
|
372
|
+
when FalseClass, NilClass
|
373
|
+
0
|
374
|
+
when Time
|
375
|
+
value.to_i
|
376
|
+
when Date
|
377
|
+
value.to_time.to_i
|
378
|
+
when String
|
379
|
+
value.to_crc32
|
380
|
+
else
|
381
|
+
value
|
382
|
+
end
|
383
|
+
end
|
384
|
+
|
385
|
+
def insensitive?
|
386
|
+
@sortable == :insensitive
|
387
|
+
end
|
388
|
+
end
|
389
|
+
end
|