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,76 @@
|
|
1
|
+
class ThinkingSphinx::Context
|
2
|
+
attr_reader :indexed_models
|
3
|
+
|
4
|
+
def initialize(*models)
|
5
|
+
@indexed_models = []
|
6
|
+
end
|
7
|
+
|
8
|
+
def prepare
|
9
|
+
ThinkingSphinx::Configuration.instance.indexed_models.each do |model|
|
10
|
+
add_indexed_model model
|
11
|
+
end
|
12
|
+
|
13
|
+
return unless indexed_models.empty?
|
14
|
+
|
15
|
+
load_models
|
16
|
+
add_indexed_models
|
17
|
+
end
|
18
|
+
|
19
|
+
def define_indexes
|
20
|
+
indexed_models.each { |model|
|
21
|
+
model.constantize.define_indexes
|
22
|
+
}
|
23
|
+
end
|
24
|
+
|
25
|
+
def add_indexed_model(model)
|
26
|
+
model = model.name if model.is_a?(Class)
|
27
|
+
|
28
|
+
indexed_models << model
|
29
|
+
indexed_models.uniq!
|
30
|
+
indexed_models.sort!
|
31
|
+
end
|
32
|
+
|
33
|
+
def superclass_indexed_models
|
34
|
+
klasses = indexed_models.collect { |name| name.constantize }
|
35
|
+
klasses.reject { |klass|
|
36
|
+
klass.superclass.ancestors.any? { |ancestor| klasses.include?(ancestor) }
|
37
|
+
}.collect { |klass| klass.name }
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def add_indexed_models
|
43
|
+
ActiveRecord::Base.descendants.each do |klass|
|
44
|
+
add_indexed_model klass if klass.has_sphinx_indexes?
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Make sure all models are loaded - without reloading any that
|
49
|
+
# ActiveRecord::Base is already aware of (otherwise we start to hit some
|
50
|
+
# messy dependencies issues).
|
51
|
+
#
|
52
|
+
def load_models
|
53
|
+
ThinkingSphinx::Configuration.instance.model_directories.each do |base|
|
54
|
+
Dir["#{base}**/*.rb"].each do |file|
|
55
|
+
model_name = file.gsub(/^#{base}([\w_\/\\]+)\.rb/, '\1')
|
56
|
+
|
57
|
+
next if model_name.nil?
|
58
|
+
next if ::ActiveRecord::Base.send(:descendants).detect { |model|
|
59
|
+
model.name == model_name.camelize
|
60
|
+
}
|
61
|
+
|
62
|
+
begin
|
63
|
+
model_name.camelize.constantize
|
64
|
+
rescue LoadError
|
65
|
+
model_name.gsub!(/.*[\/\\]/, '').nil? ? next : retry
|
66
|
+
rescue NameError
|
67
|
+
next
|
68
|
+
rescue StandardError => err
|
69
|
+
STDERR.puts "Warning: Error loading #{file}:"
|
70
|
+
STDERR.puts err.message
|
71
|
+
STDERR.puts err.backtrace.join("\n"), ''
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
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,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.send "#{@column}=", true
|
24
|
+
end
|
25
|
+
|
26
|
+
def toggled(instance)
|
27
|
+
instance.send "#{@column}"
|
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,101 @@
|
|
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://sphinxsearch.com/downloads/sphinx-0.9.9.tar.gz >> sphinx.log
|
36
|
+
tar xzvf sphinx-0.9.9.tar.gz
|
37
|
+
cd sphinx-0.9.9
|
38
|
+
./configure #{args.join(" ")}
|
39
|
+
make
|
40
|
+
#{try_sudo} make install
|
41
|
+
rm -rf sphinx-0.9.9 sphinx-0.9.9.tar.gz
|
42
|
+
CMD
|
43
|
+
run commands.split(/\n\s+/).join(" && ")
|
44
|
+
end
|
45
|
+
|
46
|
+
desc "Install Thinking Sphinx as a gem"
|
47
|
+
task :ts do
|
48
|
+
run "#{try_sudo} gem install thinking-sphinx"
|
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"
|
88
|
+
task :shared_sphinx_folder, :roles => :web do
|
89
|
+
rails_env = fetch(:rails_env, "production")
|
90
|
+
run "mkdir -p #{shared_path}/sphinx/#{rails_env}"
|
91
|
+
end
|
92
|
+
|
93
|
+
def rake(*tasks)
|
94
|
+
rails_env = fetch(:rails_env, "production")
|
95
|
+
rake = fetch(:rake, "rake")
|
96
|
+
tasks.each do |t|
|
97
|
+
run "if [ -d #{release_path} ]; then cd #{release_path}; else cd #{current_path}; fi; #{rake} RAILS_ENV=#{rails_env} #{t}"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module ThinkingSphinx
|
2
|
+
class Excerpter
|
3
|
+
CoreMethods = %w( kind_of? object_id respond_to? respond_to_missing? should
|
4
|
+
should_not stub! )
|
5
|
+
# Hide most methods, to allow them to be passed through to the instance.
|
6
|
+
instance_methods.select { |method|
|
7
|
+
method.to_s[/^__/].nil? && !CoreMethods.include?(method.to_s)
|
8
|
+
}.each { |method|
|
9
|
+
undef_method method
|
10
|
+
}
|
11
|
+
|
12
|
+
def initialize(search, instance)
|
13
|
+
@search = search
|
14
|
+
@instance = instance
|
15
|
+
end
|
16
|
+
|
17
|
+
def method_missing(method, *args, &block)
|
18
|
+
string = @instance.send(method, *args, &block).to_s
|
19
|
+
|
20
|
+
@search.excerpt_for(string, @instance.class)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,127 @@
|
|
1
|
+
module ThinkingSphinx
|
2
|
+
class Facet
|
3
|
+
attr_reader :property, :value_source
|
4
|
+
|
5
|
+
def initialize(property, value_source = nil)
|
6
|
+
@property = property
|
7
|
+
@value_source = value_source
|
8
|
+
|
9
|
+
if property.columns.length != 1
|
10
|
+
raise "Can't translate Facets on multiple-column field or attribute"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.name_for(facet)
|
15
|
+
case facet
|
16
|
+
when Facet
|
17
|
+
facet.name
|
18
|
+
when String, Symbol
|
19
|
+
facet.to_s.gsub(/(_facet|_crc)$/,'').to_sym
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.attribute_name_for(name)
|
24
|
+
name.to_s == 'class' ? 'class_crc' : "#{name}_facet"
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.attribute_name_from_value(name, value)
|
28
|
+
case value
|
29
|
+
when String
|
30
|
+
attribute_name_for(name)
|
31
|
+
when Array
|
32
|
+
if value.all? { |val| val.is_a?(Integer) }
|
33
|
+
name
|
34
|
+
else
|
35
|
+
attribute_name_for(name)
|
36
|
+
end
|
37
|
+
else
|
38
|
+
name
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.translate?(property)
|
43
|
+
return true if property.is_a?(Field)
|
44
|
+
|
45
|
+
case property.type
|
46
|
+
when :string
|
47
|
+
true
|
48
|
+
when :integer, :boolean, :datetime, :float
|
49
|
+
false
|
50
|
+
when :multi
|
51
|
+
!property.all_ints?
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def name
|
56
|
+
property.unique_name
|
57
|
+
end
|
58
|
+
|
59
|
+
def attribute_name
|
60
|
+
if translate?
|
61
|
+
Facet.attribute_name_for(@property.unique_name)
|
62
|
+
else
|
63
|
+
@property.unique_name.to_s
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def translate?
|
68
|
+
Facet.translate?(@property)
|
69
|
+
end
|
70
|
+
|
71
|
+
def type
|
72
|
+
@property.is_a?(Field) ? :string : @property.type
|
73
|
+
end
|
74
|
+
|
75
|
+
def float?
|
76
|
+
@property.type == :float
|
77
|
+
end
|
78
|
+
|
79
|
+
def value(object, attribute_hash)
|
80
|
+
attribute_value = attribute_hash['@groupby']
|
81
|
+
return translate(object, attribute_value) if translate? || float?
|
82
|
+
|
83
|
+
case @property.type
|
84
|
+
when :datetime
|
85
|
+
Time.at(attribute_value)
|
86
|
+
when :boolean
|
87
|
+
attribute_value > 0
|
88
|
+
else
|
89
|
+
attribute_value
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def to_s
|
94
|
+
name
|
95
|
+
end
|
96
|
+
|
97
|
+
private
|
98
|
+
|
99
|
+
def translate(object, attribute_value)
|
100
|
+
objects = source_objects(object)
|
101
|
+
return if objects.blank?
|
102
|
+
|
103
|
+
method = value_source || column.__name
|
104
|
+
object = objects.one? ? objects.first : objects.detect { |item|
|
105
|
+
result = item.send(method)
|
106
|
+
result && result.to_crc32 == attribute_value
|
107
|
+
}
|
108
|
+
|
109
|
+
object.try(method)
|
110
|
+
end
|
111
|
+
|
112
|
+
def source_objects(object)
|
113
|
+
column.__stack.each { |method|
|
114
|
+
object = Array(object).collect { |item|
|
115
|
+
item.send(method)
|
116
|
+
}.flatten.compact
|
117
|
+
|
118
|
+
return nil if object.empty?
|
119
|
+
}
|
120
|
+
Array(object)
|
121
|
+
end
|
122
|
+
|
123
|
+
def column
|
124
|
+
@property.columns.first
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
@@ -0,0 +1,166 @@
|
|
1
|
+
module ThinkingSphinx
|
2
|
+
class FacetSearch < Hash
|
3
|
+
attr_accessor :args, :options
|
4
|
+
|
5
|
+
def initialize(*args)
|
6
|
+
ThinkingSphinx.context.define_indexes
|
7
|
+
|
8
|
+
@options = args.extract_options!
|
9
|
+
@args = args
|
10
|
+
|
11
|
+
set_default_options
|
12
|
+
|
13
|
+
populate
|
14
|
+
end
|
15
|
+
|
16
|
+
def for(hash = {})
|
17
|
+
for_options = {:with => {}}.merge(options)
|
18
|
+
|
19
|
+
hash.each do |key, value|
|
20
|
+
attrib = ThinkingSphinx::Facet.attribute_name_from_value(key, value)
|
21
|
+
for_options[:with][attrib] = underlying_value key, value
|
22
|
+
end
|
23
|
+
|
24
|
+
ThinkingSphinx.search *(args + [for_options])
|
25
|
+
end
|
26
|
+
|
27
|
+
def facet_names
|
28
|
+
@facet_names ||= begin
|
29
|
+
names = options[:all_facets] ?
|
30
|
+
facet_names_for_all_classes : facet_names_common_to_all_classes
|
31
|
+
|
32
|
+
names.delete "class_crc" unless options[:class_facet]
|
33
|
+
names
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def set_default_options
|
40
|
+
options[:all_facets] ||= false
|
41
|
+
if options[:class_facet].nil?
|
42
|
+
options[:class_facet] = ((options[:classes] || []).length != 1)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def populate
|
47
|
+
return if facet_names.empty?
|
48
|
+
|
49
|
+
ThinkingSphinx::Search.bundle_searches(facet_names) { |sphinx, name|
|
50
|
+
sphinx.search *(args + [facet_search_options(name)])
|
51
|
+
}.each_with_index { |search, index|
|
52
|
+
add_from_results facet_names[index], search
|
53
|
+
}
|
54
|
+
end
|
55
|
+
|
56
|
+
def facet_search_options(facet_name)
|
57
|
+
options.merge(
|
58
|
+
:group_function => :attr,
|
59
|
+
:limit => max_matches,
|
60
|
+
:max_matches => max_matches,
|
61
|
+
:page => 1,
|
62
|
+
:group_by => facet_name,
|
63
|
+
:ids_only => !translate?(facet_name)
|
64
|
+
)
|
65
|
+
end
|
66
|
+
|
67
|
+
def facet_classes
|
68
|
+
(
|
69
|
+
options[:classes] || ThinkingSphinx.context.indexed_models.collect { |model|
|
70
|
+
model.constantize
|
71
|
+
}
|
72
|
+
).select { |klass| klass.sphinx_facets.any? }
|
73
|
+
end
|
74
|
+
|
75
|
+
def all_facets
|
76
|
+
facet_classes.collect { |klass|
|
77
|
+
klass.sphinx_facets
|
78
|
+
}.flatten.select { |facet|
|
79
|
+
options[:facets].blank? || Array(options[:facets]).include?(facet.name)
|
80
|
+
}
|
81
|
+
end
|
82
|
+
|
83
|
+
def facet_names_for_all_classes
|
84
|
+
all_facets.group_by { |facet|
|
85
|
+
facet.name
|
86
|
+
}.collect { |name, facets|
|
87
|
+
if facets.collect { |facet| facet.type }.uniq.length > 1
|
88
|
+
raise "Facet #{name} exists in more than one model with different types"
|
89
|
+
end
|
90
|
+
facets.first.attribute_name
|
91
|
+
}
|
92
|
+
end
|
93
|
+
|
94
|
+
def facet_names_common_to_all_classes
|
95
|
+
facet_names_for_all_classes.select { |name|
|
96
|
+
facet_classes.all? { |klass|
|
97
|
+
klass.sphinx_facets.detect { |facet|
|
98
|
+
facet.attribute_name == name
|
99
|
+
}
|
100
|
+
}
|
101
|
+
}
|
102
|
+
end
|
103
|
+
|
104
|
+
def translate?(name)
|
105
|
+
facet = facet_from_name(name)
|
106
|
+
facet.translate? || facet.float?
|
107
|
+
end
|
108
|
+
|
109
|
+
def config
|
110
|
+
ThinkingSphinx::Configuration.instance
|
111
|
+
end
|
112
|
+
|
113
|
+
def max_matches
|
114
|
+
@max_matches ||= config.configuration.searchd.max_matches || 1000
|
115
|
+
end
|
116
|
+
|
117
|
+
# example: facet = country_facet; name = :country
|
118
|
+
def add_from_results(facet, search)
|
119
|
+
name = ThinkingSphinx::Facet.name_for(facet)
|
120
|
+
facet = facet_from_name(facet)
|
121
|
+
|
122
|
+
self[name] ||= {}
|
123
|
+
|
124
|
+
return if search.empty?
|
125
|
+
|
126
|
+
search.each_with_match do |result, match|
|
127
|
+
facet_value = facet.value(result, match[:attributes])
|
128
|
+
|
129
|
+
self[name][facet_value] ||= 0
|
130
|
+
self[name][facet_value] += match[:attributes]["@count"]
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
def underlying_value(key, value)
|
135
|
+
case value
|
136
|
+
when Array
|
137
|
+
value.collect { |item| underlying_value(key, item) }
|
138
|
+
when String
|
139
|
+
value.to_crc32
|
140
|
+
else
|
141
|
+
value
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
def facet_from_object(object, name)
|
146
|
+
facet = nil
|
147
|
+
klass = object.class
|
148
|
+
|
149
|
+
while klass != ::ActiveRecord::Base && facet.nil?
|
150
|
+
facet = klass.sphinx_facets.detect { |facet|
|
151
|
+
facet.attribute_name == name
|
152
|
+
}
|
153
|
+
klass = klass.superclass
|
154
|
+
end
|
155
|
+
|
156
|
+
facet
|
157
|
+
end
|
158
|
+
|
159
|
+
def facet_from_name(name)
|
160
|
+
name = ThinkingSphinx::Facet.name_for(name)
|
161
|
+
all_facets.detect { |facet|
|
162
|
+
facet.name == name
|
163
|
+
}
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|