nxa-sunspot 0.10.7
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +153 -0
- data/LICENSE +18 -0
- data/README.rdoc +191 -0
- data/Rakefile +9 -0
- data/TODO +14 -0
- data/VERSION.yml +4 -0
- data/bin/sunspot-configure-solr +40 -0
- data/bin/sunspot-solr +95 -0
- data/lib/light_config.rb +40 -0
- data/lib/sunspot.rb +488 -0
- data/lib/sunspot/adapters.rb +265 -0
- data/lib/sunspot/composite_setup.rb +188 -0
- data/lib/sunspot/configuration.rb +56 -0
- data/lib/sunspot/data_extractor.rb +50 -0
- data/lib/sunspot/dsl.rb +4 -0
- data/lib/sunspot/dsl/field_query.rb +150 -0
- data/lib/sunspot/dsl/fields.rb +100 -0
- data/lib/sunspot/dsl/fulltext.rb +228 -0
- data/lib/sunspot/dsl/query.rb +162 -0
- data/lib/sunspot/dsl/query_facet.rb +36 -0
- data/lib/sunspot/dsl/restriction.rb +25 -0
- data/lib/sunspot/dsl/scope.rb +225 -0
- data/lib/sunspot/dsl/search.rb +30 -0
- data/lib/sunspot/field.rb +181 -0
- data/lib/sunspot/field_factory.rb +144 -0
- data/lib/sunspot/indexer.rb +133 -0
- data/lib/sunspot/query.rb +9 -0
- data/lib/sunspot/query/abstract_field_facet.rb +43 -0
- data/lib/sunspot/query/boost_query.rb +20 -0
- data/lib/sunspot/query/connective.rb +189 -0
- data/lib/sunspot/query/date_field_facet.rb +14 -0
- data/lib/sunspot/query/dismax.rb +88 -0
- data/lib/sunspot/query/field_facet.rb +9 -0
- data/lib/sunspot/query/highlighting.rb +55 -0
- data/lib/sunspot/query/local.rb +27 -0
- data/lib/sunspot/query/pagination.rb +38 -0
- data/lib/sunspot/query/query.rb +86 -0
- data/lib/sunspot/query/query_facet.rb +16 -0
- data/lib/sunspot/query/restriction.rb +254 -0
- data/lib/sunspot/query/scope.rb +9 -0
- data/lib/sunspot/query/sort.rb +105 -0
- data/lib/sunspot/query/sort_composite.rb +33 -0
- data/lib/sunspot/query/text_field_boost.rb +15 -0
- data/lib/sunspot/schema.rb +147 -0
- data/lib/sunspot/search.rb +216 -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 +44 -0
- data/lib/sunspot/search/highlight.rb +38 -0
- data/lib/sunspot/search/hit.rb +117 -0
- data/lib/sunspot/search/query_facet.rb +62 -0
- data/lib/sunspot/session.rb +236 -0
- data/lib/sunspot/setup.rb +323 -0
- data/lib/sunspot/text_field_setup.rb +29 -0
- data/lib/sunspot/type.rb +204 -0
- data/lib/sunspot/util.rb +210 -0
- data/solr/etc/jetty.xml +212 -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/elevate.xml +36 -0
- data/solr/solr/conf/protwords.txt +21 -0
- data/solr/solr/conf/schema.xml +64 -0
- data/solr/solr/conf/solrconfig.xml +725 -0
- data/solr/solr/conf/stopwords.txt +57 -0
- data/solr/solr/conf/synonyms.txt +31 -0
- data/solr/solr/lib/geoapi-nogenerics-2.1-M2.jar +0 -0
- data/solr/solr/lib/gt2-referencing-2.3.1.jar +0 -0
- data/solr/solr/lib/jsr108-0.01.jar +0 -0
- data/solr/solr/lib/locallucene.jar +0 -0
- data/solr/solr/lib/localsolr.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/indexer/attributes_spec.rb +110 -0
- data/spec/api/indexer/batch_spec.rb +46 -0
- data/spec/api/indexer/dynamic_fields_spec.rb +33 -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 +46 -0
- data/spec/api/indexer/spec_helper.rb +1 -0
- data/spec/api/indexer_spec.rb +4 -0
- data/spec/api/query/adjust_params_spec.rb +37 -0
- data/spec/api/query/connectives_spec.rb +176 -0
- data/spec/api/query/dsl_spec.rb +12 -0
- data/spec/api/query/dynamic_fields_spec.rb +149 -0
- data/spec/api/query/faceting_spec.rb +296 -0
- data/spec/api/query/fulltext_spec.rb +281 -0
- data/spec/api/query/highlighting_spec.rb +225 -0
- data/spec/api/query/local_spec.rb +62 -0
- data/spec/api/query/ordering_pagination_spec.rb +95 -0
- data/spec/api/query/scope_spec.rb +266 -0
- data/spec/api/query/spec_helper.rb +1 -0
- data/spec/api/query/text_field_scoping_spec.rb +30 -0
- data/spec/api/query/types_spec.rb +20 -0
- data/spec/api/search/dynamic_fields_spec.rb +27 -0
- data/spec/api/search/faceting_spec.rb +242 -0
- data/spec/api/search/highlighting_spec.rb +65 -0
- data/spec/api/search/hits_spec.rb +67 -0
- data/spec/api/search/results_spec.rb +52 -0
- data/spec/api/search/search_spec.rb +23 -0
- data/spec/api/search/spec_helper.rb +1 -0
- data/spec/api/session_spec.rb +198 -0
- data/spec/api/spec_helper.rb +1 -0
- data/spec/api/sunspot_spec.rb +18 -0
- data/spec/helpers/indexer_helper.rb +29 -0
- data/spec/helpers/query_helper.rb +13 -0
- data/spec/helpers/search_helper.rb +78 -0
- data/spec/integration/dynamic_fields_spec.rb +55 -0
- data/spec/integration/faceting_spec.rb +188 -0
- data/spec/integration/highlighting_spec.rb +22 -0
- data/spec/integration/indexing_spec.rb +7 -0
- data/spec/integration/keyword_search_spec.rb +245 -0
- data/spec/integration/local_search_spec.rb +56 -0
- data/spec/integration/scoped_search_spec.rb +303 -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 +19 -0
- data/spec/mocks/connection.rb +106 -0
- data/spec/mocks/mock_adapter.rb +30 -0
- data/spec/mocks/mock_record.rb +48 -0
- data/spec/mocks/photo.rb +11 -0
- data/spec/mocks/post.rb +75 -0
- data/spec/mocks/super_class.rb +2 -0
- data/spec/mocks/user.rb +8 -0
- data/spec/spec_helper.rb +68 -0
- data/tasks/gemspec.rake +42 -0
- data/tasks/rcov.rake +28 -0
- data/tasks/rdoc.rake +22 -0
- data/tasks/schema.rake +19 -0
- data/tasks/spec.rake +24 -0
- data/tasks/todo.rake +4 -0
- data/templates/schema.xml.erb +36 -0
- metadata +319 -0
@@ -0,0 +1,38 @@
|
|
1
|
+
module Sunspot
|
2
|
+
module Query
|
3
|
+
#
|
4
|
+
# A query component that holds information about pagination. Unlike other
|
5
|
+
# query components, this one is mutable, because the query itself holds a
|
6
|
+
# reference to it and updates it if pagination is changed.
|
7
|
+
#
|
8
|
+
class Pagination #:nodoc:
|
9
|
+
attr_reader :page, :per_page
|
10
|
+
|
11
|
+
def initialize(page = nil, per_page = nil)
|
12
|
+
self.page, self.per_page = page, per_page
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_params
|
16
|
+
{ :start => start, :rows => rows }
|
17
|
+
end
|
18
|
+
|
19
|
+
def page=(page)
|
20
|
+
@page = page.to_i if page
|
21
|
+
end
|
22
|
+
|
23
|
+
def per_page=(per_page)
|
24
|
+
@per_page = per_page.to_i if per_page
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def start
|
30
|
+
(@page - 1) * @per_page
|
31
|
+
end
|
32
|
+
|
33
|
+
def rows
|
34
|
+
@per_page
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
module Sunspot
|
2
|
+
module Query
|
3
|
+
class Query
|
4
|
+
attr_accessor :scope, :fulltext, :parameter_adjustment
|
5
|
+
|
6
|
+
def initialize(types)
|
7
|
+
@scope = Scope.new
|
8
|
+
@sort = SortComposite.new
|
9
|
+
@components = []
|
10
|
+
if types.length == 1
|
11
|
+
@scope.add_restriction(TypeField.instance, Restriction::EqualTo, types.first)
|
12
|
+
else
|
13
|
+
@scope.add_restriction(TypeField.instance, Restriction::AnyOf, types)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def set_fulltext(keywords)
|
18
|
+
@fulltext = Dismax.new(keywords)
|
19
|
+
end
|
20
|
+
|
21
|
+
def set_solr_parameter_adjustment( block )
|
22
|
+
@parameter_adjustment = block
|
23
|
+
end
|
24
|
+
|
25
|
+
def add_location_restriction(coordinates, radius)
|
26
|
+
@local = Local.new(coordinates, radius)
|
27
|
+
end
|
28
|
+
|
29
|
+
def add_sort(sort)
|
30
|
+
@sort << sort
|
31
|
+
end
|
32
|
+
|
33
|
+
def add_field_facet(facet)
|
34
|
+
@components << facet
|
35
|
+
facet
|
36
|
+
end
|
37
|
+
|
38
|
+
def add_query_facet(facet)
|
39
|
+
@components << facet
|
40
|
+
facet
|
41
|
+
end
|
42
|
+
|
43
|
+
def paginate(page, per_page)
|
44
|
+
if @pagination
|
45
|
+
@pagination.page = page
|
46
|
+
@pagination.per_page = per_page
|
47
|
+
else
|
48
|
+
@pagination = Pagination.new(page, per_page)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def to_params
|
53
|
+
params =
|
54
|
+
if @local
|
55
|
+
if @fulltext
|
56
|
+
raise(
|
57
|
+
IllegalSearchError,
|
58
|
+
"Can't perform search with both fulltext and geographical components due to LocalSolr limitations"
|
59
|
+
)
|
60
|
+
end
|
61
|
+
{ :q => @scope.to_boolean_phrase }
|
62
|
+
else
|
63
|
+
@scope.to_params
|
64
|
+
end
|
65
|
+
Sunspot::Util.deep_merge!(params, @fulltext.to_params) if @fulltext
|
66
|
+
Sunspot::Util.deep_merge!(params, @sort.to_params)
|
67
|
+
Sunspot::Util.deep_merge!(params, @pagination.to_params) if @pagination
|
68
|
+
Sunspot::Util.deep_merge!(params, @local.to_params) if @local
|
69
|
+
@components.each do |component|
|
70
|
+
Sunspot::Util.deep_merge!(params, component.to_params)
|
71
|
+
end
|
72
|
+
@parameter_adjustment.call(params) if @parameter_adjustment
|
73
|
+
params[:q] ||= '*:*'
|
74
|
+
params
|
75
|
+
end
|
76
|
+
|
77
|
+
def page
|
78
|
+
@pagination.page if @pagination
|
79
|
+
end
|
80
|
+
|
81
|
+
def per_page
|
82
|
+
@pagination.per_page if @pagination
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,254 @@
|
|
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 RSolr::Char
|
41
|
+
|
42
|
+
def initialize(field, value, negated = false)
|
43
|
+
@field, @value, @negated = field, value, negated
|
44
|
+
end
|
45
|
+
|
46
|
+
#
|
47
|
+
# A hash representing this restriction in solr-ruby's parameter format.
|
48
|
+
# All restriction implementations must respond to this method; however,
|
49
|
+
# the base implementation delegates to the #to_positive_boolean_phrase method, so
|
50
|
+
# subclasses may (and probably should) choose to implement that method
|
51
|
+
# instead.
|
52
|
+
#
|
53
|
+
# ==== Returns
|
54
|
+
#
|
55
|
+
# Hash:: Representation of this restriction as solr-ruby parameters
|
56
|
+
#
|
57
|
+
def to_params
|
58
|
+
{ :fq => [to_boolean_phrase] }
|
59
|
+
end
|
60
|
+
|
61
|
+
#
|
62
|
+
# Return the boolean phrase associated with this restriction object.
|
63
|
+
# Differentiates between positive and negated boolean phrases depending
|
64
|
+
# on whether this restriction is negated.
|
65
|
+
#
|
66
|
+
def to_boolean_phrase
|
67
|
+
unless negated?
|
68
|
+
to_positive_boolean_phrase
|
69
|
+
else
|
70
|
+
to_negated_boolean_phrase
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
#
|
75
|
+
# Boolean phrase representing this restriction in the positive. Subclasses
|
76
|
+
# may choose to implement this method rather than #to_params; however,
|
77
|
+
# this method delegates to the abstract #to_solr_conditional method, which
|
78
|
+
# in most cases will be what subclasses will want to implement.
|
79
|
+
# #to_solr_conditional contains the boolean phrase representing the
|
80
|
+
# condition but leaves out the field name (see built-in implementations
|
81
|
+
# for examples)
|
82
|
+
#
|
83
|
+
# ==== Returns
|
84
|
+
#
|
85
|
+
# String:: Boolean phrase for restriction in the positive
|
86
|
+
#
|
87
|
+
def to_positive_boolean_phrase
|
88
|
+
"#{escape(@field.indexed_name)}:#{to_solr_conditional}"
|
89
|
+
end
|
90
|
+
|
91
|
+
#
|
92
|
+
# Boolean phrase representing this restriction in the negated. Subclasses
|
93
|
+
# may choose to implement this method, but it is not necessary, as the
|
94
|
+
# base implementation delegates to #to_positive_boolean_phrase.
|
95
|
+
#
|
96
|
+
# ==== Returns
|
97
|
+
#
|
98
|
+
# String:: Boolean phrase for restriction in the negated
|
99
|
+
#
|
100
|
+
def to_negated_boolean_phrase
|
101
|
+
"-#{to_positive_boolean_phrase}"
|
102
|
+
end
|
103
|
+
|
104
|
+
#
|
105
|
+
# Whether this restriction should be negated from its original meaning
|
106
|
+
#
|
107
|
+
def negated? #:nodoc:
|
108
|
+
!!@negated
|
109
|
+
end
|
110
|
+
|
111
|
+
#
|
112
|
+
# Return a new restriction that is the negated version of this one. It
|
113
|
+
# is used by disjunction denormalization.
|
114
|
+
#
|
115
|
+
def negate
|
116
|
+
self.class.new(@field, @value, !@negated)
|
117
|
+
end
|
118
|
+
|
119
|
+
protected
|
120
|
+
|
121
|
+
#
|
122
|
+
# Return escaped Solr API representation of given value
|
123
|
+
#
|
124
|
+
# ==== Parameters
|
125
|
+
#
|
126
|
+
# value<Object>::
|
127
|
+
# value to convert to Solr representation (default: @value)
|
128
|
+
#
|
129
|
+
# ==== Returns
|
130
|
+
#
|
131
|
+
# String:: Solr API representation of given value
|
132
|
+
#
|
133
|
+
def solr_value(value = @value)
|
134
|
+
solr_value = escape(@field.to_indexed(value))
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
#
|
139
|
+
# Results must have field with value equal to given value. If the value
|
140
|
+
# is nil, results must have no value for the given field.
|
141
|
+
#
|
142
|
+
class EqualTo < Base
|
143
|
+
def to_positive_boolean_phrase
|
144
|
+
unless @value.nil?
|
145
|
+
super
|
146
|
+
else
|
147
|
+
"#{escape(@field.indexed_name)}:[* TO *]"
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
def negated?
|
152
|
+
if @value.nil?
|
153
|
+
!super
|
154
|
+
else
|
155
|
+
super
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
private
|
160
|
+
|
161
|
+
def to_solr_conditional
|
162
|
+
"#{solr_value}"
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
#
|
167
|
+
# Results must have field with value less than given value
|
168
|
+
#
|
169
|
+
class LessThan < Base
|
170
|
+
private
|
171
|
+
|
172
|
+
def solr_value(value = @value)
|
173
|
+
solr_value = super
|
174
|
+
solr_value = "\"#{solr_value}\"" if solr_value.index(' ')
|
175
|
+
solr_value
|
176
|
+
end
|
177
|
+
|
178
|
+
def to_solr_conditional
|
179
|
+
"[* TO #{solr_value}]"
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
#
|
184
|
+
# Results must have field with value greater than given value
|
185
|
+
#
|
186
|
+
class GreaterThan < Base
|
187
|
+
private
|
188
|
+
|
189
|
+
def solr_value(value = @value)
|
190
|
+
solr_value = super
|
191
|
+
solr_value = "\"#{solr_value}\"" if solr_value.index(' ')
|
192
|
+
solr_value
|
193
|
+
end
|
194
|
+
|
195
|
+
def to_solr_conditional
|
196
|
+
"[#{solr_value} TO *]"
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
#
|
201
|
+
# Results must have field with value in given range
|
202
|
+
#
|
203
|
+
class Between < Base
|
204
|
+
private
|
205
|
+
|
206
|
+
def solr_value(value = @value)
|
207
|
+
solr_value = super
|
208
|
+
solr_value = "\"#{solr_value}\"" if solr_value.index(' ')
|
209
|
+
solr_value
|
210
|
+
end
|
211
|
+
|
212
|
+
def to_solr_conditional
|
213
|
+
first, last = [@value.first, @value.last].sort
|
214
|
+
"[#{solr_value(first)} TO #{solr_value(last)}]"
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
#
|
219
|
+
# Results must have field with value included in given collection
|
220
|
+
#
|
221
|
+
class AnyOf < Base
|
222
|
+
private
|
223
|
+
|
224
|
+
def to_solr_conditional
|
225
|
+
"(#{@value.map { |v| solr_value v } * ' OR '})"
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
#
|
230
|
+
# Results must have field with values matching all values in given
|
231
|
+
# collection (only makes sense for fields with multiple values)
|
232
|
+
#
|
233
|
+
class AllOf < Base
|
234
|
+
private
|
235
|
+
|
236
|
+
def to_solr_conditional
|
237
|
+
"(#{@value.map { |v| solr_value v } * ' AND '})"
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
#
|
242
|
+
# Results must have a field with a value that begins with the argument.
|
243
|
+
# Most useful for strings, but in theory will work with anything.
|
244
|
+
#
|
245
|
+
class StartingWith < Base
|
246
|
+
private
|
247
|
+
|
248
|
+
def to_solr_conditional
|
249
|
+
"#{solr_value(@value)}*"
|
250
|
+
end
|
251
|
+
end
|
252
|
+
end
|
253
|
+
end
|
254
|
+
end
|
@@ -0,0 +1,105 @@
|
|
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
|
+
|
94
|
+
#
|
95
|
+
# A DistanceSort sorts by distance from the origin coordinates of a
|
96
|
+
# geographical distance search.
|
97
|
+
#
|
98
|
+
class DistanceSort < Abstract
|
99
|
+
def to_param
|
100
|
+
"geo_distance #{direction_for_solr}"
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|