ruben-sunspot 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +9 -0
- data/Gemfile.lock +22 -0
- data/History.txt +198 -0
- data/LICENSE +18 -0
- data/README.rdoc +244 -0
- data/Rakefile +11 -0
- data/TODO +13 -0
- data/VERSION.yml +4 -0
- data/bin/sunspot-installer +19 -0
- data/bin/sunspot-solr +74 -0
- data/installer/config/schema.yml +95 -0
- data/lib/light_config.rb +40 -0
- data/lib/sunspot.rb +529 -0
- data/lib/sunspot/adapters.rb +265 -0
- data/lib/sunspot/composite_setup.rb +202 -0
- data/lib/sunspot/configuration.rb +46 -0
- data/lib/sunspot/data_extractor.rb +50 -0
- data/lib/sunspot/dsl.rb +5 -0
- data/lib/sunspot/dsl/adjustable.rb +47 -0
- data/lib/sunspot/dsl/field_query.rb +266 -0
- data/lib/sunspot/dsl/fields.rb +113 -0
- data/lib/sunspot/dsl/fulltext.rb +243 -0
- data/lib/sunspot/dsl/function.rb +14 -0
- data/lib/sunspot/dsl/functional.rb +41 -0
- data/lib/sunspot/dsl/more_like_this_query.rb +56 -0
- data/lib/sunspot/dsl/paginatable.rb +28 -0
- data/lib/sunspot/dsl/query_facet.rb +36 -0
- data/lib/sunspot/dsl/restriction.rb +25 -0
- data/lib/sunspot/dsl/scope.rb +229 -0
- data/lib/sunspot/dsl/search.rb +30 -0
- data/lib/sunspot/dsl/standard_query.rb +125 -0
- data/lib/sunspot/field.rb +192 -0
- data/lib/sunspot/field_factory.rb +147 -0
- data/lib/sunspot/indexer.rb +131 -0
- data/lib/sunspot/installer.rb +31 -0
- data/lib/sunspot/installer/library_installer.rb +45 -0
- data/lib/sunspot/installer/schema_builder.rb +219 -0
- data/lib/sunspot/installer/solrconfig_updater.rb +106 -0
- data/lib/sunspot/installer/task_helper.rb +18 -0
- data/lib/sunspot/query.rb +10 -0
- data/lib/sunspot/query/abstract_field_facet.rb +52 -0
- data/lib/sunspot/query/boost_query.rb +24 -0
- data/lib/sunspot/query/common_query.rb +85 -0
- data/lib/sunspot/query/composite_fulltext.rb +31 -0
- data/lib/sunspot/query/connective.rb +195 -0
- data/lib/sunspot/query/date_field_facet.rb +14 -0
- data/lib/sunspot/query/dismax.rb +126 -0
- data/lib/sunspot/query/field_facet.rb +41 -0
- data/lib/sunspot/query/filter.rb +38 -0
- data/lib/sunspot/query/function_query.rb +52 -0
- data/lib/sunspot/query/highlighting.rb +55 -0
- data/lib/sunspot/query/local.rb +26 -0
- data/lib/sunspot/query/more_like_this.rb +60 -0
- data/lib/sunspot/query/more_like_this_query.rb +12 -0
- data/lib/sunspot/query/pagination.rb +38 -0
- data/lib/sunspot/query/query_facet.rb +16 -0
- data/lib/sunspot/query/restriction.rb +265 -0
- data/lib/sunspot/query/scope.rb +16 -0
- data/lib/sunspot/query/sort.rb +95 -0
- data/lib/sunspot/query/sort_composite.rb +33 -0
- data/lib/sunspot/query/standard_query.rb +20 -0
- data/lib/sunspot/query/text_field_boost.rb +17 -0
- data/lib/sunspot/schema.rb +151 -0
- data/lib/sunspot/search.rb +9 -0
- data/lib/sunspot/search/abstract_search.rb +302 -0
- data/lib/sunspot/search/date_facet.rb +35 -0
- data/lib/sunspot/search/facet_row.rb +27 -0
- data/lib/sunspot/search/field_facet.rb +88 -0
- data/lib/sunspot/search/highlight.rb +38 -0
- data/lib/sunspot/search/hit.rb +136 -0
- data/lib/sunspot/search/more_like_this_search.rb +31 -0
- data/lib/sunspot/search/query_facet.rb +62 -0
- data/lib/sunspot/search/standard_search.rb +21 -0
- data/lib/sunspot/server.rb +152 -0
- data/lib/sunspot/session.rb +252 -0
- data/lib/sunspot/session_proxy.rb +71 -0
- data/lib/sunspot/session_proxy/abstract_session_proxy.rb +29 -0
- data/lib/sunspot/session_proxy/class_sharding_session_proxy.rb +66 -0
- data/lib/sunspot/session_proxy/id_sharding_session_proxy.rb +89 -0
- data/lib/sunspot/session_proxy/master_slave_session_proxy.rb +43 -0
- data/lib/sunspot/session_proxy/sharding_session_proxy.rb +215 -0
- data/lib/sunspot/session_proxy/thread_local_session_proxy.rb +37 -0
- data/lib/sunspot/setup.rb +360 -0
- data/lib/sunspot/text_field_setup.rb +29 -0
- data/lib/sunspot/type.rb +342 -0
- data/lib/sunspot/util.rb +253 -0
- data/lib/sunspot/version.rb +3 -0
- data/solr/etc/jetty.xml +214 -0
- data/solr/etc/webdefault.xml +379 -0
- data/solr/lib/jetty-6.1.3.jar +0 -0
- data/solr/lib/jetty-util-6.1.3.jar +0 -0
- data/solr/lib/jsp-2.1/ant-1.6.5.jar +0 -0
- data/solr/lib/jsp-2.1/core-3.1.1.jar +0 -0
- data/solr/lib/jsp-2.1/jsp-2.1.jar +0 -0
- data/solr/lib/jsp-2.1/jsp-api-2.1.jar +0 -0
- data/solr/lib/servlet-api-2.5-6.1.3.jar +0 -0
- data/solr/solr/conf/admin-extra.html +31 -0
- data/solr/solr/conf/elevate.xml +36 -0
- data/solr/solr/conf/mapping-ISOLatin1Accent.txt +246 -0
- data/solr/solr/conf/protwords.txt +21 -0
- data/solr/solr/conf/schema.xml +238 -0
- data/solr/solr/conf/scripts.conf +24 -0
- data/solr/solr/conf/solrconfig.xml +938 -0
- data/solr/solr/conf/spellings.txt +2 -0
- data/solr/solr/conf/stopwords.txt +58 -0
- data/solr/solr/conf/synonyms.txt +31 -0
- data/solr/solr/lib/lucene-spatial-2.9.1.jar +0 -0
- data/solr/solr/lib/solr-spatial-light-0.0.6.jar +0 -0
- data/solr/start.jar +0 -0
- data/solr/webapps/solr.war +0 -0
- data/spec/api/adapters_spec.rb +33 -0
- data/spec/api/binding_spec.rb +38 -0
- data/spec/api/indexer/attributes_spec.rb +149 -0
- data/spec/api/indexer/batch_spec.rb +46 -0
- data/spec/api/indexer/dynamic_fields_spec.rb +42 -0
- data/spec/api/indexer/fixed_fields_spec.rb +57 -0
- data/spec/api/indexer/fulltext_spec.rb +43 -0
- data/spec/api/indexer/removal_spec.rb +53 -0
- data/spec/api/indexer/spec_helper.rb +1 -0
- data/spec/api/indexer_spec.rb +14 -0
- data/spec/api/query/advanced_manipulation_examples.rb +35 -0
- data/spec/api/query/connectives_examples.rb +186 -0
- data/spec/api/query/dsl_spec.rb +18 -0
- data/spec/api/query/dynamic_fields_examples.rb +165 -0
- data/spec/api/query/faceting_examples.rb +399 -0
- data/spec/api/query/fulltext_examples.rb +315 -0
- data/spec/api/query/function_spec.rb +70 -0
- data/spec/api/query/highlighting_examples.rb +225 -0
- data/spec/api/query/local_examples.rb +38 -0
- data/spec/api/query/more_like_this_spec.rb +140 -0
- data/spec/api/query/ordering_pagination_examples.rb +97 -0
- data/spec/api/query/scope_examples.rb +263 -0
- data/spec/api/query/spec_helper.rb +1 -0
- data/spec/api/query/standard_spec.rb +28 -0
- data/spec/api/query/text_field_scoping_examples.rb +30 -0
- data/spec/api/query/types_spec.rb +20 -0
- data/spec/api/search/dynamic_fields_spec.rb +33 -0
- data/spec/api/search/faceting_spec.rb +356 -0
- data/spec/api/search/highlighting_spec.rb +69 -0
- data/spec/api/search/hits_spec.rb +149 -0
- data/spec/api/search/results_spec.rb +79 -0
- data/spec/api/search/search_spec.rb +23 -0
- data/spec/api/search/spec_helper.rb +1 -0
- data/spec/api/server_spec.rb +91 -0
- data/spec/api/session_proxy/class_sharding_session_proxy_spec.rb +85 -0
- data/spec/api/session_proxy/id_sharding_session_proxy_spec.rb +30 -0
- data/spec/api/session_proxy/master_slave_session_proxy_spec.rb +41 -0
- data/spec/api/session_proxy/sharding_session_proxy_spec.rb +77 -0
- data/spec/api/session_proxy/spec_helper.rb +9 -0
- data/spec/api/session_proxy/thread_local_session_proxy_spec.rb +50 -0
- data/spec/api/session_spec.rb +198 -0
- data/spec/api/spec_helper.rb +3 -0
- data/spec/api/sunspot_spec.rb +18 -0
- data/spec/ext.rb +11 -0
- data/spec/helpers/indexer_helper.rb +29 -0
- data/spec/helpers/query_helper.rb +38 -0
- data/spec/helpers/search_helper.rb +80 -0
- data/spec/integration/dynamic_fields_spec.rb +55 -0
- data/spec/integration/faceting_spec.rb +238 -0
- data/spec/integration/highlighting_spec.rb +22 -0
- data/spec/integration/indexing_spec.rb +33 -0
- data/spec/integration/keyword_search_spec.rb +317 -0
- data/spec/integration/local_search_spec.rb +91 -0
- data/spec/integration/more_like_this_spec.rb +43 -0
- data/spec/integration/scoped_search_spec.rb +349 -0
- data/spec/integration/spec_helper.rb +7 -0
- data/spec/integration/stored_fields_spec.rb +10 -0
- data/spec/integration/test_pagination.rb +32 -0
- data/spec/mocks/adapters.rb +32 -0
- data/spec/mocks/blog.rb +3 -0
- data/spec/mocks/comment.rb +21 -0
- data/spec/mocks/connection.rb +126 -0
- data/spec/mocks/mock_adapter.rb +30 -0
- data/spec/mocks/mock_class_sharding_session_proxy.rb +24 -0
- data/spec/mocks/mock_record.rb +52 -0
- data/spec/mocks/mock_sharding_session_proxy.rb +15 -0
- data/spec/mocks/photo.rb +12 -0
- data/spec/mocks/post.rb +76 -0
- data/spec/mocks/super_class.rb +2 -0
- data/spec/mocks/user.rb +8 -0
- data/spec/spec_helper.rb +52 -0
- data/tasks/gemspec.rake +33 -0
- data/tasks/rcov.rake +28 -0
- data/tasks/rdoc.rake +27 -0
- data/tasks/schema.rake +19 -0
- data/tasks/spec.rake +24 -0
- data/tasks/todo.rake +4 -0
- metadata +356 -0
@@ -0,0 +1,265 @@
|
|
1
|
+
module Sunspot
|
2
|
+
module Query
|
3
|
+
module Restriction #:nodoc:
|
4
|
+
class <<self
|
5
|
+
#
|
6
|
+
# Return the names of all of the restriction classes that should be made
|
7
|
+
# available to the DSL.
|
8
|
+
#
|
9
|
+
# ==== Returns
|
10
|
+
#
|
11
|
+
# Array:: Collection of restriction class names
|
12
|
+
#
|
13
|
+
def names
|
14
|
+
constants - %w(Base) #XXX this seems ugly
|
15
|
+
end
|
16
|
+
|
17
|
+
#
|
18
|
+
# Convenience method to access a restriction class by an underscored
|
19
|
+
# symbol or string
|
20
|
+
#
|
21
|
+
def [](restriction_name)
|
22
|
+
@types ||= {}
|
23
|
+
@types[restriction_name.to_sym] ||= const_get(Sunspot::Util.camel_case(restriction_name.to_s))
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
#
|
28
|
+
# Subclasses of this class represent restrictions that can be applied to
|
29
|
+
# a Sunspot query. The Sunspot::DSL::Restriction class presents a builder
|
30
|
+
# API for instances of this class.
|
31
|
+
#
|
32
|
+
# Implementations of this class must respond to #to_params and
|
33
|
+
# #to_negated_params. Instead of implementing those methods, they may
|
34
|
+
# choose to implement any of:
|
35
|
+
#
|
36
|
+
# * #to_positive_boolean_phrase, and optionally #to_negated_boolean_phrase
|
37
|
+
# * #to_solr_conditional
|
38
|
+
#
|
39
|
+
class Base #:nodoc:
|
40
|
+
include Filter
|
41
|
+
include RSolr::Char
|
42
|
+
|
43
|
+
RESERVED_WORDS = Set['AND', 'OR', 'NOT']
|
44
|
+
|
45
|
+
def initialize(field, value, negated = false)
|
46
|
+
@field, @value, @negated = field, value, negated
|
47
|
+
end
|
48
|
+
|
49
|
+
#
|
50
|
+
# A hash representing this restriction in solr-ruby's parameter format.
|
51
|
+
# All restriction implementations must respond to this method; however,
|
52
|
+
# the base implementation delegates to the #to_positive_boolean_phrase method, so
|
53
|
+
# subclasses may (and probably should) choose to implement that method
|
54
|
+
# instead.
|
55
|
+
#
|
56
|
+
# ==== Returns
|
57
|
+
#
|
58
|
+
# Hash:: Representation of this restriction as solr-ruby parameters
|
59
|
+
#
|
60
|
+
def to_params
|
61
|
+
{ :fq => [to_filter_query] }
|
62
|
+
end
|
63
|
+
|
64
|
+
#
|
65
|
+
# Return the boolean phrase associated with this restriction object.
|
66
|
+
# Differentiates between positive and negated boolean phrases depending
|
67
|
+
# on whether this restriction is negated.
|
68
|
+
#
|
69
|
+
def to_boolean_phrase
|
70
|
+
unless negated?
|
71
|
+
to_positive_boolean_phrase
|
72
|
+
else
|
73
|
+
to_negated_boolean_phrase
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
#
|
78
|
+
# Boolean phrase representing this restriction in the positive. Subclasses
|
79
|
+
# may choose to implement this method rather than #to_params; however,
|
80
|
+
# this method delegates to the abstract #to_solr_conditional method, which
|
81
|
+
# in most cases will be what subclasses will want to implement.
|
82
|
+
# #to_solr_conditional contains the boolean phrase representing the
|
83
|
+
# condition but leaves out the field name (see built-in implementations
|
84
|
+
# for examples)
|
85
|
+
#
|
86
|
+
# ==== Returns
|
87
|
+
#
|
88
|
+
# String:: Boolean phrase for restriction in the positive
|
89
|
+
#
|
90
|
+
def to_positive_boolean_phrase
|
91
|
+
"#{escape(@field.indexed_name)}:#{to_solr_conditional}"
|
92
|
+
end
|
93
|
+
|
94
|
+
#
|
95
|
+
# Boolean phrase representing this restriction in the negated. Subclasses
|
96
|
+
# may choose to implement this method, but it is not necessary, as the
|
97
|
+
# base implementation delegates to #to_positive_boolean_phrase.
|
98
|
+
#
|
99
|
+
# ==== Returns
|
100
|
+
#
|
101
|
+
# String:: Boolean phrase for restriction in the negated
|
102
|
+
#
|
103
|
+
def to_negated_boolean_phrase
|
104
|
+
"-#{to_positive_boolean_phrase}"
|
105
|
+
end
|
106
|
+
|
107
|
+
#
|
108
|
+
# Whether this restriction should be negated from its original meaning
|
109
|
+
#
|
110
|
+
def negated? #:nodoc:
|
111
|
+
!!@negated
|
112
|
+
end
|
113
|
+
|
114
|
+
#
|
115
|
+
# Return a new restriction that is the negated version of this one. It
|
116
|
+
# is used by disjunction denormalization.
|
117
|
+
#
|
118
|
+
def negate
|
119
|
+
self.class.new(@field, @value, !@negated)
|
120
|
+
end
|
121
|
+
|
122
|
+
protected
|
123
|
+
|
124
|
+
#
|
125
|
+
# Return escaped Solr API representation of given value
|
126
|
+
#
|
127
|
+
# ==== Parameters
|
128
|
+
#
|
129
|
+
# value<Object>::
|
130
|
+
# value to convert to Solr representation (default: @value)
|
131
|
+
#
|
132
|
+
# ==== Returns
|
133
|
+
#
|
134
|
+
# String:: Solr API representation of given value
|
135
|
+
#
|
136
|
+
def solr_value(value = @value)
|
137
|
+
solr_value = escape(@field.to_indexed(value))
|
138
|
+
if RESERVED_WORDS.include?(solr_value)
|
139
|
+
%Q("#{solr_value}")
|
140
|
+
else
|
141
|
+
solr_value
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
#
|
147
|
+
# Results must have field with value equal to given value. If the value
|
148
|
+
# is nil, results must have no value for the given field.
|
149
|
+
#
|
150
|
+
class EqualTo < Base
|
151
|
+
def to_positive_boolean_phrase
|
152
|
+
case @value
|
153
|
+
when nil
|
154
|
+
"#{escape(@field.indexed_name)}:[* TO *]"
|
155
|
+
when ''
|
156
|
+
%Q(#{escape(@field.indexed_name)}:[* TO ""])
|
157
|
+
else
|
158
|
+
super
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
def negated?
|
163
|
+
if @value.nil?
|
164
|
+
!super
|
165
|
+
else
|
166
|
+
super
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
private
|
171
|
+
|
172
|
+
def to_solr_conditional
|
173
|
+
"#{solr_value}"
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
#
|
178
|
+
# Results must have field with value less than given value
|
179
|
+
#
|
180
|
+
class LessThan < Base
|
181
|
+
private
|
182
|
+
|
183
|
+
def solr_value(value = @value)
|
184
|
+
solr_value = super
|
185
|
+
solr_value = "\"#{solr_value}\"" if solr_value.index(' ')
|
186
|
+
solr_value
|
187
|
+
end
|
188
|
+
|
189
|
+
def to_solr_conditional
|
190
|
+
"[* TO #{solr_value}]"
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
#
|
195
|
+
# Results must have field with value greater than given value
|
196
|
+
#
|
197
|
+
class GreaterThan < Base
|
198
|
+
private
|
199
|
+
|
200
|
+
def solr_value(value = @value)
|
201
|
+
solr_value = super
|
202
|
+
solr_value = "\"#{solr_value}\"" if solr_value.index(' ')
|
203
|
+
solr_value
|
204
|
+
end
|
205
|
+
|
206
|
+
def to_solr_conditional
|
207
|
+
"[#{solr_value} TO *]"
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
#
|
212
|
+
# Results must have field with value in given range
|
213
|
+
#
|
214
|
+
class Between < Base
|
215
|
+
private
|
216
|
+
|
217
|
+
def solr_value(value = @value)
|
218
|
+
solr_value = super
|
219
|
+
solr_value = "\"#{solr_value}\"" if solr_value.index(' ')
|
220
|
+
solr_value
|
221
|
+
end
|
222
|
+
|
223
|
+
def to_solr_conditional
|
224
|
+
first, last = [@value.first, @value.last].sort
|
225
|
+
"[#{solr_value(first)} TO #{solr_value(last)}]"
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
#
|
230
|
+
# Results must have field with value included in given collection
|
231
|
+
#
|
232
|
+
class AnyOf < Base
|
233
|
+
private
|
234
|
+
|
235
|
+
def to_solr_conditional
|
236
|
+
"(#{@value.map { |v| solr_value v } * ' OR '})"
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
#
|
241
|
+
# Results must have field with values matching all values in given
|
242
|
+
# collection (only makes sense for fields with multiple values)
|
243
|
+
#
|
244
|
+
class AllOf < Base
|
245
|
+
private
|
246
|
+
|
247
|
+
def to_solr_conditional
|
248
|
+
"(#{@value.map { |v| solr_value v } * ' AND '})"
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
#
|
253
|
+
# Results must have a field with a value that begins with the argument.
|
254
|
+
# Most useful for strings, but in theory will work with anything.
|
255
|
+
#
|
256
|
+
class StartingWith < Base
|
257
|
+
private
|
258
|
+
|
259
|
+
def to_solr_conditional
|
260
|
+
"#{solr_value(@value)}*"
|
261
|
+
end
|
262
|
+
end
|
263
|
+
end
|
264
|
+
end
|
265
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Sunspot
|
2
|
+
module Query
|
3
|
+
class Scope < Connective::Conjunction
|
4
|
+
def to_params
|
5
|
+
filters = []
|
6
|
+
@components.each do |component|
|
7
|
+
filter = component.to_filter_query
|
8
|
+
filters << filter unless filter.nil?
|
9
|
+
end
|
10
|
+
if filters.empty? then {}
|
11
|
+
else { :fq => filters}
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
module Sunspot
|
2
|
+
module Query
|
3
|
+
#
|
4
|
+
# The classes in this module implement query components that build sort
|
5
|
+
# parameters for Solr. As well as regular sort on fields, there are several
|
6
|
+
# "special" sorts that allow ordering for metrics calculated during the
|
7
|
+
# search.
|
8
|
+
#
|
9
|
+
module Sort #:nodoc: all
|
10
|
+
DIRECTIONS = {
|
11
|
+
:asc => 'asc',
|
12
|
+
:ascending => 'asc',
|
13
|
+
:desc => 'desc',
|
14
|
+
:descending => 'desc'
|
15
|
+
}
|
16
|
+
|
17
|
+
class <<self
|
18
|
+
#
|
19
|
+
# Certain field names are "special", referring to specific non-field
|
20
|
+
# sorts, which are generally by other metrics associated with hits.
|
21
|
+
#
|
22
|
+
# XXX I'm not entirely convinced it's a good idea to prevent anyone from
|
23
|
+
# ever sorting by a field named 'score', etc.
|
24
|
+
#
|
25
|
+
def special(name)
|
26
|
+
special_class_name = "#{Util.camel_case(name.to_s)}Sort"
|
27
|
+
if const_defined?(special_class_name) && special_class_name != 'FieldSort'
|
28
|
+
const_get(special_class_name)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
#
|
34
|
+
# Base class for sorts. All subclasses should implement the #to_param
|
35
|
+
# method, which is a string that is then concatenated with other sort
|
36
|
+
# strings by the SortComposite to form the sort parameter.
|
37
|
+
#
|
38
|
+
class Abstract
|
39
|
+
def initialize(direction)
|
40
|
+
@direction = (direction || :asc).to_sym
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
#
|
46
|
+
# Translate fairly forgiving direction argument into solr direction
|
47
|
+
#
|
48
|
+
def direction_for_solr
|
49
|
+
DIRECTIONS[@direction] ||
|
50
|
+
raise(
|
51
|
+
ArgumentError,
|
52
|
+
"Unknown sort direction #{@direction}. Acceptable input is: #{DIRECTIONS.keys.map { |input| input.inspect } * ', '}"
|
53
|
+
)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
#
|
58
|
+
# A FieldSort is the usual kind of sort, by the value of a particular
|
59
|
+
# field, ascending or descending
|
60
|
+
#
|
61
|
+
class FieldSort < Abstract
|
62
|
+
def initialize(field, direction = nil)
|
63
|
+
if field.multiple?
|
64
|
+
raise(ArgumentError, "#{field.name} cannot be used for ordering because it is a multiple-value field")
|
65
|
+
end
|
66
|
+
@field, @direction = field, (direction || :asc).to_sym
|
67
|
+
end
|
68
|
+
|
69
|
+
def to_param
|
70
|
+
"#{@field.indexed_name.to_sym} #{direction_for_solr}"
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
#
|
75
|
+
# A RandomSort uses Solr's random field functionality to sort results
|
76
|
+
# (usually) randomly.
|
77
|
+
#
|
78
|
+
class RandomSort < Abstract
|
79
|
+
def to_param
|
80
|
+
"random_#{rand(1<<16)} #{direction_for_solr}"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
#
|
85
|
+
# A ScoreSort sorts by keyword relevance score. This is only useful when
|
86
|
+
# performing fulltext search.
|
87
|
+
#
|
88
|
+
class ScoreSort < Abstract
|
89
|
+
def to_param
|
90
|
+
"score #{direction_for_solr}"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Sunspot
|
2
|
+
module Query
|
3
|
+
#
|
4
|
+
# The SortComposite class encapsulates an ordered collection of Sort
|
5
|
+
# objects. It's necessary to keep this as a separate class as Solr takes
|
6
|
+
# the sort as a single parameter, so adding sorts as regular components
|
7
|
+
# would not merge correctly in the #to_params method.
|
8
|
+
#
|
9
|
+
class SortComposite #:nodoc:
|
10
|
+
def initialize
|
11
|
+
@sorts = []
|
12
|
+
end
|
13
|
+
|
14
|
+
#
|
15
|
+
# Add a sort to the composite
|
16
|
+
#
|
17
|
+
def <<(sort)
|
18
|
+
@sorts << sort
|
19
|
+
end
|
20
|
+
|
21
|
+
#
|
22
|
+
# Combine the sorts into a single param by joining them
|
23
|
+
#
|
24
|
+
def to_params
|
25
|
+
unless @sorts.empty?
|
26
|
+
{ :sort => @sorts.map { |sort| sort.to_param } * ', ' }
|
27
|
+
else
|
28
|
+
{}
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Sunspot
|
2
|
+
module Query
|
3
|
+
class StandardQuery < CommonQuery
|
4
|
+
attr_accessor :scope, :fulltext
|
5
|
+
|
6
|
+
def initialize(types)
|
7
|
+
super
|
8
|
+
@components << @fulltext = CompositeFulltext.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def add_fulltext(keywords)
|
12
|
+
@fulltext.add(keywords)
|
13
|
+
end
|
14
|
+
|
15
|
+
def add_location_restriction(coordinates, radius)
|
16
|
+
@components << @local = Local.new(coordinates, radius)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|