benjaminkrause-sunspot 0.9.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. data/History.txt +107 -0
  2. data/LICENSE +18 -0
  3. data/README.rdoc +159 -0
  4. data/Rakefile +9 -0
  5. data/TODO +11 -0
  6. data/VERSION.yml +4 -0
  7. data/bin/sunspot-configure-solr +46 -0
  8. data/bin/sunspot-solr +86 -0
  9. data/lib/light_config.rb +40 -0
  10. data/lib/sunspot/adapters.rb +265 -0
  11. data/lib/sunspot/composite_setup.rb +184 -0
  12. data/lib/sunspot/configuration.rb +49 -0
  13. data/lib/sunspot/data_extractor.rb +50 -0
  14. data/lib/sunspot/dsl/field_query.rb +77 -0
  15. data/lib/sunspot/dsl/fields.rb +95 -0
  16. data/lib/sunspot/dsl/fulltext.rb +106 -0
  17. data/lib/sunspot/dsl/query.rb +107 -0
  18. data/lib/sunspot/dsl/query_facet.rb +31 -0
  19. data/lib/sunspot/dsl/restriction.rb +25 -0
  20. data/lib/sunspot/dsl/scope.rb +193 -0
  21. data/lib/sunspot/dsl/search.rb +30 -0
  22. data/lib/sunspot/dsl.rb +4 -0
  23. data/lib/sunspot/facet.rb +24 -0
  24. data/lib/sunspot/facet_data.rb +152 -0
  25. data/lib/sunspot/facet_row.rb +12 -0
  26. data/lib/sunspot/field.rb +148 -0
  27. data/lib/sunspot/field_factory.rb +141 -0
  28. data/lib/sunspot/indexer.rb +129 -0
  29. data/lib/sunspot/instantiated_facet.rb +45 -0
  30. data/lib/sunspot/instantiated_facet_row.rb +27 -0
  31. data/lib/sunspot/query/base_query.rb +55 -0
  32. data/lib/sunspot/query/boost_query.rb +20 -0
  33. data/lib/sunspot/query/connective.rb +148 -0
  34. data/lib/sunspot/query/dynamic_query.rb +61 -0
  35. data/lib/sunspot/query/field_facet.rb +129 -0
  36. data/lib/sunspot/query/field_query.rb +69 -0
  37. data/lib/sunspot/query/fulltext_base_query.rb +86 -0
  38. data/lib/sunspot/query/highlighting.rb +36 -0
  39. data/lib/sunspot/query/local.rb +24 -0
  40. data/lib/sunspot/query/pagination.rb +39 -0
  41. data/lib/sunspot/query/query_facet.rb +78 -0
  42. data/lib/sunspot/query/query_facet_row.rb +19 -0
  43. data/lib/sunspot/query/query_field_facet.rb +20 -0
  44. data/lib/sunspot/query/restriction.rb +272 -0
  45. data/lib/sunspot/query/scope.rb +185 -0
  46. data/lib/sunspot/query/sort.rb +105 -0
  47. data/lib/sunspot/query/sort_composite.rb +33 -0
  48. data/lib/sunspot/query/text_field_boost.rb +15 -0
  49. data/lib/sunspot/query.rb +108 -0
  50. data/lib/sunspot/schema.rb +147 -0
  51. data/lib/sunspot/search/highlight.rb +38 -0
  52. data/lib/sunspot/search/hit.rb +113 -0
  53. data/lib/sunspot/search.rb +240 -0
  54. data/lib/sunspot/session.rb +206 -0
  55. data/lib/sunspot/setup.rb +312 -0
  56. data/lib/sunspot/text_field_setup.rb +29 -0
  57. data/lib/sunspot/type.rb +200 -0
  58. data/lib/sunspot/util.rb +190 -0
  59. data/lib/sunspot.rb +459 -0
  60. data/solr/etc/jetty.xml +212 -0
  61. data/solr/etc/webdefault.xml +379 -0
  62. data/solr/lib/jetty-6.1.3.jar +0 -0
  63. data/solr/lib/jetty-util-6.1.3.jar +0 -0
  64. data/solr/lib/jsp-2.1/ant-1.6.5.jar +0 -0
  65. data/solr/lib/jsp-2.1/core-3.1.1.jar +0 -0
  66. data/solr/lib/jsp-2.1/jsp-2.1.jar +0 -0
  67. data/solr/lib/jsp-2.1/jsp-api-2.1.jar +0 -0
  68. data/solr/lib/servlet-api-2.5-6.1.3.jar +0 -0
  69. data/solr/solr/conf/elevate.xml +36 -0
  70. data/solr/solr/conf/protwords.txt +21 -0
  71. data/solr/solr/conf/schema.xml +64 -0
  72. data/solr/solr/conf/solrconfig.xml +726 -0
  73. data/solr/solr/conf/stopwords.txt +57 -0
  74. data/solr/solr/conf/synonyms.txt +31 -0
  75. data/solr/start.jar +0 -0
  76. data/solr/webapps/solr.war +0 -0
  77. data/spec/api/adapters_spec.rb +33 -0
  78. data/spec/api/indexer/attributes_spec.rb +100 -0
  79. data/spec/api/indexer/batch_spec.rb +46 -0
  80. data/spec/api/indexer/dynamic_fields_spec.rb +33 -0
  81. data/spec/api/indexer/fixed_fields_spec.rb +57 -0
  82. data/spec/api/indexer/fulltext_spec.rb +43 -0
  83. data/spec/api/indexer/removal_spec.rb +46 -0
  84. data/spec/api/indexer/spec_helper.rb +1 -0
  85. data/spec/api/indexer_spec.rb +4 -0
  86. data/spec/api/query/connectives_spec.rb +161 -0
  87. data/spec/api/query/dsl_spec.rb +12 -0
  88. data/spec/api/query/dynamic_fields_spec.rb +148 -0
  89. data/spec/api/query/faceting_spec.rb +272 -0
  90. data/spec/api/query/fulltext_spec.rb +152 -0
  91. data/spec/api/query/highlighting_spec.rb +82 -0
  92. data/spec/api/query/local_spec.rb +37 -0
  93. data/spec/api/query/ordering_pagination_spec.rb +95 -0
  94. data/spec/api/query/scope_spec.rb +253 -0
  95. data/spec/api/query/spec_helper.rb +1 -0
  96. data/spec/api/query/text_field_scoping_spec.rb +30 -0
  97. data/spec/api/query/types_spec.rb +20 -0
  98. data/spec/api/search/dynamic_fields_spec.rb +27 -0
  99. data/spec/api/search/faceting_spec.rb +206 -0
  100. data/spec/api/search/highlighting_spec.rb +65 -0
  101. data/spec/api/search/hits_spec.rb +62 -0
  102. data/spec/api/search/results_spec.rb +52 -0
  103. data/spec/api/search/search_spec.rb +11 -0
  104. data/spec/api/search/spec_helper.rb +1 -0
  105. data/spec/api/session_spec.rb +157 -0
  106. data/spec/api/spec_helper.rb +1 -0
  107. data/spec/api/sunspot_spec.rb +18 -0
  108. data/spec/helpers/indexer_helper.rb +29 -0
  109. data/spec/helpers/query_helper.rb +13 -0
  110. data/spec/helpers/search_helper.rb +78 -0
  111. data/spec/integration/dynamic_fields_spec.rb +55 -0
  112. data/spec/integration/faceting_spec.rb +169 -0
  113. data/spec/integration/highlighting_spec.rb +22 -0
  114. data/spec/integration/keyword_search_spec.rb +148 -0
  115. data/spec/integration/local_search_spec.rb +47 -0
  116. data/spec/integration/scoped_search_spec.rb +303 -0
  117. data/spec/integration/spec_helper.rb +1 -0
  118. data/spec/integration/stored_fields_spec.rb +10 -0
  119. data/spec/integration/test_pagination.rb +32 -0
  120. data/spec/mocks/adapters.rb +32 -0
  121. data/spec/mocks/blog.rb +3 -0
  122. data/spec/mocks/comment.rb +19 -0
  123. data/spec/mocks/connection.rb +84 -0
  124. data/spec/mocks/mock_adapter.rb +30 -0
  125. data/spec/mocks/mock_record.rb +48 -0
  126. data/spec/mocks/photo.rb +8 -0
  127. data/spec/mocks/post.rb +75 -0
  128. data/spec/mocks/super_class.rb +2 -0
  129. data/spec/mocks/user.rb +8 -0
  130. data/spec/spec_helper.rb +60 -0
  131. data/tasks/gemspec.rake +35 -0
  132. data/tasks/rcov.rake +28 -0
  133. data/tasks/rdoc.rake +22 -0
  134. data/tasks/schema.rake +19 -0
  135. data/tasks/spec.rake +24 -0
  136. data/tasks/todo.rake +4 -0
  137. data/templates/schema.xml.erb +36 -0
  138. metadata +312 -0
