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.
Files changed (73) hide show
  1. data/README.rdoc +13 -21
  2. data/Rakefile +0 -2
  3. data/TODO +2 -15
  4. data/VERSION.yml +2 -2
  5. data/bin/sunspot-configure-solr +46 -0
  6. data/bin/sunspot-solr +15 -7
  7. data/lib/sunspot/adapters.rb +5 -1
  8. data/lib/sunspot/composite_setup.rb +186 -0
  9. data/lib/sunspot/configuration.rb +7 -1
  10. data/lib/sunspot/data_extractor.rb +10 -0
  11. data/lib/sunspot/date_facet.rb +36 -0
  12. data/lib/sunspot/date_facet_row.rb +17 -0
  13. data/lib/sunspot/dsl/field_query.rb +72 -0
  14. data/lib/sunspot/dsl/fields.rb +30 -3
  15. data/lib/sunspot/dsl/query.rb +16 -35
  16. data/lib/sunspot/dsl/query_facet.rb +31 -0
  17. data/lib/sunspot/dsl/scope.rb +76 -20
  18. data/lib/sunspot/dsl/search.rb +30 -0
  19. data/lib/sunspot/dsl.rb +1 -1
  20. data/lib/sunspot/facet.rb +17 -3
  21. data/lib/sunspot/facet_row.rb +4 -4
  22. data/lib/sunspot/field.rb +130 -207
  23. data/lib/sunspot/field_factory.rb +126 -0
  24. data/lib/sunspot/indexer.rb +61 -14
  25. data/lib/sunspot/instantiated_facet.rb +38 -0
  26. data/lib/sunspot/instantiated_facet_row.rb +12 -0
  27. data/lib/sunspot/query/base_query.rb +90 -0
  28. data/lib/sunspot/query/connective.rb +77 -0
  29. data/lib/sunspot/query/dynamic_query.rb +39 -56
  30. data/lib/sunspot/query/field_facet.rb +132 -4
  31. data/lib/sunspot/query/field_query.rb +57 -0
  32. data/lib/sunspot/query/pagination.rb +1 -1
  33. data/lib/sunspot/query/query_facet.rb +72 -0
  34. data/lib/sunspot/query/query_facet_row.rb +19 -0
  35. data/lib/sunspot/query/restriction.rb +9 -7
  36. data/lib/sunspot/query/scope.rb +165 -0
  37. data/lib/sunspot/query/sort.rb +17 -14
  38. data/lib/sunspot/query/sort_composite.rb +33 -0
  39. data/lib/sunspot/query.rb +162 -351
  40. data/lib/sunspot/query_facet.rb +33 -0
  41. data/lib/sunspot/query_facet_row.rb +21 -0
  42. data/lib/sunspot/schema.rb +165 -0
  43. data/lib/sunspot/search/hit.rb +62 -0
  44. data/lib/sunspot/search.rb +104 -41
  45. data/lib/sunspot/session.rb +64 -32
  46. data/lib/sunspot/setup.rb +119 -48
  47. data/lib/sunspot/type.rb +48 -2
  48. data/lib/sunspot.rb +74 -8
  49. data/solr/solr/conf/schema.xml +44 -225
  50. data/spec/api/build_search_spec.rb +557 -63
  51. data/spec/api/indexer_spec.rb +156 -74
  52. data/spec/api/query_spec.rb +55 -31
  53. data/spec/api/search_retrieval_spec.rb +210 -33
  54. data/spec/api/session_spec.rb +81 -26
  55. data/spec/api/sunspot_spec.rb +5 -7
  56. data/spec/integration/faceting_spec.rb +130 -0
  57. data/spec/integration/keyword_search_spec.rb +72 -31
  58. data/spec/integration/scoped_search_spec.rb +13 -0
  59. data/spec/integration/stored_fields_spec.rb +10 -0
  60. data/spec/mocks/blog.rb +3 -0
  61. data/spec/mocks/comment.rb +12 -23
  62. data/spec/mocks/connection.rb +84 -0
  63. data/spec/mocks/mock_adapter.rb +11 -3
  64. data/spec/mocks/mock_record.rb +41 -0
  65. data/spec/mocks/photo.rb +8 -0
  66. data/spec/mocks/post.rb +18 -23
  67. data/spec/spec_helper.rb +29 -14
  68. data/tasks/gemspec.rake +4 -3
  69. data/tasks/rdoc.rake +2 -2
  70. data/tasks/schema.rake +19 -0
  71. data/templates/schema.xml.haml +24 -0
  72. metadata +48 -7
  73. 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
- @fields, @text_fields, @dynamic_fields = [], [], []
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 fields for scope/ordering
22
+ # Add field_factories for scope/ordering
15
23
  #
16
24
  # ==== Parameters
17
25
  #
18
- # fields<Array>:: Array of Sunspot::Field objects
26
+ # field_factories<Array>:: Array of Sunspot::Field objects
19
27
  #
20
- def add_fields(fields)
21
- @fields.concat(Array(fields))
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 fields for fulltext search
35
+ # Add field_factories for fulltext search
26
36
  #
27
37
  # ==== Parameters
28
38
  #
29
- # fields<Array>:: Array of Sunspot::Field objects
39
+ # field_factories<Array>:: Array of Sunspot::Field objects
30
40
  #
31
- def add_text_fields(fields)
32
- @text_fields.concat(Array(fields))
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 fields
48
+ # Add dynamic field_factories
37
49
  #
38
50
  # ==== Parameters
39
51
  #
40
- # fields<Array>:: Array of dynamic field objects
52
+ # field_factories<Array>:: Array of dynamic field objects
41
53
  #
42
- def add_dynamic_fields(fields)
43
- @dynamic_fields.concat(Array(fields))
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
- # Get the fields associated with this setup as well as all inherited fields
55
- #
56
- # ==== Returns
57
- #
58
- # Array:: Collection of all fields associated with this setup
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
- get_inheritable_collection(:fields)
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 text fields associated with this setup as well as all inherited
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 text fields associated with this setup
122
+ # Array:: Collection of all field_factories associated with this setup
71
123
  #
72
- def text_fields
73
- get_inheritable_collection(:text_fields)
124
+ def field_factories
125
+ collection_from_inheritable_hash(:field_factories)
74
126
  end
75
127
 
76
128
  #
77
- # Get all static, dynamic, and text fields associated with this setup as
78
- # well as all inherited fields
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 and scope fields associated with this setup
134
+ # Array:: Collection of all text field_factories associated with this setup
83
135
  #
84
- def all_fields
85
- all_fields = []
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 fields for this and parent setups
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:: Dynamic fields
146
+ # Array:: Collection of all text and scope field_factories associated with this setup
96
147
  #
97
- def dynamic_fields
98
- get_inheritable_collection(:dynamic_fields)
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
- # Factory method for an Indexer object configured to use this setup
103
- #
155
+ # Get all dynamic field_factories for this and parent setups
156
+ #
104
157
  # ==== Returns
105
158
  #
106
- # Sunspot::Indexer:: Indexer configured with this setup
159
+ # Array:: Dynamic field_factories
107
160
  #
108
- def indexer(connection)
109
- Indexer.new(connection, self)
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 get_inheritable_collection(name)
139
- collection = instance_variable_get(:"@#{name}").dup
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
- setups[clazz.name.to_sym] || self.for(clazz.superclass) if clazz
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
- gem 'solr-ruby'
2
- require 'solr'
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 data_extractor indexer
6
- query search facet facet_row session type util dsl).each do |filename|
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 and Sunspot::DSL::Query for the full API presented
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
- def reset!
388
- @session = Session.new(session.config)
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