hawkerb-thinking-sphinx 1.3.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. data/LICENCE +20 -0
  2. data/README.textile +161 -0
  3. data/VERSION +1 -0
  4. data/features/abstract_inheritance.feature +10 -0
  5. data/features/alternate_primary_key.feature +27 -0
  6. data/features/attribute_transformation.feature +22 -0
  7. data/features/attribute_updates.feature +51 -0
  8. data/features/deleting_instances.feature +67 -0
  9. data/features/direct_attributes.feature +11 -0
  10. data/features/excerpts.feature +13 -0
  11. data/features/extensible_delta_indexing.feature +9 -0
  12. data/features/facets.feature +82 -0
  13. data/features/facets_across_model.feature +29 -0
  14. data/features/handling_edits.feature +92 -0
  15. data/features/retry_stale_indexes.feature +24 -0
  16. data/features/searching_across_models.feature +20 -0
  17. data/features/searching_by_index.feature +40 -0
  18. data/features/searching_by_model.feature +175 -0
  19. data/features/searching_with_find_arguments.feature +56 -0
  20. data/features/sphinx_detection.feature +25 -0
  21. data/features/sphinx_scopes.feature +42 -0
  22. data/features/step_definitions/alpha_steps.rb +16 -0
  23. data/features/step_definitions/beta_steps.rb +7 -0
  24. data/features/step_definitions/common_steps.rb +188 -0
  25. data/features/step_definitions/extensible_delta_indexing_steps.rb +7 -0
  26. data/features/step_definitions/facet_steps.rb +96 -0
  27. data/features/step_definitions/find_arguments_steps.rb +36 -0
  28. data/features/step_definitions/gamma_steps.rb +15 -0
  29. data/features/step_definitions/scope_steps.rb +15 -0
  30. data/features/step_definitions/search_steps.rb +89 -0
  31. data/features/step_definitions/sphinx_steps.rb +35 -0
  32. data/features/sti_searching.feature +19 -0
  33. data/features/support/database.example.yml +3 -0
  34. data/features/support/db/fixtures/alphas.rb +10 -0
  35. data/features/support/db/fixtures/authors.rb +1 -0
  36. data/features/support/db/fixtures/betas.rb +10 -0
  37. data/features/support/db/fixtures/boxes.rb +9 -0
  38. data/features/support/db/fixtures/categories.rb +1 -0
  39. data/features/support/db/fixtures/cats.rb +3 -0
  40. data/features/support/db/fixtures/comments.rb +24 -0
  41. data/features/support/db/fixtures/developers.rb +29 -0
  42. data/features/support/db/fixtures/dogs.rb +3 -0
  43. data/features/support/db/fixtures/extensible_betas.rb +10 -0
  44. data/features/support/db/fixtures/foxes.rb +3 -0
  45. data/features/support/db/fixtures/gammas.rb +10 -0
  46. data/features/support/db/fixtures/music.rb +4 -0
  47. data/features/support/db/fixtures/people.rb +1001 -0
  48. data/features/support/db/fixtures/posts.rb +6 -0
  49. data/features/support/db/fixtures/robots.rb +14 -0
  50. data/features/support/db/fixtures/tags.rb +27 -0
  51. data/features/support/db/migrations/create_alphas.rb +8 -0
  52. data/features/support/db/migrations/create_animals.rb +5 -0
  53. data/features/support/db/migrations/create_authors.rb +3 -0
  54. data/features/support/db/migrations/create_authors_posts.rb +6 -0
  55. data/features/support/db/migrations/create_betas.rb +5 -0
  56. data/features/support/db/migrations/create_boxes.rb +5 -0
  57. data/features/support/db/migrations/create_categories.rb +3 -0
  58. data/features/support/db/migrations/create_comments.rb +10 -0
  59. data/features/support/db/migrations/create_developers.rb +9 -0
  60. data/features/support/db/migrations/create_extensible_betas.rb +5 -0
  61. data/features/support/db/migrations/create_gammas.rb +3 -0
  62. data/features/support/db/migrations/create_genres.rb +3 -0
  63. data/features/support/db/migrations/create_music.rb +6 -0
  64. data/features/support/db/migrations/create_people.rb +13 -0
  65. data/features/support/db/migrations/create_posts.rb +5 -0
  66. data/features/support/db/migrations/create_robots.rb +4 -0
  67. data/features/support/db/migrations/create_taggings.rb +5 -0
  68. data/features/support/db/migrations/create_tags.rb +4 -0
  69. data/features/support/env.rb +21 -0
  70. data/features/support/lib/generic_delta_handler.rb +8 -0
  71. data/features/support/models/alpha.rb +22 -0
  72. data/features/support/models/animal.rb +5 -0
  73. data/features/support/models/author.rb +3 -0
  74. data/features/support/models/beta.rb +8 -0
  75. data/features/support/models/box.rb +8 -0
  76. data/features/support/models/cat.rb +3 -0
  77. data/features/support/models/category.rb +4 -0
  78. data/features/support/models/comment.rb +10 -0
  79. data/features/support/models/developer.rb +16 -0
  80. data/features/support/models/dog.rb +3 -0
  81. data/features/support/models/extensible_beta.rb +9 -0
  82. data/features/support/models/fox.rb +5 -0
  83. data/features/support/models/gamma.rb +5 -0
  84. data/features/support/models/genre.rb +3 -0
  85. data/features/support/models/medium.rb +5 -0
  86. data/features/support/models/music.rb +8 -0
  87. data/features/support/models/person.rb +23 -0
  88. data/features/support/models/post.rb +21 -0
  89. data/features/support/models/robot.rb +12 -0
  90. data/features/support/models/tag.rb +3 -0
  91. data/features/support/models/tagging.rb +4 -0
  92. data/lib/cucumber/thinking_sphinx/external_world.rb +8 -0
  93. data/lib/cucumber/thinking_sphinx/internal_world.rb +126 -0
  94. data/lib/cucumber/thinking_sphinx/sql_logger.rb +20 -0
  95. data/lib/thinking_sphinx/active_record/attribute_updates.rb +46 -0
  96. data/lib/thinking_sphinx/active_record/delta.rb +61 -0
  97. data/lib/thinking_sphinx/active_record/has_many_association.rb +29 -0
  98. data/lib/thinking_sphinx/active_record/scopes.rb +75 -0
  99. data/lib/thinking_sphinx/active_record.rb +367 -0
  100. data/lib/thinking_sphinx/adapters/abstract_adapter.rb +42 -0
  101. data/lib/thinking_sphinx/adapters/mysql_adapter.rb +54 -0
  102. data/lib/thinking_sphinx/adapters/postgresql_adapter.rb +143 -0
  103. data/lib/thinking_sphinx/association.rb +164 -0
  104. data/lib/thinking_sphinx/attribute.rb +362 -0
  105. data/lib/thinking_sphinx/auto_version.rb +22 -0
  106. data/lib/thinking_sphinx/class_facet.rb +15 -0
  107. data/lib/thinking_sphinx/configuration.rb +282 -0
  108. data/lib/thinking_sphinx/context.rb +68 -0
  109. data/lib/thinking_sphinx/core/array.rb +7 -0
  110. data/lib/thinking_sphinx/core/string.rb +15 -0
  111. data/lib/thinking_sphinx/deltas/default_delta.rb +62 -0
  112. data/lib/thinking_sphinx/deltas.rb +28 -0
  113. data/lib/thinking_sphinx/deploy/capistrano.rb +100 -0
  114. data/lib/thinking_sphinx/excerpter.rb +22 -0
  115. data/lib/thinking_sphinx/facet.rb +125 -0
  116. data/lib/thinking_sphinx/facet_search.rb +136 -0
  117. data/lib/thinking_sphinx/field.rb +82 -0
  118. data/lib/thinking_sphinx/index/builder.rb +296 -0
  119. data/lib/thinking_sphinx/index/faux_column.rb +110 -0
  120. data/lib/thinking_sphinx/index.rb +157 -0
  121. data/lib/thinking_sphinx/property.rb +162 -0
  122. data/lib/thinking_sphinx/rails_additions.rb +150 -0
  123. data/lib/thinking_sphinx/search.rb +775 -0
  124. data/lib/thinking_sphinx/search_methods.rb +439 -0
  125. data/lib/thinking_sphinx/source/internal_properties.rb +46 -0
  126. data/lib/thinking_sphinx/source/sql.rb +130 -0
  127. data/lib/thinking_sphinx/source.rb +153 -0
  128. data/lib/thinking_sphinx/tasks.rb +121 -0
  129. data/lib/thinking_sphinx/test.rb +52 -0
  130. data/lib/thinking_sphinx.rb +231 -0
  131. data/rails/init.rb +16 -0
  132. data/spec/thinking_sphinx/active_record/delta_spec.rb +128 -0
  133. data/spec/thinking_sphinx/active_record/has_many_association_spec.rb +55 -0
  134. data/spec/thinking_sphinx/active_record/scopes_spec.rb +177 -0
  135. data/spec/thinking_sphinx/active_record_spec.rb +622 -0
  136. data/spec/thinking_sphinx/association_spec.rb +239 -0
  137. data/spec/thinking_sphinx/attribute_spec.rb +570 -0
  138. data/spec/thinking_sphinx/auto_version_spec.rb +39 -0
  139. data/spec/thinking_sphinx/configuration_spec.rb +234 -0
  140. data/spec/thinking_sphinx/context_spec.rb +119 -0
  141. data/spec/thinking_sphinx/core/array_spec.rb +9 -0
  142. data/spec/thinking_sphinx/core/string_spec.rb +9 -0
  143. data/spec/thinking_sphinx/excerpter_spec.rb +57 -0
  144. data/spec/thinking_sphinx/facet_search_spec.rb +176 -0
  145. data/spec/thinking_sphinx/facet_spec.rb +333 -0
  146. data/spec/thinking_sphinx/field_spec.rb +154 -0
  147. data/spec/thinking_sphinx/index/builder_spec.rb +479 -0
  148. data/spec/thinking_sphinx/index/faux_column_spec.rb +30 -0
  149. data/spec/thinking_sphinx/index_spec.rb +183 -0
  150. data/spec/thinking_sphinx/rails_additions_spec.rb +203 -0
  151. data/spec/thinking_sphinx/search_methods_spec.rb +152 -0
  152. data/spec/thinking_sphinx/search_spec.rb +1181 -0
  153. data/spec/thinking_sphinx/source_spec.rb +235 -0
  154. data/spec/thinking_sphinx_spec.rb +204 -0
  155. data/tasks/distribution.rb +41 -0
  156. data/tasks/rails.rake +1 -0
  157. data/tasks/testing.rb +72 -0
  158. metadata +243 -0