@@ -0,0 +1,19 @@
1
+ module Sunspot
2
+ module Query
3
+ #
4
+ # QueryFacetRow objects encapsulate restrictions for a particular
5
+ # QueryFacet. They also contain a label attribute, which is used as the
6
+ # value for the search result's corresponding facet row object.
7
+ #
8
+ # See Query::Scope for the API provided.
9
+ #
10
+ class QueryFacetRow < Connective::Conjunction #:nodoc:
11
+ attr_reader :label
12
+
13
+ def initialize(label, setup)
14
+ super(setup)
15
+ @label = label
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ module Sunspot
2
+ module Query
3
+ #
4
+ # QueryFieldFacets are used for the "restricted field facet" feature, which
5
+ # allows an :only parameter for field facets, specifying a set of values in
6
+ # which the searcher is interested. Since Solr does not support this feature
7
+ # directly in field facets, build query facets that replicate field facet
8
+ # behavior.
9
+ #
10
+ class QueryFieldFacet < QueryFacet #:nodoc:
11
+ def initialize(field, values, options)
12
+ super(field.name, options)
13
+ @field = field
14
+ values.each do |value|
15
+ add_row(value).add_component(Restriction::EqualTo.new(field, value))
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,272 @@
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 SameAs) #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
+
253
+ #
254
+ # Result must be the exact instance given (only useful when negated).
255
+ #
256
+ class SameAs < Base
257
+ def initialize(object, negated = false)
258
+ @object, @negated = object, negated
259
+ end
260
+
261
+ def to_positive_boolean_phrase
262
+ adapter = Adapters::InstanceAdapter.adapt(@object)
263
+ "id:#{escape(adapter.index_id)}"
264
+ end
265
+
266
+ def negate
267
+ SameAs.new(@object, !negated?)
268
+ end
269
+ end
270
+ end
271
+ end
272
+ end
@@ -0,0 +1,185 @@
1
+ module Sunspot
2
+ module Query
3
+ #
4
+ # The Scope class encapsulates a set of restrictions that scope search
5
+ # results (as well as query facets rows). This class's API is exposed by
6
+ # Query::Query and Query::QueryFacetRow.
7
+ #
8
+ class Scope #:nodoc:
9
+ def initialize(setup)
10
+ @setup = setup
11
+ @components = []
12
+ end
13
+
14
+ #
15
+ # Add a restriction to the query.
16
+ #
17
+ # ==== Parameters
18
+ #
19
+ # field_name<Symbol>:: Name of the field to which the restriction applies
20
+ # restriction_type<Class,Symbol>::
21
+ # Subclass of Sunspot::Query::Restriction::Base, or snake_cased name as symbol
22
+ # (e.g., +:equal_to+)
23
+ # value<Object>::
24
+ # Value against which the restriction applies (e.g. less_than(2) has a
25
+ # value of 2)
26
+ # negated::
27
+ # Whether this restriction should be negated (use add_negated_restriction)
28
+ #
29
+ def add_restriction(field_name, restriction_type, value, negated = false)
30
+ if restriction_type.is_a?(Symbol)
31
+ restriction_type = Restriction[restriction_type]
32
+ end
33
+ add_component(
34
+ restriction = restriction_type.new(
35
+ build_field(field_name), value, negated
36
+ )
37
+ )
38
+ restriction
39
+ end
40
+
41
+ #
42
+ # Add a negated restriction to the query. The restriction will be taken as
43
+ # the opposite of its usual meaning (e.g., an :equal_to restriction will
44
+ # be "not equal to".
45
+ #
46
+ # ==== Parameters
47
+ #
48
+ # field_name<Symbol>:: Name of the field to which the restriction applies
49
+ # restriction_type<Class>::
50
+ # Subclass of Sunspot::Query::Restriction::Base to instantiate
51
+ # value<Object>::
52
+ # Value against which the restriction applies (e.g. less_than(2) has a
53
+ # value of 2)
54
+ #
55
+ def add_negated_restriction(field_name, restriction_type, value)
56
+ add_restriction(field_name, restriction_type, value, true)
57
+ end
58
+
59
+ #
60
+ # Add a disjunction to the scope. The disjunction can then take a set of
61
+ # restrictions, which are combined with OR semantics.
62
+ #
63
+ # ==== Returns
64
+ #
65
+ # Connective::Disjunction:: New disjunction
66
+ #
67
+ def add_disjunction
68
+ add_component(disjunction = Connective::Disjunction.new(@setup))
69
+ disjunction
70
+ end
71
+
72
+ #
73
+ # Add a conjunction to the scope. In most cases, this will simply return
74
+ # the Scope object itself, since scopes by default combine their
75
+ # restrictions with OR semantics. The Connective::Disjunction class
76
+ # overrides this method to return a Connective::Conjunction.
77
+ #
78
+ # ==== Returns
79
+ #
80
+ # Scope:: Self or another scope with conjunctive semantics.
81
+ #
82
+ def add_conjunction
83
+ self
84
+ end
85
+
86
+ #
87
+ # Exclude a particular instance from the search results
88
+ #
89
+ # ==== Parameters
90
+ #
91
+ # instance<Object>:: instance to exclude from results
92
+ #
93
+ def exclude_instance(instance)
94
+ add_component(Restriction::SameAs.new(instance, true))
95
+ end
96
+
97
+ #
98
+ # Generate a DynamicQuery instance for the given base name.
99
+ # This gives you access to a subset of the Query API but the operations
100
+ # apply to dynamic fields inside the dynamic field definition specified
101
+ # by +base_name+.
102
+ #
103
+ # ==== Parameters
104
+ #
105
+ # base_name<Symbol>::
106
+ # Base name of the dynamic field definition to use in the dynamic query
107
+ # operations
108
+ #
109
+ # ==== Returns
110
+ #
111
+ # DynamicQuery::
112
+ # Instance providing dynamic query functionality for the given field
113
+ # definitions.
114
+ #
115
+ def dynamic_query(base_name)
116
+ DynamicQuery.new(@setup.dynamic_field_factory(base_name), self)
117
+ end
118
+
119
+ #
120
+ # Determine which restriction type to add based on the type of the value.
121
+ # Used to interpret query conditions passed as a hash, as well as the
122
+ # short-form DSL::Scope#with method.
123
+ #
124
+ # ==== Parameters
125
+ #
126
+ # field_name<Symbol>:: Name of the field on which to apply the restriction
127
+ # value<Object,Array,Range>:: Value to which to apply to the restriction
128
+ #--
129
+ # negated<Boolean>:: Whether to negate the restriction.
130
+ #
131
+ def add_shorthand_restriction(field_name, value, negated = false) #:nodoc:
132
+ restriction_type =
133
+ case value
134
+ when Range
135
+ Restriction::Between
136
+ when Array
137
+ Restriction::AnyOf
138
+ else
139
+ Restriction::EqualTo
140
+ end
141
+ add_restriction(field_name, restriction_type, value, negated)
142
+ end
143
+
144
+ #
145
+ # Add a negated shorthand restriction. See #add_shorthand_restriction
146
+ #
147
+ def add_negated_shorthand_restriction(field_name, value)
148
+ add_shorthand_restriction(field_name, value, true)
149
+ end
150
+
151
+ #
152
+ # Add a component to the connective. All components must implement the
153
+ # #to_boolean_phrase method.
154
+ #
155
+ def add_component(component) #:nodoc:
156
+ @components << component
157
+ end
158
+
159
+ #
160
+ # Representation of this query as solr parameters.
161
+ #
162
+ # ==== Returns
163
+ #
164
+ # Hash:: Representation of query in Solr form
165
+ #
166
+ def to_params #:nodoc:
167
+ params = {}
168
+ for component in @components
169
+ Util.deep_merge!(params, component.to_params)
170
+ end
171
+ params
172
+ end
173
+
174
+ private
175
+
176
+ #
177
+ # Build a field with the given field name. Subclasses may override this
178
+ # method.
179
+ #
180
+ def build_field(field_name)
181
+ @setup.field(field_name)
182
+ end
183
+ end
184
+ end
185
+ 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
@@ -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,15 @@
1
+ module Sunspot
2
+ module Query
3
+ class TextFieldBoost #:nodoc:
4
+ def initialize(field, boost = nil)
5
+ @field, @boost = field, boost
6
+ end
7
+
8
+ def to_boosted_field
9
+ boosted_field = @field.indexed_name
10
+ boosted_field.concat("^#{@boost}") if @boost
11
+ boosted_field
12
+ end
13
+ end
14
+ end
15
+ end