freelancing-god-thinking-sphinx 0.9.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/LICENCE +20 -0
  2. data/README +25 -0
  3. data/lib/riddle.rb +22 -0
  4. data/lib/riddle/client.rb +593 -0
  5. data/lib/riddle/client/filter.rb +44 -0
  6. data/lib/riddle/client/message.rb +65 -0
  7. data/lib/riddle/client/response.rb +84 -0
  8. data/lib/test.rb +46 -0
  9. data/lib/thinking_sphinx.rb +79 -0
  10. data/lib/thinking_sphinx/active_record.rb +115 -0
  11. data/lib/thinking_sphinx/active_record/delta.rb +86 -0
  12. data/lib/thinking_sphinx/active_record/has_many_association.rb +29 -0
  13. data/lib/thinking_sphinx/active_record/search.rb +36 -0
  14. data/lib/thinking_sphinx/association.rb +140 -0
  15. data/lib/thinking_sphinx/attribute.rb +279 -0
  16. data/lib/thinking_sphinx/configuration.rb +275 -0
  17. data/lib/thinking_sphinx/field.rb +186 -0
  18. data/lib/thinking_sphinx/index.rb +234 -0
  19. data/lib/thinking_sphinx/index/builder.rb +197 -0
  20. data/lib/thinking_sphinx/index/faux_column.rb +97 -0
  21. data/lib/thinking_sphinx/rails_additions.rb +56 -0
  22. data/lib/thinking_sphinx/search.rb +413 -0
  23. data/spec/unit/thinking_sphinx/active_record/delta_spec.rb +184 -0
  24. data/spec/unit/thinking_sphinx/active_record/has_many_association_spec.rb +53 -0
  25. data/spec/unit/thinking_sphinx/active_record/search_spec.rb +0 -0
  26. data/spec/unit/thinking_sphinx/active_record_spec.rb +85 -0
  27. data/spec/unit/thinking_sphinx/association_spec.rb +0 -0
  28. data/spec/unit/thinking_sphinx/attribute_spec.rb +73 -0
  29. data/spec/unit/thinking_sphinx/configuration_spec.rb +7 -0
  30. data/spec/unit/thinking_sphinx/field_spec.rb +51 -0
  31. data/spec/unit/thinking_sphinx/index/builder_spec.rb +33 -0
  32. data/spec/unit/thinking_sphinx/index/faux_column_spec.rb +41 -0
  33. data/spec/unit/thinking_sphinx/index_spec.rb +5 -0
  34. data/spec/unit/thinking_sphinx/search_spec.rb +121 -0
  35. data/spec/unit/thinking_sphinx_spec.rb +82 -0
  36. data/tasks/thinking_sphinx_tasks.rake +1 -0
  37. data/tasks/thinking_sphinx_tasks.rb +86 -0
  38. metadata +90 -0
