freelancing-god-thinking-sphinx 1.1.24 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +1 -0
- data/lib/thinking_sphinx.rb +8 -5
- data/lib/thinking_sphinx/active_record.rb +5 -4
- data/lib/thinking_sphinx/active_record/scopes.rb +37 -0
- data/lib/thinking_sphinx/configuration.rb +6 -0
- data/lib/thinking_sphinx/excerpter.rb +22 -0
- data/lib/thinking_sphinx/facet_search.rb +134 -0
- data/lib/thinking_sphinx/search.rb +590 -673
- data/lib/thinking_sphinx/search_methods.rb +421 -0
- data/lib/thinking_sphinx/tasks.rb +3 -3
- data/spec/{unit → lib}/thinking_sphinx/active_record/delta_spec.rb +0 -0
- data/spec/{unit → lib}/thinking_sphinx/active_record/has_many_association_spec.rb +0 -0
- data/spec/lib/thinking_sphinx/active_record/scopes_spec.rb +92 -0
- data/spec/{unit → lib}/thinking_sphinx/active_record_spec.rb +0 -0
- data/spec/{unit → lib}/thinking_sphinx/association_spec.rb +0 -0
- data/spec/{unit → lib}/thinking_sphinx/attribute_spec.rb +0 -0
- data/spec/{unit → lib}/thinking_sphinx/configuration_spec.rb +25 -0
- data/spec/{unit → lib}/thinking_sphinx/core/string_spec.rb +0 -0
- data/spec/lib/thinking_sphinx/excerpter_spec.rb +49 -0
- data/spec/lib/thinking_sphinx/facet_search_spec.rb +176 -0
- data/spec/{unit → lib}/thinking_sphinx/facet_spec.rb +0 -0
- data/spec/{unit → lib}/thinking_sphinx/field_spec.rb +0 -0
- data/spec/{unit → lib}/thinking_sphinx/index/builder_spec.rb +0 -0
- data/spec/{unit → lib}/thinking_sphinx/index/faux_column_spec.rb +0 -0
- data/spec/{unit → lib}/thinking_sphinx/index_spec.rb +0 -0
- data/spec/{unit → lib}/thinking_sphinx/rails_additions_spec.rb +0 -0
- data/spec/lib/thinking_sphinx/search_methods_spec.rb +152 -0
- data/spec/lib/thinking_sphinx/search_spec.rb +879 -0
- data/spec/{unit → lib}/thinking_sphinx/source_spec.rb +0 -0
- data/spec/{unit → lib}/thinking_sphinx_spec.rb +0 -0
- data/vendor/riddle/lib/riddle/client.rb +3 -0
- data/vendor/riddle/lib/riddle/configuration/section.rb +1 -1
- data/vendor/riddle/lib/riddle/controller.rb +1 -1
- metadata +46 -44
- data/lib/thinking_sphinx/active_record/search.rb +0 -57
- data/lib/thinking_sphinx/collection.rb +0 -148
- data/lib/thinking_sphinx/facet_collection.rb +0 -59
- data/lib/thinking_sphinx/search/facets.rb +0 -104
- data/spec/unit/thinking_sphinx/active_record/search_spec.rb +0 -107
- data/spec/unit/thinking_sphinx/collection_spec.rb +0 -15
- data/spec/unit/thinking_sphinx/facet_collection_spec.rb +0 -64
- data/spec/unit/thinking_sphinx/search_spec.rb +0 -228
File without changes
|
File without changes
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: freelancing-god-thinking-sphinx
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pat Allan
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-07-
|
12
|
+
date: 2009-07-25 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -26,7 +26,7 @@ files:
|
|
26
26
|
- lib/thinking_sphinx/active_record/attribute_updates.rb
|
27
27
|
- lib/thinking_sphinx/active_record/delta.rb
|
28
28
|
- lib/thinking_sphinx/active_record/has_many_association.rb
|
29
|
-
- lib/thinking_sphinx/active_record/
|
29
|
+
- lib/thinking_sphinx/active_record/scopes.rb
|
30
30
|
- lib/thinking_sphinx/active_record.rb
|
31
31
|
- lib/thinking_sphinx/adapters/abstract_adapter.rb
|
32
32
|
- lib/thinking_sphinx/adapters/mysql_adapter.rb
|
@@ -34,7 +34,6 @@ files:
|
|
34
34
|
- lib/thinking_sphinx/association.rb
|
35
35
|
- lib/thinking_sphinx/attribute.rb
|
36
36
|
- lib/thinking_sphinx/class_facet.rb
|
37
|
-
- lib/thinking_sphinx/collection.rb
|
38
37
|
- lib/thinking_sphinx/configuration.rb
|
39
38
|
- lib/thinking_sphinx/core/string.rb
|
40
39
|
- lib/thinking_sphinx/deltas/datetime_delta.rb
|
@@ -45,16 +44,17 @@ files:
|
|
45
44
|
- lib/thinking_sphinx/deltas/delayed_delta.rb
|
46
45
|
- lib/thinking_sphinx/deltas.rb
|
47
46
|
- lib/thinking_sphinx/deploy/capistrano.rb
|
47
|
+
- lib/thinking_sphinx/excerpter.rb
|
48
48
|
- lib/thinking_sphinx/facet.rb
|
49
|
-
- lib/thinking_sphinx/
|
49
|
+
- lib/thinking_sphinx/facet_search.rb
|
50
50
|
- lib/thinking_sphinx/field.rb
|
51
51
|
- lib/thinking_sphinx/index/builder.rb
|
52
52
|
- lib/thinking_sphinx/index/faux_column.rb
|
53
53
|
- lib/thinking_sphinx/index.rb
|
54
54
|
- lib/thinking_sphinx/property.rb
|
55
55
|
- lib/thinking_sphinx/rails_additions.rb
|
56
|
-
- lib/thinking_sphinx/search/facets.rb
|
57
56
|
- lib/thinking_sphinx/search.rb
|
57
|
+
- lib/thinking_sphinx/search_methods.rb
|
58
58
|
- lib/thinking_sphinx/source/internal_properties.rb
|
59
59
|
- lib/thinking_sphinx/source/sql.rb
|
60
60
|
- lib/thinking_sphinx/source.rb
|
@@ -105,25 +105,26 @@ files:
|
|
105
105
|
- vendor/riddle/lib/riddle/configuration.rb
|
106
106
|
- vendor/riddle/lib/riddle/controller.rb
|
107
107
|
- vendor/riddle/lib/riddle.rb
|
108
|
-
- spec/
|
109
|
-
- spec/
|
110
|
-
- spec/
|
111
|
-
- spec/
|
112
|
-
- spec/
|
113
|
-
- spec/
|
114
|
-
- spec/
|
115
|
-
- spec/
|
116
|
-
- spec/
|
117
|
-
- spec/
|
118
|
-
- spec/
|
119
|
-
- spec/
|
120
|
-
- spec/
|
121
|
-
- spec/
|
122
|
-
- spec/
|
123
|
-
- spec/
|
124
|
-
- spec/
|
125
|
-
- spec/
|
126
|
-
- spec/
|
108
|
+
- spec/lib/thinking_sphinx/active_record/delta_spec.rb
|
109
|
+
- spec/lib/thinking_sphinx/active_record/has_many_association_spec.rb
|
110
|
+
- spec/lib/thinking_sphinx/active_record/scopes_spec.rb
|
111
|
+
- spec/lib/thinking_sphinx/active_record_spec.rb
|
112
|
+
- spec/lib/thinking_sphinx/association_spec.rb
|
113
|
+
- spec/lib/thinking_sphinx/attribute_spec.rb
|
114
|
+
- spec/lib/thinking_sphinx/configuration_spec.rb
|
115
|
+
- spec/lib/thinking_sphinx/core/string_spec.rb
|
116
|
+
- spec/lib/thinking_sphinx/excerpter_spec.rb
|
117
|
+
- spec/lib/thinking_sphinx/facet_search_spec.rb
|
118
|
+
- spec/lib/thinking_sphinx/facet_spec.rb
|
119
|
+
- spec/lib/thinking_sphinx/field_spec.rb
|
120
|
+
- spec/lib/thinking_sphinx/index/builder_spec.rb
|
121
|
+
- spec/lib/thinking_sphinx/index/faux_column_spec.rb
|
122
|
+
- spec/lib/thinking_sphinx/index_spec.rb
|
123
|
+
- spec/lib/thinking_sphinx/rails_additions_spec.rb
|
124
|
+
- spec/lib/thinking_sphinx/search_methods_spec.rb
|
125
|
+
- spec/lib/thinking_sphinx/search_spec.rb
|
126
|
+
- spec/lib/thinking_sphinx/source_spec.rb
|
127
|
+
- spec/lib/thinking_sphinx_spec.rb
|
127
128
|
has_rdoc: true
|
128
129
|
homepage: http://ts.freelancing-gods.com
|
129
130
|
post_install_message: |+
|
@@ -169,22 +170,23 @@ signing_key:
|
|
169
170
|
specification_version: 2
|
170
171
|
summary: A concise and easy-to-use Ruby library that connects ActiveRecord to the Sphinx search daemon, managing configuration, indexing and searching.
|
171
172
|
test_files:
|
172
|
-
- spec/
|
173
|
-
- spec/
|
174
|
-
- spec/
|
175
|
-
- spec/
|
176
|
-
- spec/
|
177
|
-
- spec/
|
178
|
-
- spec/
|
179
|
-
- spec/
|
180
|
-
- spec/
|
181
|
-
- spec/
|
182
|
-
- spec/
|
183
|
-
- spec/
|
184
|
-
- spec/
|
185
|
-
- spec/
|
186
|
-
- spec/
|
187
|
-
- spec/
|
188
|
-
- spec/
|
189
|
-
- spec/
|
190
|
-
- spec/
|
173
|
+
- spec/lib/thinking_sphinx/active_record/delta_spec.rb
|
174
|
+
- spec/lib/thinking_sphinx/active_record/has_many_association_spec.rb
|
175
|
+
- spec/lib/thinking_sphinx/active_record/scopes_spec.rb
|
176
|
+
- spec/lib/thinking_sphinx/active_record_spec.rb
|
177
|
+
- spec/lib/thinking_sphinx/association_spec.rb
|
178
|
+
- spec/lib/thinking_sphinx/attribute_spec.rb
|
179
|
+
- spec/lib/thinking_sphinx/configuration_spec.rb
|
180
|
+
- spec/lib/thinking_sphinx/core/string_spec.rb
|
181
|
+
- spec/lib/thinking_sphinx/excerpter_spec.rb
|
182
|
+
- spec/lib/thinking_sphinx/facet_search_spec.rb
|
183
|
+
- spec/lib/thinking_sphinx/facet_spec.rb
|
184
|
+
- spec/lib/thinking_sphinx/field_spec.rb
|
185
|
+
- spec/lib/thinking_sphinx/index/builder_spec.rb
|
186
|
+
- spec/lib/thinking_sphinx/index/faux_column_spec.rb
|
187
|
+
- spec/lib/thinking_sphinx/index_spec.rb
|
188
|
+
- spec/lib/thinking_sphinx/rails_additions_spec.rb
|
189
|
+
- spec/lib/thinking_sphinx/search_methods_spec.rb
|
190
|
+
- spec/lib/thinking_sphinx/search_spec.rb
|
191
|
+
- spec/lib/thinking_sphinx/source_spec.rb
|
192
|
+
- spec/lib/thinking_sphinx_spec.rb
|
@@ -1,57 +0,0 @@
|
|
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
|
-
|
32
|
-
def search_count(*args)
|
33
|
-
options = args.extract_options!
|
34
|
-
options[:class] = self
|
35
|
-
args << options
|
36
|
-
ThinkingSphinx::Search.count(*args)
|
37
|
-
end
|
38
|
-
|
39
|
-
def search_for_id(*args)
|
40
|
-
options = args.extract_options!
|
41
|
-
options[:class] = self
|
42
|
-
args << options
|
43
|
-
ThinkingSphinx::Search.search_for_id(*args)
|
44
|
-
end
|
45
|
-
|
46
|
-
def facets(*args)
|
47
|
-
options = args.extract_options!
|
48
|
-
options[:class] = self
|
49
|
-
args << options
|
50
|
-
ThinkingSphinx::Search.facets(*args)
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
@@ -1,148 +0,0 @@
|
|
1
|
-
module ThinkingSphinx
|
2
|
-
class Collection < ::Array
|
3
|
-
attr_reader :total_entries, :total_pages, :current_page, :per_page
|
4
|
-
attr_accessor :results
|
5
|
-
|
6
|
-
# Compatibility with older versions of will_paginate
|
7
|
-
alias_method :page_count, :total_pages
|
8
|
-
|
9
|
-
def initialize(page, per_page, entries, total_entries)
|
10
|
-
@current_page, @per_page, @total_entries = page, per_page, total_entries
|
11
|
-
|
12
|
-
@total_pages = (entries / @per_page.to_f).ceil
|
13
|
-
end
|
14
|
-
|
15
|
-
def self.ids_from_results(results, page, limit, options)
|
16
|
-
collection = self.new(page, limit,
|
17
|
-
results[:total] || 0, results[:total_found] || 0
|
18
|
-
)
|
19
|
-
collection.results = results
|
20
|
-
collection.replace results[:matches].collect { |match|
|
21
|
-
match[:attributes]["sphinx_internal_id"]
|
22
|
-
}
|
23
|
-
return collection
|
24
|
-
end
|
25
|
-
|
26
|
-
def self.create_from_results(results, page, limit, options)
|
27
|
-
collection = self.new(page, limit,
|
28
|
-
results[:total] || 0, results[:total_found] || 0
|
29
|
-
)
|
30
|
-
collection.results = results
|
31
|
-
collection.replace instances_from_matches(results[:matches], options)
|
32
|
-
return collection
|
33
|
-
end
|
34
|
-
|
35
|
-
def self.instances_from_matches(matches, options = {})
|
36
|
-
if klass = options[:class]
|
37
|
-
instances_from_class klass, matches, options
|
38
|
-
else
|
39
|
-
instances_from_classes matches, options
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
def self.instances_from_class(klass, matches, options = {})
|
44
|
-
index_options = klass.sphinx_index_options
|
45
|
-
|
46
|
-
ids = matches.collect { |match| match[:attributes]["sphinx_internal_id"] }
|
47
|
-
instances = ids.length > 0 ? klass.find(
|
48
|
-
:all,
|
49
|
-
:joins => options[:joins],
|
50
|
-
:conditions => {klass.primary_key.to_sym => ids},
|
51
|
-
:include => (options[:include] || index_options[:include]),
|
52
|
-
:select => (options[:select] || index_options[:select]),
|
53
|
-
:order => (options[:sql_order] || index_options[:sql_order])
|
54
|
-
) : []
|
55
|
-
|
56
|
-
# Raise an exception if we find records in Sphinx but not in the DB, so
|
57
|
-
# the search method can retry without them. See
|
58
|
-
# ThinkingSphinx::Search.retry_search_on_stale_index.
|
59
|
-
if options[:raise_on_stale] && instances.length < ids.length
|
60
|
-
stale_ids = ids - instances.map {|i| i.id }
|
61
|
-
raise StaleIdsException, stale_ids
|
62
|
-
end
|
63
|
-
|
64
|
-
# if the user has specified an SQL order, return the collection
|
65
|
-
# without rearranging it into the Sphinx order
|
66
|
-
return instances if options[:sql_order]
|
67
|
-
|
68
|
-
ids.collect { |obj_id|
|
69
|
-
instances.detect { |obj| obj.id == obj_id }
|
70
|
-
}
|
71
|
-
end
|
72
|
-
|
73
|
-
# Group results by class and call #find(:all) once for each group to reduce
|
74
|
-
# the number of #find's in multi-model searches.
|
75
|
-
#
|
76
|
-
def self.instances_from_classes(matches, options = {})
|
77
|
-
groups = matches.group_by { |match| match[:attributes]["class_crc"] }
|
78
|
-
groups.each do |crc, group|
|
79
|
-
group.replace(
|
80
|
-
instances_from_class(class_from_crc(crc), group, options)
|
81
|
-
)
|
82
|
-
end
|
83
|
-
|
84
|
-
matches.collect do |match|
|
85
|
-
groups.detect { |crc, group|
|
86
|
-
crc == match[:attributes]["class_crc"]
|
87
|
-
}[1].detect { |obj|
|
88
|
-
obj.id == match[:attributes]["sphinx_internal_id"]
|
89
|
-
}
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
def self.class_from_crc(crc)
|
94
|
-
@@models_by_crc ||= ThinkingSphinx.indexed_models.inject({}) do |hash, model|
|
95
|
-
hash[model.constantize.to_crc32] = model
|
96
|
-
model.constantize.subclasses.each { |subclass|
|
97
|
-
hash[subclass.to_crc32] = subclass.name
|
98
|
-
}
|
99
|
-
hash
|
100
|
-
end
|
101
|
-
@@models_by_crc[crc].constantize
|
102
|
-
end
|
103
|
-
|
104
|
-
def previous_page
|
105
|
-
current_page > 1 ? (current_page - 1) : nil
|
106
|
-
end
|
107
|
-
|
108
|
-
def next_page
|
109
|
-
current_page < total_pages ? (current_page + 1): nil
|
110
|
-
end
|
111
|
-
|
112
|
-
def offset
|
113
|
-
(current_page - 1) * @per_page
|
114
|
-
end
|
115
|
-
|
116
|
-
def method_missing(method, *args, &block)
|
117
|
-
super unless method.to_s[/^each_with_.*/]
|
118
|
-
|
119
|
-
each_with_attribute method.to_s.gsub(/^each_with_/, ''), &block
|
120
|
-
end
|
121
|
-
|
122
|
-
def each_with_groupby_and_count(&block)
|
123
|
-
results[:matches].each_with_index do |match, index|
|
124
|
-
yield self[index], match[:attributes]["@groupby"], match[:attributes]["@count"]
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
def each_with_attribute(attribute, &block)
|
129
|
-
results[:matches].each_with_index do |match, index|
|
130
|
-
yield self[index], (match[:attributes][attribute] || match[:attributes]["@#{attribute}"])
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
def each_with_weighting(&block)
|
135
|
-
results[:matches].each_with_index do |match, index|
|
136
|
-
yield self[index], match[:weight]
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
def inject_with_groupby_and_count(initial = nil, &block)
|
141
|
-
index = -1
|
142
|
-
results[:matches].inject(initial) do |memo, match|
|
143
|
-
index += 1
|
144
|
-
yield memo, self[index], match[:attributes]["@groupby"], match[:attributes]["@count"]
|
145
|
-
end
|
146
|
-
end
|
147
|
-
end
|
148
|
-
end
|
@@ -1,59 +0,0 @@
|
|
1
|
-
module ThinkingSphinx
|
2
|
-
class FacetCollection < Hash
|
3
|
-
attr_accessor :arguments
|
4
|
-
|
5
|
-
def initialize(arguments)
|
6
|
-
@arguments = arguments.clone
|
7
|
-
@facet_names = []
|
8
|
-
end
|
9
|
-
|
10
|
-
def add_from_results(facet, results)
|
11
|
-
name = ThinkingSphinx::Facet.name_for(facet)
|
12
|
-
|
13
|
-
self[name] ||= {}
|
14
|
-
@facet_names << name
|
15
|
-
|
16
|
-
return if results.empty?
|
17
|
-
|
18
|
-
facet = facet_from_object(results.first, facet) if facet.is_a?(String)
|
19
|
-
|
20
|
-
results.each_with_groupby_and_count { |result, group, count|
|
21
|
-
facet_value = facet.value(result, group)
|
22
|
-
|
23
|
-
self[name][facet_value] ||= 0
|
24
|
-
self[name][facet_value] += count
|
25
|
-
}
|
26
|
-
end
|
27
|
-
|
28
|
-
def for(hash = {})
|
29
|
-
arguments = @arguments.clone
|
30
|
-
options = arguments.extract_options!
|
31
|
-
options[:with] ||= {}
|
32
|
-
|
33
|
-
hash.each do |key, value|
|
34
|
-
attrib = ThinkingSphinx::Facet.attribute_name_from_value(key, value)
|
35
|
-
options[:with][attrib] = underlying_value key, value
|
36
|
-
end
|
37
|
-
|
38
|
-
arguments << options
|
39
|
-
ThinkingSphinx::Search.search *arguments
|
40
|
-
end
|
41
|
-
|
42
|
-
private
|
43
|
-
|
44
|
-
def underlying_value(key, value)
|
45
|
-
case value
|
46
|
-
when Array
|
47
|
-
value.collect { |item| underlying_value(key, item) }
|
48
|
-
when String
|
49
|
-
value.to_crc32
|
50
|
-
else
|
51
|
-
value
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
def facet_from_object(object, name)
|
56
|
-
object.sphinx_facets.detect { |facet| facet.attribute_name == name }
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
@@ -1,104 +0,0 @@
|
|
1
|
-
module ThinkingSphinx
|
2
|
-
class Search
|
3
|
-
module Facets
|
4
|
-
# Model.facets *args
|
5
|
-
# ThinkingSphinx::Search.facets *args
|
6
|
-
# ThinkingSphinx::Search.facets *args, :all_attributes => true
|
7
|
-
# ThinkingSphinx::Search.facets *args, :class_facet => false
|
8
|
-
#
|
9
|
-
def facets(*args)
|
10
|
-
options = args.extract_options!
|
11
|
-
|
12
|
-
if options[:class]
|
13
|
-
facets_for_model options[:class], args, options
|
14
|
-
else
|
15
|
-
facets_for_all_models args, options
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
private
|
20
|
-
|
21
|
-
def facets_for_model(klass, args, options)
|
22
|
-
hash = ThinkingSphinx::FacetCollection.new args + [options]
|
23
|
-
options = options.clone.merge! facet_query_options
|
24
|
-
|
25
|
-
facets = klass.sphinx_facets
|
26
|
-
facets = Array(options.delete(:facets)).collect { |name|
|
27
|
-
klass.sphinx_facets.detect { |facet| facet.name.to_s == name.to_s }
|
28
|
-
}.compact if options[:facets]
|
29
|
-
|
30
|
-
facets.inject(hash) do |hash, facet|
|
31
|
-
unless facet.name == :class && !options[:class_facet]
|
32
|
-
options[:group_by] = facet.attribute_name
|
33
|
-
hash.add_from_results facet, search(*(args + [options]))
|
34
|
-
end
|
35
|
-
|
36
|
-
hash
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
def facets_for_all_models(args, options)
|
41
|
-
options = GlobalFacetOptions.merge(options)
|
42
|
-
hash = ThinkingSphinx::FacetCollection.new args + [options]
|
43
|
-
options = options.merge! facet_query_options
|
44
|
-
|
45
|
-
facet_names(options).inject(hash) do |hash, name|
|
46
|
-
options[:group_by] = name
|
47
|
-
hash.add_from_results name, search(*(args + [options]))
|
48
|
-
hash
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
def facet_query_options
|
53
|
-
config = ThinkingSphinx::Configuration.instance
|
54
|
-
max = config.configuration.searchd.max_matches || 1000
|
55
|
-
|
56
|
-
{
|
57
|
-
:group_function => :attr,
|
58
|
-
:limit => max,
|
59
|
-
:max_matches => max,
|
60
|
-
:page => 1
|
61
|
-
}
|
62
|
-
end
|
63
|
-
|
64
|
-
def facet_classes(options)
|
65
|
-
options[:classes] || ThinkingSphinx.indexed_models.collect { |model|
|
66
|
-
model.constantize
|
67
|
-
}
|
68
|
-
end
|
69
|
-
|
70
|
-
def facet_names(options)
|
71
|
-
classes = facet_classes(options)
|
72
|
-
names = options[:all_attributes] ?
|
73
|
-
facet_names_for_all_classes(classes) :
|
74
|
-
facet_names_common_to_all_classes(classes)
|
75
|
-
|
76
|
-
names.delete "class_crc" unless options[:class_facet]
|
77
|
-
names
|
78
|
-
end
|
79
|
-
|
80
|
-
def facet_names_for_all_classes(classes)
|
81
|
-
all_facets = classes.collect { |klass| klass.sphinx_facets }.flatten
|
82
|
-
|
83
|
-
all_facets.group_by { |facet|
|
84
|
-
facet.name
|
85
|
-
}.collect { |name, facets|
|
86
|
-
if facets.collect { |facet| facet.type }.uniq.length > 1
|
87
|
-
raise "Facet #{name} exists in more than one model with different types"
|
88
|
-
end
|
89
|
-
facets.first.attribute_name
|
90
|
-
}
|
91
|
-
end
|
92
|
-
|
93
|
-
def facet_names_common_to_all_classes(classes)
|
94
|
-
facet_names_for_all_classes(classes).select { |name|
|
95
|
-
classes.all? { |klass|
|
96
|
-
klass.sphinx_facets.detect { |facet|
|
97
|
-
facet.attribute_name == name
|
98
|
-
}
|
99
|
-
}
|
100
|
-
}
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
|
-
end
|