@@ -0,0 +1,282 @@
1
+ require 'erb'
2
+ require 'singleton'
3
+
4
+ module ThinkingSphinx
5
+ # This class both keeps track of the configuration settings for Sphinx and
6
+ # also generates the resulting file for Sphinx to use.
7
+ #
8
+ # Here are the default settings, relative to RAILS_ROOT where relevant:
9
+ #
10
+ # config file:: config/#{environment}.sphinx.conf
11
+ # searchd log file:: log/searchd.log
12
+ # query log file:: log/searchd.query.log
13
+ # pid file:: log/searchd.#{environment}.pid
14
+ # searchd files:: db/sphinx/#{environment}/
15
+ # address:: 127.0.0.1
16
+ # port:: 9312
17
+ # allow star:: false
18
+ # min prefix length:: 1
19
+ # min infix length:: 1
20
+ # mem limit:: 64M
21
+ # max matches:: 1000
22
+ # morphology:: nil
23
+ # charset type:: utf-8
24
+ # charset table:: nil
25
+ # ignore chars:: nil
26
+ # html strip:: false
27
+ # html remove elements:: ''
28
+ # searchd_binary_name:: searchd
29
+ # indexer_binary_name:: indexer
30
+ #
31
+ # If you want to change these settings, create a YAML file at
32
+ # config/sphinx.yml with settings for each environment, in a similar
33
+ # fashion to database.yml - using the following keys: config_file,
34
+ # searchd_log_file, query_log_file, pid_file, searchd_file_path, port,
35
+ # allow_star, enable_star, min_prefix_len, min_infix_len, mem_limit,
36
+ # max_matches, morphology, charset_type, charset_table, ignore_chars,
37
+ # html_strip, html_remove_elements, delayed_job_priority,
38
+ # searchd_binary_name, indexer_binary_name.
39
+ #
40
+ # I think you've got the idea.
41
+ #
42
+ # Each setting in the YAML file is optional - so only put in the ones you
43
+ # want to change.
44
+ #
45
+ # Keep in mind, if for some particular reason you're using a version of
46
+ # Sphinx older than 0.9.8 r871 (that's prior to the proper 0.9.8 release),
47
+ # don't set allow_star to true.
48
+ #
49
+ class Configuration
50
+ include Singleton
51
+
52
+ SourceOptions = %w( mysql_connect_flags mysql_ssl_cert mysql_ssl_key
53
+ mysql_ssl_ca sql_range_step sql_query_pre sql_query_post
54
+ sql_query_killlist sql_ranged_throttle sql_query_post_index unpack_zlib
55
+ unpack_mysqlcompress unpack_mysqlcompress_maxsize )
56
+
57
+ IndexOptions = %w( charset_table charset_type charset_dictpath docinfo
58
+ enable_star exceptions html_index_attrs html_remove_elements html_strip
59
+ index_exact_words ignore_chars inplace_docinfo_gap inplace_enable
60
+ inplace_hit_gap inplace_reloc_factor inplace_write_factor min_infix_len
61
+ min_prefix_len min_stemming_len min_word_len mlock morphology ngram_chars
62
+ ngram_len ondisk_dict overshort_step phrase_boundary phrase_boundary_step
63
+ preopen stopwords stopwords_step wordforms )
64
+
65
+ CustomOptions = %w( disable_range )
66
+
67
+ attr_accessor :searchd_file_path, :allow_star, :database_yml_file,
68
+ :app_root, :model_directories, :delayed_job_priority
69
+
70
+ attr_accessor :source_options, :index_options
71
+
72
+ attr_reader :environment, :configuration, :controller
73
+
74
+ # Load in the configuration settings - this will look for config/sphinx.yml
75
+ # and parse it according to the current environment.
76
+ #
77
+ def initialize(app_root = Dir.pwd)
78
+ self.reset
79
+ end
80
+
81
+ def self.configure(&block)
82
+ yield instance
83
+ instance.reset(instance.app_root)
84
+ end
85
+
86
+ def reset(custom_app_root=nil)
87
+ if custom_app_root
88
+ self.app_root = custom_app_root
89
+ else
90
+ self.app_root = RAILS_ROOT if defined?(RAILS_ROOT)
91
+ self.app_root = Merb.root if defined?(Merb)
92
+ self.app_root ||= app_root
93
+ end
94
+
95
+ @configuration = Riddle::Configuration.new
96
+ @configuration.searchd.pid_file = "#{self.app_root}/log/searchd.#{environment}.pid"
97
+ @configuration.searchd.log = "#{self.app_root}/log/searchd.log"
98
+ @configuration.searchd.query_log = "#{self.app_root}/log/searchd.query.log"
99
+
100
+ @controller = Riddle::Controller.new @configuration,
101
+ "#{self.app_root}/config/#{environment}.sphinx.conf"
102
+
103
+ self.address = "127.0.0.1"
104
+ self.port = 9312
105
+ self.database_yml_file = "#{self.app_root}/config/database.yml"
106
+ self.searchd_file_path = "#{self.app_root}/db/sphinx/#{environment}"
107
+ self.allow_star = false
108
+ self.model_directories = ["#{app_root}/app/models/"] +
109
+ Dir.glob("#{app_root}/vendor/plugins/*/app/models/")
110
+ self.delayed_job_priority = 0
111
+
112
+ self.source_options = {}
113
+ self.index_options = {
114
+ :charset_type => "utf-8"
115
+ }
116
+
117
+ parse_config
118
+
119
+ self
120
+ end
121
+
122
+ def self.environment
123
+ Thread.current[:thinking_sphinx_environment] ||= (
124
+ defined?(Merb) ? Merb.environment : ENV['RAILS_ENV']
125
+ ) || "development"
126
+ end
127
+
128
+ def environment
129
+ self.class.environment
130
+ end
131
+
132
+ # Generate the config file for Sphinx by using all the settings defined and
133
+ # looping through all the models with indexes to build the relevant
134
+ # indexer and searchd configuration, and sources and indexes details.
135
+ #
136
+ def build(file_path=nil)
137
+ file_path ||= "#{self.config_file}"
138
+
139
+ @configuration.indexes.clear
140
+
141
+ ThinkingSphinx.context.indexed_models.each do |model|
142
+ model = model.constantize
143
+ model.define_indexes
144
+ @configuration.indexes.concat model.to_riddle
145
+ end
146
+
147
+ open(file_path, "w") do |file|
148
+ file.write @configuration.render
149
+ end
150
+ end
151
+
152
+ def address
153
+ @address
154
+ end
155
+
156
+ def address=(address)
157
+ @address = address
158
+ @configuration.searchd.address = address
159
+ end
160
+
161
+ def port
162
+ @port
163
+ end
164
+
165
+ def port=(port)
166
+ @port = port
167
+ @configuration.searchd.port = port
168
+ end
169
+
170
+ def pid_file
171
+ @configuration.searchd.pid_file
172
+ end
173
+
174
+ def pid_file=(pid_file)
175
+ @configuration.searchd.pid_file = pid_file
176
+ end
177
+
178
+ def searchd_log_file
179
+ @configuration.searchd.log
180
+ end
181
+
182
+ def searchd_log_file=(file)
183
+ @configuration.searchd.log = file
184
+ end
185
+
186
+ def query_log_file
187
+ @configuration.searchd.query_log
188
+ end
189
+
190
+ def query_log_file=(file)
191
+ @configuration.searchd.query_log = file
192
+ end
193
+
194
+ def config_file
195
+ @controller.path
196
+ end
197
+
198
+ def config_file=(file)
199
+ @controller.path = file
200
+ end
201
+
202
+ def bin_path
203
+ @controller.bin_path
204
+ end
205
+
206
+ def bin_path=(path)
207
+ @controller.bin_path = path
208
+ end
209
+
210
+ def searchd_binary_name
211
+ @controller.searchd_binary_name
212
+ end
213
+
214
+ def searchd_binary_name=(name)
215
+ @controller.searchd_binary_name = name
216
+ end
217
+
218
+ def indexer_binary_name
219
+ @controller.indexer_binary_name
220
+ end
221
+
222
+ def indexer_binary_name=(name)
223
+ @controller.indexer_binary_name = name
224
+ end
225
+
226
+ def client
227
+ client = Riddle::Client.new address, port
228
+ client.max_matches = configuration.searchd.max_matches || 1000
229
+ client
230
+ end
231
+
232
+ def models_by_crc
233
+ @models_by_crc ||= begin
234
+ ThinkingSphinx.context.indexed_models.inject({}) do |hash, model|
235
+ hash[model.constantize.to_crc32] = model
236
+ Object.subclasses_of(model.constantize).each { |subclass|
237
+ hash[subclass.to_crc32] = subclass.name
238
+ }
239
+ hash
240
+ end
241
+ end
242
+ end
243
+
244
+ private
245
+
246
+ # Parse the config/sphinx.yml file - if it exists - then use the attribute
247
+ # accessors to set the appropriate values. Nothing too clever.
248
+ #
249
+ def parse_config
250
+ path = "#{app_root}/config/sphinx.yml"
251
+ return unless File.exists?(path)
252
+
253
+ conf = YAML::load(ERB.new(IO.read(path)).result)[environment]
254
+
255
+ conf.each do |key,value|
256
+ self.send("#{key}=", value) if self.respond_to?("#{key}=")
257
+
258
+ set_sphinx_setting self.source_options, key, value, SourceOptions
259
+ set_sphinx_setting self.index_options, key, value, IndexOptions
260
+ set_sphinx_setting self.index_options, key, value, CustomOptions
261
+ set_sphinx_setting @configuration.searchd, key, value
262
+ set_sphinx_setting @configuration.indexer, key, value
263
+ end unless conf.nil?
264
+
265
+ self.bin_path += '/' unless self.bin_path.blank?
266
+
267
+ if self.allow_star
268
+ self.index_options[:enable_star] = true
269
+ self.index_options[:min_prefix_len] = 1
270
+ end
271
+ end
272
+
273
+ def set_sphinx_setting(object, key, value, allowed = {})
274
+ if object.is_a?(Hash)
275
+ object[key.to_sym] = value if allowed.include?(key.to_s)
276
+ else
277
+ object.send("#{key}=", value) if object.respond_to?("#{key}")
278
+ send("#{key}=", value) if self.respond_to?("#{key}")
279
+ end
280
+ end
281
+ end
282
+ end
@@ -0,0 +1,68 @@
1
+ class ThinkingSphinx::Context
2
+ attr_reader :indexed_models
3
+
4
+ def initialize
5
+ @indexed_models = []
6
+ end
7
+
8
+ def prepare
9
+ load_models
10
+ add_indexed_models
11
+ end
12
+
13
+ def define_indexes
14
+ indexed_models.each { |model|
15
+ model.constantize.define_indexes
16
+ }
17
+ end
18
+
19
+ def add_indexed_model(model)
20
+ model = model.name if model.is_a?(Class)
21
+
22
+ indexed_models << model
23
+ indexed_models.uniq!
24
+ indexed_models.sort!
25
+ end
26
+
27
+ def superclass_indexed_models
28
+ klasses = indexed_models.collect { |name| name.constantize }
29
+ klasses.reject { |klass|
30
+ klass.superclass.ancestors.any? { |ancestor| klasses.include?(ancestor) }
31
+ }.collect { |klass| klass.name }
32
+ end
33
+
34
+ private
35
+
36
+ def add_indexed_models
37
+ Object.subclasses_of(ActiveRecord::Base).each do |klass|
38
+ add_indexed_model klass if klass.has_sphinx_indexes?
39
+ end
40
+ end
41
+
42
+ # Make sure all models are loaded - without reloading any that
43
+ # ActiveRecord::Base is already aware of (otherwise we start to hit some
44
+ # messy dependencies issues).
45
+ #
46
+ def load_models
47
+ ThinkingSphinx::Configuration.instance.model_directories.each do |base|
48
+ Dir["#{base}**/*.rb"].each do |file|
49
+ model_name = file.gsub(/^#{base}([\w_\/\\]+)\.rb/, '\1')
50
+
51
+ next if model_name.nil?
52
+ next if ::ActiveRecord::Base.send(:subclasses).detect { |model|
53
+ model.name == model_name
54
+ }
55
+
56
+ begin
57
+ model_name.camelize.constantize
58
+ rescue LoadError
59
+ model_name.gsub!(/.*[\/\\]/, '').nil? ? next : retry
60
+ rescue NameError
61
+ next
62
+ rescue StandardError
63
+ STDERR.puts "Warning: Error loading #{file}"
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,7 @@
1
+ module SearchAsArray
2
+ def ===(object)
3
+ object.is_a?(ThinkingSphinx::Search) || super
4
+ end
5
+ end
6
+
7
+ Array.extend SearchAsArray
@@ -0,0 +1,15 @@
1
+ require 'zlib'
2
+
3
+ module ThinkingSphinx
4
+ module Core
5
+ module String
6
+ def to_crc32
7
+ Zlib.crc32 self
8
+ end
9
+ end
10
+ end
11
+ end
12
+
13
+ class String
14
+ include ThinkingSphinx::Core::String
15
+ end
@@ -0,0 +1,62 @@
1
+ module ThinkingSphinx
2
+ module Deltas
3
+ class DefaultDelta
4
+ attr_accessor :column
5
+
6
+ def initialize(index, options)
7
+ @index = index
8
+ @column = options.delete(:delta_column) || :delta
9
+ end
10
+
11
+ def index(model, instance = nil)
12
+ return true unless ThinkingSphinx.updates_enabled? &&
13
+ ThinkingSphinx.deltas_enabled?
14
+ return true if instance && !toggled(instance)
15
+
16
+ update_delta_indexes model
17
+ delete_from_core model, instance if instance
18
+
19
+ true
20
+ end
21
+
22
+ def toggle(instance)
23
+ instance.delta = true
24
+ end
25
+
26
+ def toggled(instance)
27
+ instance.delta
28
+ end
29
+
30
+ def reset_query(model)
31
+ "UPDATE #{model.quoted_table_name} SET " +
32
+ "#{model.connection.quote_column_name(@column.to_s)} = #{adapter.boolean(false)} " +
33
+ "WHERE #{model.connection.quote_column_name(@column.to_s)} = #{adapter.boolean(true)}"
34
+ end
35
+
36
+ def clause(model, toggled)
37
+ "#{model.quoted_table_name}.#{model.connection.quote_column_name(@column.to_s)}" +
38
+ " = #{adapter.boolean(toggled)}"
39
+ end
40
+
41
+ private
42
+
43
+ def update_delta_indexes(model)
44
+ config = ThinkingSphinx::Configuration.instance
45
+ rotate = ThinkingSphinx.sphinx_running? ? "--rotate" : ""
46
+
47
+ output = `#{config.bin_path}#{config.indexer_binary_name} --config #{config.config_file} #{rotate} #{model.delta_index_names.join(' ')}`
48
+ puts(output) unless ThinkingSphinx.suppress_delta_output?
49
+ end
50
+
51
+ def delete_from_core(model, instance)
52
+ model.core_index_names.each do |index_name|
53
+ model.delete_in_index index_name, instance.sphinx_document_id
54
+ end
55
+ end
56
+
57
+ def adapter
58
+ @adapter = @index.model.sphinx_database_adapter
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,28 @@
1
+ require 'thinking_sphinx/deltas/default_delta'
2
+
3
+ module ThinkingSphinx
4
+ module Deltas
5
+ def self.parse(index)
6
+ delta_option = index.local_options.delete(:delta)
7
+ case delta_option
8
+ when TrueClass, :default
9
+ DefaultDelta.new index, index.local_options
10
+ when :delayed
11
+ DelayedDelta.new index, index.local_options
12
+ when :datetime
13
+ DatetimeDelta.new index, index.local_options
14
+ when FalseClass, nil
15
+ nil
16
+ else
17
+ if delta_option.is_a?(String)
18
+ delta_option = Kernel.const_get(delta_option)
19
+ end
20
+ if delta_option.ancestors.include?(ThinkingSphinx::Deltas::DefaultDelta)
21
+ delta_option.new index, index.local_options
22
+ else
23
+ raise "Unknown delta type"
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,100 @@
1
+ Capistrano::Configuration.instance(:must_exist).load do
2
+ namespace :thinking_sphinx do
3
+ namespace :install do
4
+ desc <<-DESC
5
+ Install Sphinx by source
6
+
7
+ If Postgres is available, Sphinx will use it.
8
+
9
+ If the variable :thinking_sphinx_configure_args is set, it will
10
+ be passed to the Sphinx configure script. You can use this to
11
+ install Sphinx in a non-standard location:
12
+
13
+ set :thinking_sphinx_configure_args, "--prefix=$HOME/software"
14
+ DESC
15
+
16
+ task :sphinx do
17
+ with_postgres = false
18
+ begin
19
+ run "which pg_config" do |channel, stream, data|
20
+ with_postgres = !(data.nil? || data == "")
21
+ end
22
+ rescue Capistrano::CommandError => e
23
+ puts "Continuing despite error: #{e.message}"
24
+ end
25
+
26
+ args = []
27
+ if with_postgres
28
+ run "pg_config --pkgincludedir" do |channel, stream, data|
29
+ args << "--with-pgsql=#{data}"
30
+ end
31
+ end
32
+ args << fetch(:thinking_sphinx_configure_args, '')
33
+
34
+ commands = <<-CMD
35
+ wget -q http://www.sphinxsearch.com/downloads/sphinx-0.9.8.1.tar.gz >> sphinx.log
36
+ tar xzvf sphinx-0.9.8.1.tar.gz
37
+ cd sphinx-0.9.8.1
38
+ ./configure #{args.join(" ")}
39
+ make
40
+ #{try_sudo} make install
41
+ rm -rf sphinx-0.9.8.1 sphinx-0.9.8.1.tar.gz
42
+ CMD
43
+ run commands.split(/\n\s+/).join(" && ")
44
+ end
45
+
46
+ desc "Install Thinking Sphinx as a gem from GitHub"
47
+ task :ts do
48
+ run "#{try_sudo} gem install thinking-sphinx --source http://gemcutter.org"
49
+ end
50
+ end
51
+
52
+ desc "Generate the Sphinx configuration file"
53
+ task :configure do
54
+ rake "thinking_sphinx:configure"
55
+ end
56
+
57
+ desc "Index data"
58
+ task :index do
59
+ rake "thinking_sphinx:index"
60
+ end
61
+
62
+ desc "Start the Sphinx daemon"
63
+ task :start do
64
+ configure
65
+ rake "thinking_sphinx:start"
66
+ end
67
+
68
+ desc "Stop the Sphinx daemon"
69
+ task :stop do
70
+ configure
71
+ rake "thinking_sphinx:stop"
72
+ end
73
+
74
+ desc "Stop and then start the Sphinx daemon"
75
+ task :restart do
76
+ stop
77
+ start
78
+ end
79
+
80
+ desc "Stop, re-index and then start the Sphinx daemon"
81
+ task :rebuild do
82
+ stop
83
+ index
84
+ start
85
+ end
86
+
87
+ desc "Add the shared folder for sphinx files for the production environment"
88
+ task :shared_sphinx_folder, :roles => :web do
89
+ run "mkdir -p #{shared_path}/db/sphinx/production"
90
+ end
91
+
92
+ def rake(*tasks)
93
+ rails_env = fetch(:rails_env, "production")
94
+ rake = fetch(:rake, "rake")
95
+ tasks.each do |t|
96
+ run "if [ -d #{release_path} ]; then cd #{release_path}; else cd #{current_path}; fi; #{rake} RAILS_ENV=#{rails_env} #{t}"
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,22 @@
1
+ module ThinkingSphinx
2
+ class Excerpter
3
+ CoreMethods = %w( kind_of? object_id respond_to? should should_not stub! )
4
+ # Hide most methods, to allow them to be passed through to the instance.
5
+ instance_methods.select { |method|
6
+ method.to_s[/^__/].nil? && !CoreMethods.include?(method.to_s)
7
+ }.each { |method|
8
+ undef_method method
9
+ }
10
+
11
+ def initialize(search, instance)
12
+ @search = search
13
+ @instance = instance
14
+ end
15
+
16
+ def method_missing(method, *args, &block)
17
+ string = CGI::escapeHTML @instance.send(method, *args, &block).to_s
18
+
19
+ @search.excerpt_for(string, @instance.class)
20
+ end
21
+ end
22
+ end