outoftime-sunspot 0.8.9 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +13 -21
- data/Rakefile +0 -2
- data/TODO +2 -15
- data/VERSION.yml +2 -2
- data/bin/sunspot-configure-solr +46 -0
- data/bin/sunspot-solr +15 -7
- data/lib/sunspot/adapters.rb +5 -1
- data/lib/sunspot/composite_setup.rb +186 -0
- data/lib/sunspot/configuration.rb +7 -1
- data/lib/sunspot/data_extractor.rb +10 -0
- data/lib/sunspot/date_facet.rb +36 -0
- data/lib/sunspot/date_facet_row.rb +17 -0
- data/lib/sunspot/dsl/field_query.rb +72 -0
- data/lib/sunspot/dsl/fields.rb +30 -3
- data/lib/sunspot/dsl/query.rb +16 -35
- data/lib/sunspot/dsl/query_facet.rb +31 -0
- data/lib/sunspot/dsl/scope.rb +76 -20
- data/lib/sunspot/dsl/search.rb +30 -0
- data/lib/sunspot/dsl.rb +1 -1
- data/lib/sunspot/facet.rb +17 -3
- data/lib/sunspot/facet_row.rb +4 -4
- data/lib/sunspot/field.rb +130 -207
- data/lib/sunspot/field_factory.rb +126 -0
- data/lib/sunspot/indexer.rb +61 -14
- data/lib/sunspot/instantiated_facet.rb +38 -0
- data/lib/sunspot/instantiated_facet_row.rb +12 -0
- data/lib/sunspot/query/base_query.rb +90 -0
- data/lib/sunspot/query/connective.rb +77 -0
- data/lib/sunspot/query/dynamic_query.rb +39 -56
- data/lib/sunspot/query/field_facet.rb +132 -4
- data/lib/sunspot/query/field_query.rb +57 -0
- data/lib/sunspot/query/pagination.rb +1 -1
- data/lib/sunspot/query/query_facet.rb +72 -0
- data/lib/sunspot/query/query_facet_row.rb +19 -0
- data/lib/sunspot/query/restriction.rb +9 -7
- data/lib/sunspot/query/scope.rb +165 -0
- data/lib/sunspot/query/sort.rb +17 -14
- data/lib/sunspot/query/sort_composite.rb +33 -0
- data/lib/sunspot/query.rb +162 -351
- data/lib/sunspot/query_facet.rb +33 -0
- data/lib/sunspot/query_facet_row.rb +21 -0
- data/lib/sunspot/schema.rb +165 -0
- data/lib/sunspot/search/hit.rb +62 -0
- data/lib/sunspot/search.rb +104 -41
- data/lib/sunspot/session.rb +64 -32
- data/lib/sunspot/setup.rb +119 -48
- data/lib/sunspot/type.rb +48 -2
- data/lib/sunspot.rb +74 -8
- data/solr/solr/conf/schema.xml +44 -225
- data/spec/api/build_search_spec.rb +557 -63
- data/spec/api/indexer_spec.rb +156 -74
- data/spec/api/query_spec.rb +55 -31
- data/spec/api/search_retrieval_spec.rb +210 -33
- data/spec/api/session_spec.rb +81 -26
- data/spec/api/sunspot_spec.rb +5 -7
- data/spec/integration/faceting_spec.rb +130 -0
- data/spec/integration/keyword_search_spec.rb +72 -31
- data/spec/integration/scoped_search_spec.rb +13 -0
- data/spec/integration/stored_fields_spec.rb +10 -0
- data/spec/mocks/blog.rb +3 -0
- data/spec/mocks/comment.rb +12 -23
- data/spec/mocks/connection.rb +84 -0
- data/spec/mocks/mock_adapter.rb +11 -3
- data/spec/mocks/mock_record.rb +41 -0
- data/spec/mocks/photo.rb +8 -0
- data/spec/mocks/post.rb +18 -23
- data/spec/spec_helper.rb +29 -14
- data/tasks/gemspec.rake +4 -3
- data/tasks/rdoc.rake +2 -2
- data/tasks/schema.rake +19 -0
- data/templates/schema.xml.haml +24 -0
- metadata +48 -7
- data/spec/mocks/base_class.rb +0 -2
data/lib/sunspot/setup.rb
CHANGED
@@ -5,42 +5,69 @@ module Sunspot
|
|
5
5
|
#
|
6
6
|
class Setup #:nodoc:
|
7
7
|
def initialize(clazz)
|
8
|
+
@clazz = clazz
|
8
9
|
@class_name = clazz.name
|
9
|
-
@
|
10
|
+
@field_factories, @text_field_factories, @dynamic_field_factories,
|
11
|
+
@field_factories_cache, @text_field_factories_cache,
|
12
|
+
@dynamic_field_factories_cache = *Array.new(6) { Hash.new }
|
10
13
|
@dsl = DSL::Fields.new(self)
|
14
|
+
add_field_factory(:class, Type::ClassType)
|
15
|
+
end
|
16
|
+
|
17
|
+
def type_names
|
18
|
+
[@class_name]
|
11
19
|
end
|
12
20
|
|
13
21
|
#
|
14
|
-
# Add
|
22
|
+
# Add field_factories for scope/ordering
|
15
23
|
#
|
16
24
|
# ==== Parameters
|
17
25
|
#
|
18
|
-
#
|
26
|
+
# field_factories<Array>:: Array of Sunspot::Field objects
|
19
27
|
#
|
20
|
-
def
|
21
|
-
|
28
|
+
def add_field_factory(name, type, options = {}, &block)
|
29
|
+
field_factory = FieldFactory::Static.new(name, type, options, &block)
|
30
|
+
@field_factories[field_factory.signature] = field_factory
|
31
|
+
@field_factories_cache[field_factory.name] = field_factory
|
22
32
|
end
|
23
33
|
|
24
34
|
#
|
25
|
-
# Add
|
35
|
+
# Add field_factories for fulltext search
|
26
36
|
#
|
27
37
|
# ==== Parameters
|
28
38
|
#
|
29
|
-
#
|
39
|
+
# field_factories<Array>:: Array of Sunspot::Field objects
|
30
40
|
#
|
31
|
-
def
|
32
|
-
|
41
|
+
def add_text_field_factory(name, options = {}, &block)
|
42
|
+
field_factory = FieldFactory::Static.new(name, Type::TextType, options, &block)
|
43
|
+
@text_field_factories[name] = field_factory
|
44
|
+
@text_field_factories_cache[field_factory.name] = field_factory
|
33
45
|
end
|
34
46
|
|
35
47
|
#
|
36
|
-
# Add dynamic
|
48
|
+
# Add dynamic field_factories
|
37
49
|
#
|
38
50
|
# ==== Parameters
|
39
51
|
#
|
40
|
-
#
|
52
|
+
# field_factories<Array>:: Array of dynamic field objects
|
41
53
|
#
|
42
|
-
def
|
43
|
-
|
54
|
+
def add_dynamic_field_factory(name, type, options = {}, &block)
|
55
|
+
field_factory = FieldFactory::Dynamic.new(name, type, options, &block)
|
56
|
+
@dynamic_field_factories[field_factory.signature] = field_factory
|
57
|
+
@dynamic_field_factories_cache[field_factory.name] = field_factory
|
58
|
+
end
|
59
|
+
|
60
|
+
def add_document_boost(attr_name, &block)
|
61
|
+
@document_boost_extractor =
|
62
|
+
if attr_name
|
63
|
+
if attr_name.respond_to?(:to_f)
|
64
|
+
DataExtractor::Constant.new(attr_name)
|
65
|
+
else
|
66
|
+
DataExtractor::AttributeExtractor.new(attr_name)
|
67
|
+
end
|
68
|
+
else
|
69
|
+
DataExtractor::BlockExtractor.new(&block)
|
70
|
+
end
|
44
71
|
end
|
45
72
|
|
46
73
|
#
|
@@ -50,63 +77,89 @@ module Sunspot
|
|
50
77
|
@dsl.instance_eval(&block)
|
51
78
|
end
|
52
79
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
80
|
+
def field(field_name)
|
81
|
+
if field_factory = @field_factories_cache[field_name.to_sym]
|
82
|
+
field_factory.build
|
83
|
+
else
|
84
|
+
raise(
|
85
|
+
UnrecognizedFieldError,
|
86
|
+
"No field configured for #{@clazz.name} with name '#{field_name}'"
|
87
|
+
)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def text_field(field_name)
|
92
|
+
if field_factory = @text_field_factories_cache[field_name.to_sym]
|
93
|
+
field_factory.build
|
94
|
+
else
|
95
|
+
raise(
|
96
|
+
UnrecognizedFieldError,
|
97
|
+
"No text field configured for #{@clazz.name} with name '#{field_name}'"
|
98
|
+
)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def dynamic_field_factory(field_name)
|
103
|
+
@dynamic_field_factories_cache[field_name.to_sym] || raise(
|
104
|
+
UnrecognizedFieldError,
|
105
|
+
"No dynamic field configured for #{@clazz.name} with name '#{field_name}'"
|
106
|
+
)
|
107
|
+
end
|
108
|
+
|
60
109
|
def fields
|
61
|
-
|
110
|
+
field_factories.map { |field_factory| field_factory.build }
|
111
|
+
end
|
112
|
+
|
113
|
+
def text_fields
|
114
|
+
text_field_factories.map { |text_field_factory| text_field_factory.build }
|
62
115
|
end
|
63
116
|
|
64
117
|
#
|
65
|
-
# Get the
|
66
|
-
# text fields
|
118
|
+
# Get the field_factories associated with this setup as well as all inherited field_factories
|
67
119
|
#
|
68
120
|
# ==== Returns
|
69
121
|
#
|
70
|
-
# Array:: Collection of all
|
122
|
+
# Array:: Collection of all field_factories associated with this setup
|
71
123
|
#
|
72
|
-
def
|
73
|
-
|
124
|
+
def field_factories
|
125
|
+
collection_from_inheritable_hash(:field_factories)
|
74
126
|
end
|
75
127
|
|
76
128
|
#
|
77
|
-
# Get
|
78
|
-
#
|
129
|
+
# Get the text field_factories associated with this setup as well as all inherited
|
130
|
+
# text field_factories
|
79
131
|
#
|
80
132
|
# ==== Returns
|
81
133
|
#
|
82
|
-
# Array:: Collection of all text
|
134
|
+
# Array:: Collection of all text field_factories associated with this setup
|
83
135
|
#
|
84
|
-
def
|
85
|
-
|
86
|
-
all_fields.concat(fields).concat(text_fields).concat(dynamic_fields)
|
87
|
-
all_fields
|
136
|
+
def text_field_factories
|
137
|
+
collection_from_inheritable_hash(:text_field_factories)
|
88
138
|
end
|
89
139
|
|
90
140
|
#
|
91
|
-
# Get all dynamic
|
92
|
-
#
|
141
|
+
# Get all static, dynamic, and text field_factories associated with this setup as
|
142
|
+
# well as all inherited field_factories
|
143
|
+
#
|
93
144
|
# ==== Returns
|
94
145
|
#
|
95
|
-
# Array::
|
146
|
+
# Array:: Collection of all text and scope field_factories associated with this setup
|
96
147
|
#
|
97
|
-
def
|
98
|
-
|
148
|
+
def all_field_factories
|
149
|
+
all_field_factories = []
|
150
|
+
all_field_factories.concat(field_factories).concat(text_field_factories).concat(dynamic_field_factories)
|
151
|
+
all_field_factories
|
99
152
|
end
|
100
153
|
|
101
154
|
#
|
102
|
-
#
|
103
|
-
#
|
155
|
+
# Get all dynamic field_factories for this and parent setups
|
156
|
+
#
|
104
157
|
# ==== Returns
|
105
158
|
#
|
106
|
-
#
|
159
|
+
# Array:: Dynamic field_factories
|
107
160
|
#
|
108
|
-
def
|
109
|
-
|
161
|
+
def dynamic_field_factories
|
162
|
+
collection_from_inheritable_hash(:dynamic_field_factories)
|
110
163
|
end
|
111
164
|
|
112
165
|
#
|
@@ -120,6 +173,12 @@ module Sunspot
|
|
120
173
|
Util.full_const_get(@class_name)
|
121
174
|
end
|
122
175
|
|
176
|
+
def document_boost_for(model)
|
177
|
+
if @document_boost_extractor
|
178
|
+
@document_boost_extractor.value_for(model)
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
123
182
|
protected
|
124
183
|
|
125
184
|
#
|
@@ -133,12 +192,18 @@ module Sunspot
|
|
133
192
|
Setup.for(clazz.superclass)
|
134
193
|
end
|
135
194
|
|
195
|
+
def get_inheritable_hash(name)
|
196
|
+
hash = instance_variable_get(:"@#{name}")
|
197
|
+
parent.get_inheritable_hash(name).each_pair do |key, value|
|
198
|
+
hash[key] = value unless hash.has_key?(key)
|
199
|
+
end if parent
|
200
|
+
hash
|
201
|
+
end
|
202
|
+
|
136
203
|
private
|
137
204
|
|
138
|
-
def
|
139
|
-
|
140
|
-
collection.concat(parent.send(name)) if parent
|
141
|
-
collection
|
205
|
+
def collection_from_inheritable_hash(name)
|
206
|
+
get_inheritable_hash(name).values
|
142
207
|
end
|
143
208
|
|
144
209
|
class <<self
|
@@ -164,7 +229,13 @@ module Sunspot
|
|
164
229
|
# Setup instance associated with the given class or its nearest ancestor
|
165
230
|
#
|
166
231
|
def for(clazz) #:nodoc:
|
167
|
-
|
232
|
+
class_name =
|
233
|
+
if clazz.respond_to?(:name)
|
234
|
+
clazz.name
|
235
|
+
else
|
236
|
+
clazz
|
237
|
+
end
|
238
|
+
setups[class_name.to_sym] || self.for(clazz.superclass) if clazz
|
168
239
|
end
|
169
240
|
|
170
241
|
protected
|
data/lib/sunspot/type.rb
CHANGED
@@ -109,8 +109,6 @@ module Sunspot
|
|
109
109
|
time =
|
110
110
|
if value.respond_to?(:utc)
|
111
111
|
value
|
112
|
-
elsif %w(year mon mday).each { |method| value.respond_to?(method) }
|
113
|
-
Time.gm(value.year, value.mon, value.mday)
|
114
112
|
else
|
115
113
|
Time.parse(value.to_s)
|
116
114
|
end
|
@@ -124,6 +122,38 @@ module Sunspot
|
|
124
122
|
end
|
125
123
|
end
|
126
124
|
|
125
|
+
#
|
126
|
+
# The DateType encapsulates dates (without time information). Internally,
|
127
|
+
# Solr does not have a date-only type, so this type indexes data using
|
128
|
+
# Solr's DateField type (which is actually date/time), midnight UTC of the
|
129
|
+
# indexed date.
|
130
|
+
#
|
131
|
+
module DateType
|
132
|
+
class <<self
|
133
|
+
def indexed_name(name) #:nodoc:
|
134
|
+
"#{name}_d"
|
135
|
+
end
|
136
|
+
|
137
|
+
def to_indexed(value) #:nodoc:
|
138
|
+
if value
|
139
|
+
time =
|
140
|
+
if %w(year mon mday).all? { |method| value.respond_to?(method) }
|
141
|
+
Time.utc(value.year, value.mon, value.mday)
|
142
|
+
else
|
143
|
+
date = Date.parse(value.to_s)
|
144
|
+
Time.utc(date.year, date.mon, date.mday)
|
145
|
+
end
|
146
|
+
time.utc.xmlschema
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
def cast(string)
|
151
|
+
time = Time.xmlschema(string)
|
152
|
+
Date.civil(time.year, time.mon, time.mday)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
127
157
|
#
|
128
158
|
# The boolean type represents true/false values. Note that +nil+ will not be
|
129
159
|
# indexed at all; only +false+ will be indexed with a false value.
|
@@ -150,5 +180,21 @@ module Sunspot
|
|
150
180
|
end
|
151
181
|
end
|
152
182
|
end
|
183
|
+
|
184
|
+
module ClassType
|
185
|
+
class <<self
|
186
|
+
def indexed_name(name)
|
187
|
+
'class_name'
|
188
|
+
end
|
189
|
+
|
190
|
+
def to_indexed(value)
|
191
|
+
value.name
|
192
|
+
end
|
193
|
+
|
194
|
+
def cast(string)
|
195
|
+
Sunspot::Util.full_const_get(string)
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
153
199
|
end
|
154
200
|
end
|
data/lib/sunspot.rb
CHANGED
@@ -1,9 +1,18 @@
|
|
1
|
-
|
2
|
-
require '
|
1
|
+
begin
|
2
|
+
require 'time'
|
3
|
+
require 'date'
|
4
|
+
require 'rsolr'
|
5
|
+
rescue LoadError
|
6
|
+
require 'rubygems'
|
7
|
+
require 'rsolr'
|
8
|
+
end
|
9
|
+
|
3
10
|
require File.join(File.dirname(__FILE__), 'light_config')
|
4
11
|
|
5
|
-
%w(adapters configuration setup field
|
6
|
-
query search facet facet_row
|
12
|
+
%w(util adapters configuration setup composite_setup field field_factory
|
13
|
+
data_extractor indexer query search facet facet_row instantiated_facet
|
14
|
+
instantiated_facet_row date_facet date_facet_row query_facet query_facet_row
|
15
|
+
session type dsl).each do |filename|
|
7
16
|
require File.join(File.dirname(__FILE__), 'sunspot', filename)
|
8
17
|
end
|
9
18
|
|
@@ -282,8 +291,8 @@ module Sunspot
|
|
282
291
|
# query.with(:blog_id, @current_blog.id)
|
283
292
|
# end
|
284
293
|
#
|
285
|
-
# See Sunspot::DSL::Scope
|
286
|
-
# inside the block.
|
294
|
+
# See Sunspot::DSL::Search, Sunspot::DSL::Scope, Sunspot::DSL::FieldQuery
|
295
|
+
# and Sunspot::DSL::Query for the full API presented inside the block.
|
287
296
|
#
|
288
297
|
def search(*types, &block)
|
289
298
|
session.search(*types, &block)
|
@@ -319,6 +328,30 @@ module Sunspot
|
|
319
328
|
session.remove!(*objects)
|
320
329
|
end
|
321
330
|
|
331
|
+
#
|
332
|
+
# Remove an object from the index using its class name and primary key.
|
333
|
+
# Useful if you know this information and want to remove an object without
|
334
|
+
# instantiating it from persistent storage
|
335
|
+
#
|
336
|
+
# ==== Parameters
|
337
|
+
#
|
338
|
+
# clazz<Class>:: Class of the object, or class name as a string or symbol
|
339
|
+
# id::
|
340
|
+
# Primary key of the object. This should be the same id that would be
|
341
|
+
# returned by the class's instance adapter.
|
342
|
+
#
|
343
|
+
def remove_by_id(clazz, id)
|
344
|
+
session.remove_by_id(clazz, id)
|
345
|
+
end
|
346
|
+
|
347
|
+
#
|
348
|
+
# Remove an object by class name and primary key, and immediately commit.
|
349
|
+
# See #remove_by_id and #commit
|
350
|
+
#
|
351
|
+
def remove_by_id!(clazz, id)
|
352
|
+
session.remove_by_id!(clazz, id)
|
353
|
+
end
|
354
|
+
|
322
355
|
# Remove all objects of the given classes from the index. There isn't much
|
323
356
|
# use for this in general operations but it can be useful for maintenance,
|
324
357
|
# testing, etc. If no arguments are passed, remove everything from the
|
@@ -350,6 +383,27 @@ module Sunspot
|
|
350
383
|
session.remove_all!(*classes)
|
351
384
|
end
|
352
385
|
|
386
|
+
#
|
387
|
+
# Process all adds in a batch. Any Sunspot adds initiated inside the block
|
388
|
+
# will be sent in bulk when the block finishes. Useful if your application
|
389
|
+
# initiates index adds from various places in code as part of a single
|
390
|
+
# operation; doing a batch add will give better performance.
|
391
|
+
#
|
392
|
+
# ==== Example
|
393
|
+
#
|
394
|
+
# Sunspot.batch do
|
395
|
+
# post = Post.new
|
396
|
+
# Sunspot.add(post)
|
397
|
+
# comment = Comment.new
|
398
|
+
# Sunspot.add(comment)
|
399
|
+
# end
|
400
|
+
#
|
401
|
+
# Sunspot will send both the post and the comment in a single request.
|
402
|
+
#
|
403
|
+
def batch(&block)
|
404
|
+
session.batch(&block)
|
405
|
+
end
|
406
|
+
|
353
407
|
#
|
354
408
|
# True if documents have been added, updated, or removed since the last
|
355
409
|
# commit.
|
@@ -384,8 +438,20 @@ module Sunspot
|
|
384
438
|
# Resets the singleton session. This is useful for clearing out all
|
385
439
|
# static data between tests, but probably nowhere else.
|
386
440
|
#
|
387
|
-
|
388
|
-
|
441
|
+
# ==== Parameters
|
442
|
+
#
|
443
|
+
# keep_config<Boolean>::
|
444
|
+
# Whether to retain the configuration used by the current singleton
|
445
|
+
# session. Default false.
|
446
|
+
#
|
447
|
+
def reset!(keep_config = false)
|
448
|
+
config =
|
449
|
+
if keep_config
|
450
|
+
session.config
|
451
|
+
else
|
452
|
+
Configuration.build
|
453
|
+
end
|
454
|
+
@session = Session.new(config)
|
389
455
|
end
|
390
456
|
|
391
457
|
private
|