@@ -0,0 +1,29 @@
1
+ module ThinkingSphinx
2
+ module ActiveRecord
3
+ module HasManyAssociation
4
+ def search(*args)
5
+ foreign_key = @reflection.primary_key_name
6
+ stack = [@reflection.options[:through]].compact
7
+
8
+ attribute = nil
9
+ (@reflection.klass.indexes || []).each do |index|
10
+ attribute = index.attributes.detect { |attrib|
11
+ attrib.columns.length == 1 &&
12
+ attrib.columns.first.__name == foreign_key.to_sym &&
13
+ attrib.columns.first.__stack == stack
14
+ }
15
+ break if attribute
16
+ end
17
+
18
+ raise "Missing Attribute for Foreign Key #{foreign_key}" unless attribute
19
+
20
+ options = args.extract_options!
21
+ options[:with] ||= {}
22
+ options[:with][attribute.unique_name] = @owner.id
23
+
24
+ args << options
25
+ @reflection.klass.search(*args)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,36 @@
1
+ module ThinkingSphinx
2
+ module ActiveRecord
3
+ # This module covers the specific model searches - but the syntax is
4
+ # exactly the same as the core Search class - so use that as your refence
5
+ # point.
6
+ #
7
+ module Search
8
+ def self.included(base)
9
+ base.class_eval do
10
+ class << self
11
+ # Searches for results that match the parameters provided. Will only
12
+ # return the ids for the matching objects. See
13
+ # ThinkingSphinx::Search#search for syntax examples.
14
+ #
15
+ def search_for_ids(*args)
16
+ options = args.extract_options!
17
+ options[:class] = self
18
+ args << options
19
+ ThinkingSphinx::Search.search_for_ids(*args)
20
+ end
21
+
22
+ # Searches for results limited to a single model. See
23
+ # ThinkingSphinx::Search#search for syntax examples.
24
+ #
25
+ def search(*args)
26
+ options = args.extract_options!
27
+ options[:class] = self
28
+ args << options
29
+ ThinkingSphinx::Search.search(*args)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,140 @@
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 { |klass|
49
+ Association.new parent, ::ActiveRecord::Reflection::AssociationReflection.new(
50
+ ref.macro,
51
+ "#{ref.name}_#{klass.name}".to_sym,
52
+ casted_options(klass, ref),
53
+ ref.active_record
54
+ )
55
+ }
56
+ end
57
+
58
+ # Link up the join for this model from a base join - and set parent
59
+ # associations' joins recursively.
60
+ #
61
+ def join_to(base_join)
62
+ parent.join_to(base_join) if parent && parent.join.nil?
63
+
64
+ @join ||= ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation.new(
65
+ @reflection, base_join, parent ? parent.join : base_join.joins.first
66
+ )
67
+ end
68
+
69
+ # Returns the association's join SQL statements - and it replaces
70
+ # ::ts_join_alias:: with the aliased table name so the generated reflection
71
+ # join conditions avoid column name collisions.
72
+ #
73
+ def to_sql
74
+ @join.association_join.gsub(/::ts_join_alias::/,
75
+ "#{@reflection.klass.connection.quote_table_name(@join.parent.aliased_table_name)}"
76
+ )
77
+ end
78
+
79
+ # Returns true if the association - or a parent - is a has_many or
80
+ # has_and_belongs_to_many.
81
+ #
82
+ def is_many?
83
+ case @reflection.macro
84
+ when :has_many, :has_and_belongs_to_many
85
+ true
86
+ else
87
+ @parent ? @parent.is_many? : false
88
+ end
89
+ end
90
+
91
+ # Returns an array of all the associations that lead to this one - starting
92
+ # with the top level all the way to the current association object.
93
+ #
94
+ def ancestors
95
+ (parent ? parent.ancestors : []) << self
96
+ end
97
+
98
+ private
99
+
100
+ # Returns all the objects that could be currently instantiated from a
101
+ # polymorphic association. This is pretty damn fast if there's an index on
102
+ # the foreign type column - but if there isn't, it can take a while if you
103
+ # have a lot of data.
104
+ #
105
+ def self.polymorphic_classes(ref)
106
+ ref.active_record.connection.select_all(
107
+ "SELECT DISTINCT #{ref.options[:foreign_type]} " +
108
+ "FROM #{ref.active_record.table_name} " +
109
+ "WHERE #{ref.options[:foreign_type]} IS NOT NULL"
110
+ ).collect { |row|
111
+ row[ref.options[:foreign_type]].constantize
112
+ }
113
+ end
114
+
115
+ # Returns a new set of options for an association that mimics an existing
116
+ # polymorphic relationship for a specific class. It adds a condition to
117
+ # filter by the appropriate object.
118
+ #
119
+ def self.casted_options(klass, ref)
120
+ options = ref.options.clone
121
+ options[:polymorphic] = nil
122
+ options[:class_name] = klass.name
123
+ options[:foreign_key] ||= "#{ref.name}_id"
124
+
125
+ foreign_type = klass.connection.quote_column_name ref.options[:foreign_type]
126
+ case options[:conditions]
127
+ when nil
128
+ options[:conditions] = "::ts_join_alias::.#{foreign_type} = '#{klass.name}'"
129
+ when Array
130
+ options[:conditions] << "::ts_join_alias::.#{foreign_type} = '#{klass.name}'"
131
+ when Hash
132
+ options[:conditions].merge!(foreign_type => klass.name)
133
+ else
134
+ options[:conditions] << " AND ::ts_join_alias::.#{foreign_type} = '#{klass.name}'"
135
+ end
136
+
137
+ options
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,279 @@
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
12
+ attr_accessor :alias, :columns, :associations, :model
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
+ #
21
+ # Alias is only required in three circumstances: when there's
22
+ # another attribute or field with the same name, when the column name is
23
+ # 'id', or when there's more than one column.
24
+ #
25
+ # Type is not required, unless you want to force a column to be a certain
26
+ # type (but keep in mind the value will not be CASTed in the SQL
27
+ # statements). The only time you really need to use this is when the type
28
+ # can't be figured out by the column - ie: when not actually using a
29
+ # database column as your source.
30
+ #
31
+ # Example usage:
32
+ #
33
+ # Attribute.new(
34
+ # Column.new(:created_at)
35
+ # )
36
+ #
37
+ # Attribute.new(
38
+ # Column.new(:posts, :id),
39
+ # :as => :post_ids
40
+ # )
41
+ #
42
+ # Attribute.new(
43
+ # [Column.new(:pages, :id), Column.new(:articles, :id)],
44
+ # :as => :content_ids
45
+ # )
46
+ #
47
+ # Attribute.new(
48
+ # Column.new("NOW()"),
49
+ # :as => :indexed_at,
50
+ # :type => :datetime
51
+ # )
52
+ #
53
+ # If you're creating attributes for latitude and longitude, don't forget
54
+ # that Sphinx expects these values to be in radians.
55
+ #
56
+ def initialize(columns, options = {})
57
+ @columns = Array(columns)
58
+ @associations = {}
59
+
60
+ @alias = options[:as]
61
+ @type = options[:type]
62
+ end
63
+
64
+ # Get the part of the SELECT clause related to this attribute. Don't forget
65
+ # to set your model and associations first though.
66
+ #
67
+ # This will concatenate strings and arrays of integers, and convert
68
+ # datetimes to timestamps, as needed.
69
+ #
70
+ def to_select_sql
71
+ clause = @columns.collect { |column|
72
+ column_with_prefix(column)
73
+ }.join(', ')
74
+
75
+ separator = all_ints? ? ',' : ' '
76
+
77
+ clause = concatenate(clause, separator) if concat_ws?
78
+ clause = group_concatenate(clause, separator) if is_many?
79
+ clause = cast_to_datetime(clause) if type == :datetime
80
+ clause = convert_nulls(clause) if type == :string
81
+
82
+ "#{clause} AS #{quote_column(unique_name)}"
83
+ end
84
+
85
+ # Get the part of the GROUP BY clause related to this attribute - if one is
86
+ # needed. If not, all you'll get back is nil. The latter will happen if
87
+ # there isn't actually a real column to get data from, or if there's
88
+ # multiple data values (read: a has_many or has_and_belongs_to_many
89
+ # association).
90
+ #
91
+ def to_group_sql
92
+ case
93
+ when is_many?, is_string?, ThinkingSphinx.use_group_by_shortcut?
94
+ nil
95
+ else
96
+ @columns.collect { |column|
97
+ column_with_prefix(column)
98
+ }
99
+ end
100
+ end
101
+
102
+ # Generates the appropriate attribute statement for a Sphinx configuration
103
+ # file, depending on the attribute's type.
104
+ #
105
+ def to_sphinx_clause
106
+ case type
107
+ when :multi
108
+ "sql_attr_multi = uint #{unique_name} from field"
109
+ when :datetime
110
+ "sql_attr_timestamp = #{unique_name}"
111
+ when :string
112
+ "sql_attr_str2ordinal = #{unique_name}"
113
+ when :float
114
+ "sql_attr_float = #{unique_name}"
115
+ when :boolean
116
+ "sql_attr_bool = #{unique_name}"
117
+ else
118
+ "sql_attr_uint = #{unique_name}"
119
+ end
120
+ end
121
+
122
+ # Returns the unique name of the attribute - which is either the alias of
123
+ # the attribute, or the name of the only column - if there is only one. If
124
+ # there isn't, there should be an alias. Else things probably won't work.
125
+ # Consider yourself warned.
126
+ #
127
+ def unique_name
128
+ if @columns.length == 1
129
+ @alias || @columns.first.__name
130
+ else
131
+ @alias
132
+ end
133
+ end
134
+
135
+ private
136
+
137
+ def concatenate(clause, separator = ' ')
138
+ case @model.connection.class.name
139
+ when "ActiveRecord::ConnectionAdapters::MysqlAdapter"
140
+ "CONCAT_WS('#{separator}', #{clause})"
141
+ when "ActiveRecord::ConnectionAdapters::PostgreSQLAdapter"
142
+ clause.split(', ').join(" || #{separator} || ")
143
+ else
144
+ clause
145
+ end
146
+ end
147
+
148
+ def group_concatenate(clause, separator = ' ')
149
+ case @model.connection.class.name
150
+ when "ActiveRecord::ConnectionAdapters::MysqlAdapter"
151
+ "GROUP_CONCAT(#{clause} SEPARATOR '#{separator}')"
152
+ when "ActiveRecord::ConnectionAdapters::PostgreSQLAdapter"
153
+ "array_to_string(array_accum(#{clause}), '#{separator}')"
154
+ else
155
+ clause
156
+ end
157
+ end
158
+
159
+ def cast_to_string(clause)
160
+ case @model.connection.class.name
161
+ when "ActiveRecord::ConnectionAdapters::MysqlAdapter"
162
+ "CAST(#{clause} AS CHAR)"
163
+ when "ActiveRecord::ConnectionAdapters::PostgreSQLAdapter"
164
+ clause
165
+ else
166
+ clause
167
+ end
168
+ end
169
+
170
+ def cast_to_datetime(column)
171
+ case @model.connection.class.name
172
+ when "ActiveRecord::ConnectionAdapters::MysqlAdapter"
173
+ "UNIX_TIMESTAMP(#{clause})"
174
+ when "ActiveRecord::ConnectionAdapters::PostgreSQLAdapter"
175
+ clause # Rails' datetimes are timestamps in PostgreSQL
176
+ else
177
+ clause
178
+ end
179
+ end
180
+
181
+ def convert_nulls(column)
182
+ case @model.connection.class.name
183
+ when "ActiveRecord::ConnectionAdapters::MysqlAdapter"
184
+ "IFNULL(#{clause}, '')"
185
+ when "ActiveRecord::ConnectionAdapters::PostgreSQLAdapter"
186
+ "COALESCE(#{clause}, '')"
187
+ else
188
+ clause
189
+ end
190
+ end
191
+
192
+ def quote_column(column)
193
+ @model.connection.quote_column_name(column)
194
+ end
195
+
196
+ # Indication of whether the columns should be concatenated with a space
197
+ # between each value. True if there's either multiple sources or multiple
198
+ # associations.
199
+ #
200
+ def concat_ws?
201
+ multiple_associations? || @columns.length > 1
202
+ end
203
+
204
+ # Checks the association tree for each column - if they're all the same,
205
+ # returns false.
206
+ #
207
+ def multiple_sources?
208
+ first = associations[@columns.first]
209
+
210
+ !@columns.all? { |col| associations[col] == first }
211
+ end
212
+
213
+ # Checks whether any column requires multiple associations (which only
214
+ # happens for polymorphic situations).
215
+ #
216
+ def multiple_associations?
217
+ associations.any? { |col,assocs| assocs.length > 1 }
218
+ end
219
+
220
+ # Builds a column reference tied to the appropriate associations. This
221
+ # dives into the associations hash and their corresponding joins to
222
+ # figure out how to correctly reference a column in SQL.
223
+ #
224
+ def column_with_prefix(column)
225
+ if column.is_string?
226
+ column.__name
227
+ elsif associations[column].empty?
228
+ "#{@model.quoted_table_name}.#{quote_column(column.__name)}"
229
+ else
230
+ associations[column].collect { |assoc|
231
+ "#{@model.connection.quote_table_name(assoc.join.aliased_table_name)}" +
232
+ ".#{quote_column(column.__name)}"
233
+ }.join(', ')
234
+ end
235
+ end
236
+
237
+ # Could there be more than one value related to the parent record? If so,
238
+ # then this will return true. If not, false. It's that simple.
239
+ #
240
+ def is_many?
241
+ associations.values.flatten.any? { |assoc| assoc.is_many? }
242
+ end
243
+
244
+ # Returns true if any of the columns are string values, instead of database
245
+ # column references.
246
+ def is_string?
247
+ columns.all? { |col| col.is_string? }
248
+ end
249
+
250
+ # Returns the type of the column. If that's not already set, it returns
251
+ # :multi if there's the possibility of more than one value, :string if
252
+ # there's more than one association, otherwise it figures out what the
253
+ # actual column's datatype is and returns that.
254
+ def type
255
+ @type ||= case
256
+ when is_many?
257
+ :multi
258
+ when @associations.values.flatten.length > 1
259
+ :string
260
+ else
261
+ klass = @associations.values.flatten.first ?
262
+ @associations.values.flatten.first.reflection.klass : @model
263
+ klass.columns.detect { |col|
264
+ @columns.collect { |c| c.__name.to_s }.include? col.name
265
+ }.type
266
+ end
267
+ end
268
+
269
+ def all_ints?
270
+ @columns.all? { |col|
271
+ klasses = @associations[col].empty? ? [@model] :
272
+ @associations[col].collect { |assoc| assoc.reflection.klass }
273
+ klasses.all? { |klass|
274
+ klass.columns.detect { |column| column.name == col.__name.to_s }.type == :integer
275
+ }
276
+ }
277
+ end
278
+ end
279
+ end