sunspot 0.9.8 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +32 -0
- data/README.rdoc +40 -3
- data/TODO +10 -8
- data/VERSION.yml +2 -2
- data/bin/sunspot-configure-solr +22 -28
- data/bin/sunspot-solr +50 -29
- data/lib/sunspot/adapters.rb +1 -1
- data/lib/sunspot/composite_setup.rb +13 -15
- data/lib/sunspot/configuration.rb +14 -0
- data/lib/sunspot/data_extractor.rb +3 -0
- data/lib/sunspot/dsl/field_query.rb +33 -6
- data/lib/sunspot/dsl/fields.rb +14 -1
- data/lib/sunspot/dsl/fulltext.rb +168 -0
- data/lib/sunspot/dsl/query.rb +82 -5
- data/lib/sunspot/dsl/query_facet.rb +3 -3
- data/lib/sunspot/dsl/restriction.rb +7 -7
- data/lib/sunspot/dsl/scope.rb +17 -10
- data/lib/sunspot/dsl/search.rb +2 -2
- data/lib/sunspot/dsl.rb +2 -1
- data/lib/sunspot/facet.rb +9 -1
- data/lib/sunspot/facet_data.rb +56 -7
- data/lib/sunspot/facet_row.rb +2 -0
- data/lib/sunspot/field.rb +50 -26
- data/lib/sunspot/field_factory.rb +15 -0
- data/lib/sunspot/indexer.rb +6 -0
- data/lib/sunspot/instantiated_facet.rb +6 -9
- data/lib/sunspot/instantiated_facet_row.rb +7 -2
- data/lib/sunspot/query/boost_query.rb +20 -0
- data/lib/sunspot/query/connective.rb +98 -35
- data/lib/sunspot/query/dismax.rb +69 -0
- data/lib/sunspot/query/field_facet.rb +1 -22
- data/lib/sunspot/query/fulltext_base_query.rb +47 -0
- data/lib/sunspot/query/highlighting.rb +43 -0
- data/lib/sunspot/query/local.rb +24 -0
- data/lib/sunspot/query/pagination.rb +3 -4
- data/lib/sunspot/query/query.rb +93 -0
- data/lib/sunspot/query/query_facet.rb +14 -9
- data/lib/sunspot/query/query_facet_row.rb +3 -3
- data/lib/sunspot/query/query_field_facet.rb +10 -3
- data/lib/sunspot/query/restriction.rb +36 -15
- data/lib/sunspot/query/scope.rb +3 -159
- data/lib/sunspot/query/sort.rb +84 -15
- data/lib/sunspot/query/text_field_boost.rb +15 -0
- data/lib/sunspot/query.rb +2 -188
- data/lib/sunspot/schema.rb +7 -25
- data/lib/sunspot/search/highlight.rb +38 -0
- data/lib/sunspot/search/hit.rb +50 -3
- data/lib/sunspot/search.rb +51 -32
- data/lib/sunspot/session.rb +32 -12
- data/lib/sunspot/setup.rb +47 -10
- data/lib/sunspot/text_field_setup.rb +29 -0
- data/lib/sunspot/type.rb +4 -4
- data/lib/sunspot/util.rb +27 -1
- data/lib/sunspot.rb +8 -17
- data/solr/solr/conf/schema.xml +54 -40
- data/solr/solr/conf/solrconfig.xml +30 -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/spec/api/indexer/attributes_spec.rb +100 -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 +1 -308
- data/spec/api/query/connectives_spec.rb +162 -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 +272 -0
- data/spec/api/query/fulltext_spec.rb +193 -0
- data/spec/api/query/highlighting_spec.rb +138 -0
- data/spec/api/query/local_spec.rb +54 -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 +206 -0
- data/spec/api/search/highlighting_spec.rb +65 -0
- data/spec/api/search/hits_spec.rb +62 -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/spec_helper.rb +1 -1
- 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/faceting_spec.rb +1 -1
- data/spec/integration/highlighting_spec.rb +22 -0
- data/spec/integration/keyword_search_spec.rb +65 -0
- data/spec/integration/local_search_spec.rb +56 -0
- data/spec/integration/scoped_search_spec.rb +15 -1
- data/spec/integration/spec_helper.rb +3 -3
- data/spec/mocks/connection.rb +14 -1
- data/spec/mocks/photo.rb +1 -1
- data/spec/mocks/post.rb +5 -3
- data/spec/mocks/super_class.rb +2 -0
- data/spec/spec_helper.rb +13 -0
- data/tasks/gemspec.rake +18 -7
- data/tasks/schema.rake +1 -1
- data/tasks/spec.rake +1 -1
- data/templates/schema.xml.erb +36 -0
- metadata +117 -48
- data/lib/sunspot/query/base_query.rb +0 -90
- data/lib/sunspot/query/dynamic_query.rb +0 -69
- data/lib/sunspot/query/field_query.rb +0 -63
- data/spec/api/build_search_spec.rb +0 -1017
- data/spec/api/query_spec.rb +0 -153
- data/spec/api/search_retrieval_spec.rb +0 -362
- data/templates/schema.xml.haml +0 -24
@@ -0,0 +1,93 @@
|
|
1
|
+
module Sunspot
|
2
|
+
module Query
|
3
|
+
class Query
|
4
|
+
attr_accessor :scope
|
5
|
+
attr_accessor :fulltext
|
6
|
+
|
7
|
+
def initialize(types)
|
8
|
+
@scope = Scope.new
|
9
|
+
@field_facets = []
|
10
|
+
@query_facets = {}
|
11
|
+
@sort = SortComposite.new
|
12
|
+
if types.length == 1
|
13
|
+
@scope.add_restriction(TypeField.instance, Restriction::EqualTo, types.first)
|
14
|
+
else
|
15
|
+
@scope.add_restriction(TypeField.instance, Restriction::AnyOf, types)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def set_fulltext(keywords)
|
20
|
+
@fulltext = Dismax.new(keywords)
|
21
|
+
end
|
22
|
+
|
23
|
+
def add_location_restriction(coordinates, radius)
|
24
|
+
@local = Local.new(coordinates, radius)
|
25
|
+
end
|
26
|
+
|
27
|
+
def add_field_facet(field, options = {})
|
28
|
+
facet = FieldFacet.build(field, options)
|
29
|
+
if facet.is_a?(QueryFacet)
|
30
|
+
@query_facets[field.name.to_sym] = facet
|
31
|
+
else
|
32
|
+
@field_facets << facet
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def add_query_facet(name, options = {})
|
37
|
+
@query_facets[name.to_sym] = QueryFacet.new(name, options)
|
38
|
+
end
|
39
|
+
|
40
|
+
def add_sort(sort)
|
41
|
+
@sort << sort
|
42
|
+
end
|
43
|
+
|
44
|
+
def paginate(page, per_page)
|
45
|
+
if @pagination
|
46
|
+
@pagination.page = page
|
47
|
+
@pagination.per_page = per_page
|
48
|
+
else
|
49
|
+
@pagination = Pagination.new(page, per_page)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def to_params
|
54
|
+
params =
|
55
|
+
if @local
|
56
|
+
if @fulltext
|
57
|
+
raise(
|
58
|
+
IllegalSearchError,
|
59
|
+
"Can't perform search with both fulltext and geographical components due to LocalSolr limitations"
|
60
|
+
)
|
61
|
+
end
|
62
|
+
{ :q => @scope.to_boolean_phrase }
|
63
|
+
else
|
64
|
+
@scope.to_params
|
65
|
+
end
|
66
|
+
Sunspot::Util.deep_merge!(params, @fulltext.to_params) if @fulltext
|
67
|
+
@field_facets.each do |facet|
|
68
|
+
Sunspot::Util.deep_merge!(params, facet.to_params)
|
69
|
+
end
|
70
|
+
@query_facets.values.each do |facet|
|
71
|
+
Sunspot::Util.deep_merge!(params, facet.to_params)
|
72
|
+
end
|
73
|
+
Sunspot::Util.deep_merge!(params, @sort.to_params)
|
74
|
+
Sunspot::Util.deep_merge!(params, @pagination.to_params) if @pagination
|
75
|
+
Sunspot::Util.deep_merge!(params, @local.to_params) if @local
|
76
|
+
params[:q] ||= '*:*'
|
77
|
+
params
|
78
|
+
end
|
79
|
+
|
80
|
+
def page
|
81
|
+
@pagination.page if @pagination
|
82
|
+
end
|
83
|
+
|
84
|
+
def per_page
|
85
|
+
@pagination.per_page if @pagination
|
86
|
+
end
|
87
|
+
|
88
|
+
def query_facet(name)
|
89
|
+
@query_facets[name] if @query_facets
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -12,13 +12,13 @@ module Sunspot
|
|
12
12
|
# query facet rows, instead returning each requested row individually, keyed
|
13
13
|
# by the boolean phrase used in the facet query.
|
14
14
|
#
|
15
|
-
class QueryFacet
|
15
|
+
class QueryFacet #:nodoc:
|
16
16
|
attr_reader :name #:nodoc:
|
17
17
|
attr_reader :field #:nodoc:
|
18
|
+
attr_reader :options #:nodoc:
|
18
19
|
|
19
|
-
def initialize(name, setup = nil) #:nodoc:
|
20
|
-
@name = name
|
21
|
-
@setup = setup
|
20
|
+
def initialize(name, options, setup = nil) #:nodoc:
|
21
|
+
@name, @options, @setup = name, options, setup
|
22
22
|
@components = []
|
23
23
|
end
|
24
24
|
|
@@ -51,11 +51,16 @@ module Sunspot
|
|
51
51
|
#
|
52
52
|
def to_params #:nodoc:
|
53
53
|
components = @components.map { |component| component.to_boolean_phrase }
|
54
|
-
components
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
54
|
+
components.compact!
|
55
|
+
if components.empty?
|
56
|
+
{}
|
57
|
+
else
|
58
|
+
components = components.first if components.length == 1
|
59
|
+
{
|
60
|
+
:facet => 'true',
|
61
|
+
:"facet.query" => components
|
62
|
+
}
|
63
|
+
end
|
59
64
|
end
|
60
65
|
|
61
66
|
#
|
@@ -7,10 +7,10 @@ module Sunspot
|
|
7
7
|
#
|
8
8
|
# See Query::Scope for the API provided.
|
9
9
|
#
|
10
|
-
class QueryFacetRow < Connective::Conjunction
|
11
|
-
attr_reader :label
|
10
|
+
class QueryFacetRow < Connective::Conjunction #:nodoc:
|
11
|
+
attr_reader :label
|
12
12
|
|
13
|
-
def initialize(label, setup)
|
13
|
+
def initialize(label, setup)
|
14
14
|
super(setup)
|
15
15
|
@label = label
|
16
16
|
end
|
@@ -1,8 +1,15 @@
|
|
1
1
|
module Sunspot
|
2
2
|
module Query
|
3
|
-
|
4
|
-
|
5
|
-
|
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)
|
6
13
|
@field = field
|
7
14
|
values.each do |value|
|
8
15
|
add_row(value).add_component(Restriction::EqualTo.new(field, value))
|
@@ -11,9 +11,13 @@ module Sunspot
|
|
11
11
|
# Array:: Collection of restriction class names
|
12
12
|
#
|
13
13
|
def names
|
14
|
-
constants - %w(Base
|
14
|
+
constants - %w(Base) #XXX this seems ugly
|
15
15
|
end
|
16
16
|
|
17
|
+
#
|
18
|
+
# Convenience method to access a restriction class by an underscored
|
19
|
+
# symbol or string
|
20
|
+
#
|
17
21
|
def [](restriction_name)
|
18
22
|
@types ||= {}
|
19
23
|
@types[restriction_name.to_sym] ||= const_get(Sunspot::Util.camel_case(restriction_name.to_s))
|
@@ -104,6 +108,10 @@ module Sunspot
|
|
104
108
|
!!@negated
|
105
109
|
end
|
106
110
|
|
111
|
+
#
|
112
|
+
# Return a new restriction that is the negated version of this one. It
|
113
|
+
# is used by disjunction denormalization.
|
114
|
+
#
|
107
115
|
def negate
|
108
116
|
self.class.new(@field, @value, !@negated)
|
109
117
|
end
|
@@ -123,7 +131,7 @@ module Sunspot
|
|
123
131
|
# String:: Solr API representation of given value
|
124
132
|
#
|
125
133
|
def solr_value(value = @value)
|
126
|
-
escape(@field.to_indexed(value))
|
134
|
+
solr_value = escape(@field.to_indexed(value))
|
127
135
|
end
|
128
136
|
end
|
129
137
|
|
@@ -161,6 +169,12 @@ module Sunspot
|
|
161
169
|
class LessThan < Base
|
162
170
|
private
|
163
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
|
+
|
164
178
|
def to_solr_conditional
|
165
179
|
"[* TO #{solr_value}]"
|
166
180
|
end
|
@@ -172,6 +186,12 @@ module Sunspot
|
|
172
186
|
class GreaterThan < Base
|
173
187
|
private
|
174
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
|
+
|
175
195
|
def to_solr_conditional
|
176
196
|
"[#{solr_value} TO *]"
|
177
197
|
end
|
@@ -183,8 +203,15 @@ module Sunspot
|
|
183
203
|
class Between < Base
|
184
204
|
private
|
185
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
|
+
|
186
212
|
def to_solr_conditional
|
187
|
-
|
213
|
+
first, last = [@value.first, @value.last].sort
|
214
|
+
"[#{solr_value(first)} TO #{solr_value(last)}]"
|
188
215
|
end
|
189
216
|
end
|
190
217
|
|
@@ -212,20 +239,14 @@ module Sunspot
|
|
212
239
|
end
|
213
240
|
|
214
241
|
#
|
215
|
-
#
|
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.
|
216
244
|
#
|
217
|
-
class
|
218
|
-
|
219
|
-
@object, @negated = object, negated
|
220
|
-
end
|
221
|
-
|
222
|
-
def to_positive_boolean_phrase
|
223
|
-
adapter = Adapters::InstanceAdapter.adapt(@object)
|
224
|
-
"id:#{escape(adapter.index_id)}"
|
225
|
-
end
|
245
|
+
class StartingWith < Base
|
246
|
+
private
|
226
247
|
|
227
|
-
def
|
228
|
-
|
248
|
+
def to_solr_conditional
|
249
|
+
"#{solr_value(@value)}*"
|
229
250
|
end
|
230
251
|
end
|
231
252
|
end
|
data/lib/sunspot/query/scope.rb
CHANGED
@@ -1,164 +1,8 @@
|
|
1
1
|
module Sunspot
|
2
2
|
module Query
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
# Query::Query and Query::QueryFacetRow.
|
7
|
-
#
|
8
|
-
class Scope
|
9
|
-
#
|
10
|
-
# Add a restriction to the query.
|
11
|
-
#
|
12
|
-
# ==== Parameters
|
13
|
-
#
|
14
|
-
# field_name<Symbol>:: Name of the field to which the restriction applies
|
15
|
-
# restriction_type<Class,Symbol>::
|
16
|
-
# Subclass of Sunspot::Query::Restriction::Base, or snake_cased name as symbol
|
17
|
-
# (e.g., +:equal_to+)
|
18
|
-
# value<Object>::
|
19
|
-
# Value against which the restriction applies (e.g. less_than(2) has a
|
20
|
-
# value of 2)
|
21
|
-
# negated::
|
22
|
-
# Whether this restriction should be negated (use add_negated_restriction)
|
23
|
-
#
|
24
|
-
def add_restriction(field_name, restriction_type, value, negated = false)
|
25
|
-
if restriction_type.is_a?(Symbol)
|
26
|
-
restriction_type = Restriction[restriction_type]
|
27
|
-
end
|
28
|
-
add_component(
|
29
|
-
restriction = restriction_type.new(
|
30
|
-
build_field(field_name), value, negated
|
31
|
-
)
|
32
|
-
)
|
33
|
-
restriction
|
34
|
-
end
|
35
|
-
|
36
|
-
#
|
37
|
-
# Add a negated restriction to the query. The restriction will be taken as
|
38
|
-
# the opposite of its usual meaning (e.g., an :equal_to restriction will
|
39
|
-
# be "not equal to".
|
40
|
-
#
|
41
|
-
# ==== Parameters
|
42
|
-
#
|
43
|
-
# field_name<Symbol>:: Name of the field to which the restriction applies
|
44
|
-
# restriction_type<Class>::
|
45
|
-
# Subclass of Sunspot::Query::Restriction::Base to instantiate
|
46
|
-
# value<Object>::
|
47
|
-
# Value against which the restriction applies (e.g. less_than(2) has a
|
48
|
-
# value of 2)
|
49
|
-
#
|
50
|
-
def add_negated_restriction(field_name, restriction_type, value)
|
51
|
-
add_restriction(field_name, restriction_type, value, true)
|
52
|
-
end
|
53
|
-
|
54
|
-
#
|
55
|
-
# Add a disjunction to the scope. The disjunction can then take a set of
|
56
|
-
# restrictions, which are combined with OR semantics.
|
57
|
-
#
|
58
|
-
# ==== Returns
|
59
|
-
#
|
60
|
-
# Connective::Disjunction:: New disjunction
|
61
|
-
#
|
62
|
-
def add_disjunction
|
63
|
-
add_component(disjunction = Connective::Disjunction.new(setup))
|
64
|
-
disjunction
|
65
|
-
end
|
66
|
-
|
67
|
-
#
|
68
|
-
# Add a conjunction to the scope. In most cases, this will simply return
|
69
|
-
# the Scope object itself, since scopes by default combine their
|
70
|
-
# restrictions with OR semantics. The Connective::Disjunction class
|
71
|
-
# overrides this method to return a Connective::Conjunction.
|
72
|
-
#
|
73
|
-
# ==== Returns
|
74
|
-
#
|
75
|
-
# Scope:: Self or another scope with conjunctive semantics.
|
76
|
-
#
|
77
|
-
def add_conjunction
|
78
|
-
self
|
79
|
-
end
|
80
|
-
|
81
|
-
#
|
82
|
-
# Exclude a particular instance from the search results
|
83
|
-
#
|
84
|
-
# ==== Parameters
|
85
|
-
#
|
86
|
-
# instance<Object>:: instance to exclude from results
|
87
|
-
#
|
88
|
-
def exclude_instance(instance)
|
89
|
-
add_component(Restriction::SameAs.new(instance, true))
|
90
|
-
end
|
91
|
-
|
92
|
-
#
|
93
|
-
# Generate a DynamicQuery instance for the given base name.
|
94
|
-
# This gives you access to a subset of the Query API but the operations
|
95
|
-
# apply to dynamic fields inside the dynamic field definition specified
|
96
|
-
# by +base_name+.
|
97
|
-
#
|
98
|
-
# ==== Parameters
|
99
|
-
#
|
100
|
-
# base_name<Symbol>::
|
101
|
-
# Base name of the dynamic field definition to use in the dynamic query
|
102
|
-
# operations
|
103
|
-
#
|
104
|
-
# ==== Returns
|
105
|
-
#
|
106
|
-
# DynamicQuery::
|
107
|
-
# Instance providing dynamic query functionality for the given field
|
108
|
-
# definitions.
|
109
|
-
#
|
110
|
-
def dynamic_query(base_name)
|
111
|
-
DynamicQuery.new(setup.dynamic_field_factory(base_name), self)
|
112
|
-
end
|
113
|
-
|
114
|
-
#
|
115
|
-
# Determine which restriction type to add based on the type of the value.
|
116
|
-
# Used to interpret query conditions passed as a hash, as well as the
|
117
|
-
# short-form DSL::Scope#with method.
|
118
|
-
#
|
119
|
-
# ==== Parameters
|
120
|
-
#
|
121
|
-
# field_name<Symbol>:: Name of the field on which to apply the restriction
|
122
|
-
# value<Object,Array,Range>:: Value to which to apply to the restriction
|
123
|
-
#--
|
124
|
-
# negated<Boolean>:: Whether to negate the restriction.
|
125
|
-
#
|
126
|
-
def add_shorthand_restriction(field_name, value, negated = false) #:nodoc:
|
127
|
-
restriction_type =
|
128
|
-
case value
|
129
|
-
when Range
|
130
|
-
Restriction::Between
|
131
|
-
when Array
|
132
|
-
Restriction::AnyOf
|
133
|
-
else
|
134
|
-
Restriction::EqualTo
|
135
|
-
end
|
136
|
-
add_restriction(field_name, restriction_type, value, negated)
|
137
|
-
end
|
138
|
-
|
139
|
-
#
|
140
|
-
# Add a negated shorthand restriction. See #add_shorthand_restriction
|
141
|
-
#
|
142
|
-
def add_negated_shorthand_restriction(field_name, value)
|
143
|
-
add_shorthand_restriction(field_name, value, true)
|
144
|
-
end
|
145
|
-
|
146
|
-
private
|
147
|
-
|
148
|
-
#
|
149
|
-
# Build a field with the given field name. Subclasses may override this
|
150
|
-
# method.
|
151
|
-
#
|
152
|
-
def build_field(field_name)
|
153
|
-
setup.field(field_name)
|
154
|
-
end
|
155
|
-
|
156
|
-
#
|
157
|
-
# Return a setup object which can return a field object given a name.
|
158
|
-
# Subclasses may override this method.
|
159
|
-
#
|
160
|
-
def setup
|
161
|
-
@setup
|
3
|
+
class Scope < Connective::Conjunction
|
4
|
+
def to_params
|
5
|
+
{ :fq => @components.map { |component| component.to_boolean_phrase }}
|
162
6
|
end
|
163
7
|
end
|
164
8
|
end
|
data/lib/sunspot/query/sort.rb
CHANGED
@@ -1,9 +1,12 @@
|
|
1
1
|
module Sunspot
|
2
2
|
module Query
|
3
3
|
#
|
4
|
-
# The
|
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.
|
5
8
|
#
|
6
|
-
|
9
|
+
module Sort #:nodoc: all
|
7
10
|
DIRECTIONS = {
|
8
11
|
:asc => 'asc',
|
9
12
|
:ascending => 'asc',
|
@@ -11,25 +14,91 @@ module Sunspot
|
|
11
14
|
:descending => 'desc'
|
12
15
|
}
|
13
16
|
|
14
|
-
|
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
|
17
30
|
end
|
18
|
-
@field, @direction = field, (direction || :asc).to_sym
|
19
31
|
end
|
20
32
|
|
21
|
-
|
22
|
-
|
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
|
23
55
|
end
|
24
56
|
|
25
|
-
|
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
|
26
73
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
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
|
33
102
|
end
|
34
103
|
end
|
35
104
|
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
|