nxa-sunspot 0.10.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. data/History.txt +153 -0
  2. data/LICENSE +18 -0
  3. data/README.rdoc +191 -0
  4. data/Rakefile +9 -0
  5. data/TODO +14 -0
  6. data/VERSION.yml +4 -0
  7. data/bin/sunspot-configure-solr +40 -0
  8. data/bin/sunspot-solr +95 -0
  9. data/lib/light_config.rb +40 -0
  10. data/lib/sunspot.rb +488 -0
  11. data/lib/sunspot/adapters.rb +265 -0
  12. data/lib/sunspot/composite_setup.rb +188 -0
  13. data/lib/sunspot/configuration.rb +56 -0
  14. data/lib/sunspot/data_extractor.rb +50 -0
  15. data/lib/sunspot/dsl.rb +4 -0
  16. data/lib/sunspot/dsl/field_query.rb +150 -0
  17. data/lib/sunspot/dsl/fields.rb +100 -0
  18. data/lib/sunspot/dsl/fulltext.rb +228 -0
  19. data/lib/sunspot/dsl/query.rb +162 -0
  20. data/lib/sunspot/dsl/query_facet.rb +36 -0
  21. data/lib/sunspot/dsl/restriction.rb +25 -0
  22. data/lib/sunspot/dsl/scope.rb +225 -0
  23. data/lib/sunspot/dsl/search.rb +30 -0
  24. data/lib/sunspot/field.rb +181 -0
  25. data/lib/sunspot/field_factory.rb +144 -0
  26. data/lib/sunspot/indexer.rb +133 -0
  27. data/lib/sunspot/query.rb +9 -0
  28. data/lib/sunspot/query/abstract_field_facet.rb +43 -0
  29. data/lib/sunspot/query/boost_query.rb +20 -0
  30. data/lib/sunspot/query/connective.rb +189 -0
  31. data/lib/sunspot/query/date_field_facet.rb +14 -0
  32. data/lib/sunspot/query/dismax.rb +88 -0
  33. data/lib/sunspot/query/field_facet.rb +9 -0
  34. data/lib/sunspot/query/highlighting.rb +55 -0
  35. data/lib/sunspot/query/local.rb +27 -0
  36. data/lib/sunspot/query/pagination.rb +38 -0
  37. data/lib/sunspot/query/query.rb +86 -0
  38. data/lib/sunspot/query/query_facet.rb +16 -0
  39. data/lib/sunspot/query/restriction.rb +254 -0
  40. data/lib/sunspot/query/scope.rb +9 -0
  41. data/lib/sunspot/query/sort.rb +105 -0
  42. data/lib/sunspot/query/sort_composite.rb +33 -0
  43. data/lib/sunspot/query/text_field_boost.rb +15 -0
  44. data/lib/sunspot/schema.rb +147 -0
  45. data/lib/sunspot/search.rb +216 -0
  46. data/lib/sunspot/search/date_facet.rb +35 -0
  47. data/lib/sunspot/search/facet_row.rb +27 -0
  48. data/lib/sunspot/search/field_facet.rb +44 -0
  49. data/lib/sunspot/search/highlight.rb +38 -0
  50. data/lib/sunspot/search/hit.rb +117 -0
  51. data/lib/sunspot/search/query_facet.rb +62 -0
  52. data/lib/sunspot/session.rb +236 -0
  53. data/lib/sunspot/setup.rb +323 -0
  54. data/lib/sunspot/text_field_setup.rb +29 -0
  55. data/lib/sunspot/type.rb +204 -0
  56. data/lib/sunspot/util.rb +210 -0
  57. data/solr/etc/jetty.xml +212 -0
  58. data/solr/etc/webdefault.xml +379 -0
  59. data/solr/lib/jetty-6.1.3.jar +0 -0
  60. data/solr/lib/jetty-util-6.1.3.jar +0 -0
  61. data/solr/lib/jsp-2.1/ant-1.6.5.jar +0 -0
  62. data/solr/lib/jsp-2.1/core-3.1.1.jar +0 -0
  63. data/solr/lib/jsp-2.1/jsp-2.1.jar +0 -0
  64. data/solr/lib/jsp-2.1/jsp-api-2.1.jar +0 -0
  65. data/solr/lib/servlet-api-2.5-6.1.3.jar +0 -0
  66. data/solr/solr/conf/elevate.xml +36 -0
  67. data/solr/solr/conf/protwords.txt +21 -0
  68. data/solr/solr/conf/schema.xml +64 -0
  69. data/solr/solr/conf/solrconfig.xml +725 -0
  70. data/solr/solr/conf/stopwords.txt +57 -0
  71. data/solr/solr/conf/synonyms.txt +31 -0
  72. data/solr/solr/lib/geoapi-nogenerics-2.1-M2.jar +0 -0
  73. data/solr/solr/lib/gt2-referencing-2.3.1.jar +0 -0
  74. data/solr/solr/lib/jsr108-0.01.jar +0 -0
  75. data/solr/solr/lib/locallucene.jar +0 -0
  76. data/solr/solr/lib/localsolr.jar +0 -0
  77. data/solr/start.jar +0 -0
  78. data/solr/webapps/solr.war +0 -0
  79. data/spec/api/adapters_spec.rb +33 -0
  80. data/spec/api/indexer/attributes_spec.rb +110 -0
  81. data/spec/api/indexer/batch_spec.rb +46 -0
  82. data/spec/api/indexer/dynamic_fields_spec.rb +33 -0
  83. data/spec/api/indexer/fixed_fields_spec.rb +57 -0
  84. data/spec/api/indexer/fulltext_spec.rb +43 -0
  85. data/spec/api/indexer/removal_spec.rb +46 -0
  86. data/spec/api/indexer/spec_helper.rb +1 -0
  87. data/spec/api/indexer_spec.rb +4 -0
  88. data/spec/api/query/adjust_params_spec.rb +37 -0
  89. data/spec/api/query/connectives_spec.rb +176 -0
  90. data/spec/api/query/dsl_spec.rb +12 -0
  91. data/spec/api/query/dynamic_fields_spec.rb +149 -0
  92. data/spec/api/query/faceting_spec.rb +296 -0
  93. data/spec/api/query/fulltext_spec.rb +281 -0
  94. data/spec/api/query/highlighting_spec.rb +225 -0
  95. data/spec/api/query/local_spec.rb +62 -0
  96. data/spec/api/query/ordering_pagination_spec.rb +95 -0
  97. data/spec/api/query/scope_spec.rb +266 -0
  98. data/spec/api/query/spec_helper.rb +1 -0
  99. data/spec/api/query/text_field_scoping_spec.rb +30 -0
  100. data/spec/api/query/types_spec.rb +20 -0
  101. data/spec/api/search/dynamic_fields_spec.rb +27 -0
  102. data/spec/api/search/faceting_spec.rb +242 -0
  103. data/spec/api/search/highlighting_spec.rb +65 -0
  104. data/spec/api/search/hits_spec.rb +67 -0
  105. data/spec/api/search/results_spec.rb +52 -0
  106. data/spec/api/search/search_spec.rb +23 -0
  107. data/spec/api/search/spec_helper.rb +1 -0
  108. data/spec/api/session_spec.rb +198 -0
  109. data/spec/api/spec_helper.rb +1 -0
  110. data/spec/api/sunspot_spec.rb +18 -0
  111. data/spec/helpers/indexer_helper.rb +29 -0
  112. data/spec/helpers/query_helper.rb +13 -0
  113. data/spec/helpers/search_helper.rb +78 -0
  114. data/spec/integration/dynamic_fields_spec.rb +55 -0
  115. data/spec/integration/faceting_spec.rb +188 -0
  116. data/spec/integration/highlighting_spec.rb +22 -0
  117. data/spec/integration/indexing_spec.rb +7 -0
  118. data/spec/integration/keyword_search_spec.rb +245 -0
  119. data/spec/integration/local_search_spec.rb +56 -0
  120. data/spec/integration/scoped_search_spec.rb +303 -0
  121. data/spec/integration/spec_helper.rb +7 -0
  122. data/spec/integration/stored_fields_spec.rb +10 -0
  123. data/spec/integration/test_pagination.rb +32 -0
  124. data/spec/mocks/adapters.rb +32 -0
  125. data/spec/mocks/blog.rb +3 -0
  126. data/spec/mocks/comment.rb +19 -0
  127. data/spec/mocks/connection.rb +106 -0
  128. data/spec/mocks/mock_adapter.rb +30 -0
  129. data/spec/mocks/mock_record.rb +48 -0
  130. data/spec/mocks/photo.rb +11 -0
  131. data/spec/mocks/post.rb +75 -0
  132. data/spec/mocks/super_class.rb +2 -0
  133. data/spec/mocks/user.rb +8 -0
  134. data/spec/spec_helper.rb +68 -0
  135. data/tasks/gemspec.rake +42 -0
  136. data/tasks/rcov.rake +28 -0
  137. data/tasks/rdoc.rake +22 -0
  138. data/tasks/schema.rake +19 -0
  139. data/tasks/spec.rake +24 -0
  140. data/tasks/todo.rake +4 -0
  141. data/templates/schema.xml.erb +36 -0
  142. metadata +319 -0
@@ -0,0 +1,40 @@
1
+ module LightConfig
2
+ class Configuration
3
+ def initialize(&block)
4
+ @properties = {}
5
+ ::LightConfig::Builder.new(self).instance_eval(&block)
6
+ singleton = (class <<self; self; end)
7
+ @properties.keys.each do |property|
8
+ singleton.module_eval do
9
+ define_method property do
10
+ @properties[property]
11
+ end
12
+
13
+ define_method "#{property}=" do |value|
14
+ @properties[property] = value
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ class Builder
22
+ def initialize(configuration)
23
+ @configuration = configuration
24
+ end
25
+
26
+ def method_missing(method, *args, &block)
27
+ raise ArgumentError("wrong number of arguments(#{args.length} for 1)") unless args.length < 2
28
+ value = if block then ::LightConfig::Configuration.new(&block)
29
+ else args.first
30
+ end
31
+ @configuration.instance_variable_get(:@properties)[method] = value
32
+ end
33
+ end
34
+
35
+ class <<self
36
+ def build(&block)
37
+ LightConfig::Configuration.new(&block)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,488 @@
1
+ require 'set'
2
+ require 'time'
3
+ require 'date'
4
+ require 'enumerator'
5
+ begin
6
+ require 'rsolr'
7
+ rescue LoadError
8
+ require 'rubygems'
9
+ require 'rsolr'
10
+ end
11
+
12
+ require File.join(File.dirname(__FILE__), 'light_config')
13
+
14
+ %w(util adapters configuration setup composite_setup text_field_setup field
15
+ field_factory data_extractor indexer query search session type
16
+ dsl).each do |filename|
17
+ require File.join(File.dirname(__FILE__), 'sunspot', filename)
18
+ end
19
+
20
+ #
21
+ # The Sunspot module provides class-method entry points to most of the
22
+ # functionality provided by the Sunspot library. Internally, the Sunspot
23
+ # singleton class contains a (non-thread-safe!) instance of Sunspot::Session,
24
+ # to which it delegates most of the class methods it exposes. In the method
25
+ # documentation below, this instance is referred to as the "singleton session".
26
+ #
27
+ # Though the singleton session provides a convenient entry point to Sunspot,
28
+ # it is by no means required to use the Sunspot class methods. Multiple sessions
29
+ # may be instantiated and used (if you need to connect to multiple Solr
30
+ # instances, for example.)
31
+ #
32
+ # Note that the configuration of classes for index/search (the +setup+
33
+ # method) is _not_ session-specific, but rather global.
34
+ #
35
+ module Sunspot
36
+ UnrecognizedFieldError = Class.new(Exception)
37
+ UnrecognizedRestrictionError = Class.new(Exception)
38
+ NoAdapterError = Class.new(Exception)
39
+ NoSetupError = Class.new(Exception)
40
+ IllegalSearchError = Class.new(Exception)
41
+
42
+
43
+ class <<self
44
+ #
45
+ # Clients can inject a session proxy, allowing them to implement custom
46
+ # session-management logic while retaining the Sunspot singleton API as
47
+ # an available interface. The object assigned to this attribute must
48
+ # respond to all of the public methods of the Sunspot::Session class.
49
+ #
50
+ attr_writer :session
51
+
52
+ # Configures indexing and search for a given class.
53
+ #
54
+ # ==== Parameters
55
+ #
56
+ # clazz<Class>:: class to configure
57
+ #
58
+ # ==== Example
59
+ #
60
+ # Sunspot.setup(Post) do
61
+ # text :title, :body
62
+ # string :author_name
63
+ # integer :blog_id
64
+ # integer :category_ids
65
+ # float :average_rating, :using => :ratings_average
66
+ # time :published_at
67
+ # string :sort_title do
68
+ # title.downcase.sub(/^(an?|the)\W+/, ''/) if title = self.title
69
+ # end
70
+ # end
71
+ #
72
+ # ====== Attribute Fields vs. Virtual Fields
73
+ #
74
+ # Attribute fields call a method on the indexed object and index the
75
+ # return value. All of the fields defined above except for the last one are
76
+ # attribute fields. By default, the field name will also be the attribute
77
+ # used; this can be overriden with the +:using+ option, as in
78
+ # +:average_rating+ above. In that case, the attribute +:ratings_average+
79
+ # will be indexed with the field name +:average_rating+.
80
+ #
81
+ # +:sort_title+ is a virtual field, which evaluates the block inside the
82
+ # context of the instance being indexed, and indexes the value returned
83
+ # by the block. If the block you pass takes an argument, it will be passed
84
+ # the instance rather than being evaluated inside of it; so, the following
85
+ # example is equivalent to the one above (assuming #title is public):
86
+ #
87
+ # Sunspot.setup(Post) do
88
+ # string :sort_title do |post|
89
+ # post.title.downcase.sub(/^(an?|the)\W+/, ''/) if title = self.title
90
+ # end
91
+ # end
92
+ #
93
+ # ===== Field Types
94
+ #
95
+ # The available types are:
96
+ #
97
+ # * +text+
98
+ # * +string+
99
+ # * +integer+
100
+ # * +float+
101
+ # * +time+
102
+ # * +boolean+
103
+ #
104
+ # Note that the +text+ type behaves quite differently from the others -
105
+ # this is the type that is indexed as fulltext, and is searched using the
106
+ # +keywords+ method inside the search DSL. Text fields cannot have
107
+ # restrictions set on them, nor can they be used in order statements or
108
+ # for facets. All other types are indexed literally, and thus can be used
109
+ # for all of those operations. They will not, however, be searched in
110
+ # fulltext. In this way, Sunspot provides a complete barrier between
111
+ # fulltext fields and value fields.
112
+ #
113
+ # It is fine to specify a field both as a text field and a string field;
114
+ # internally, the fields will have different names so there is no danger
115
+ # of conflict.
116
+ #
117
+ # ===== Dynamic Fields
118
+ #
119
+ # For use cases which have highly dynamic data models (for instance, an
120
+ # open set of key-value pairs attached to a model), it may be useful to
121
+ # defer definition of fields until indexing time. Sunspot exposes dynamic
122
+ # fields, which define a data accessor (either attribute or virtual, see
123
+ # above), which accepts a hash of field names to values. Note that the field
124
+ # names in the hash are internally scoped to the base name of the dynamic
125
+ # field, so any time they are referred to, they are referred to using both
126
+ # the base name and the dynamic (runtime-specified) name.
127
+ #
128
+ # Dynamic fields are speficied in the setup block using the type name
129
+ # prefixed by +dynamic_+. For example:
130
+ #
131
+ # Sunspot.setup(Post) do
132
+ # dynamic_string :custom_values do
133
+ # key_value_pairs.inject({}) do |hash, key_value_pair|
134
+ # hash[key_value_pair.key.to_sym] = key_value_pair.value
135
+ # end
136
+ # end
137
+ # end
138
+ #
139
+ # If you later wanted to facet all of the values for the key "cuisine",
140
+ # you could issue:
141
+ #
142
+ # Sunspot.search(Post) do
143
+ # dynamic :custom_values do
144
+ # facet :cuisine
145
+ # end
146
+ # end
147
+ #
148
+ # In the documentation, +:custom_values+ is referred to as the "base name" -
149
+ # that is, the one specified statically - and +:cuisine+ is referred to as
150
+ # the dynamic name, which is the part that is specified at indexing time.
151
+ #
152
+ def setup(clazz, &block)
153
+ Setup.setup(clazz, &block)
154
+ end
155
+
156
+ # Indexes objects on the singleton session.
157
+ #
158
+ # ==== Parameters
159
+ #
160
+ # objects...<Object>:: objects to index (may pass an array or varargs)
161
+ #
162
+ # ==== Example
163
+ #
164
+ # post1, post2 = new Array(2) { Post.create }
165
+ # Sunspot.index(post1, post2)
166
+ #
167
+ # Note that indexed objects won't be reflected in search until a commit is
168
+ # sent - see Sunspot.index! and Sunspot.commit
169
+ #
170
+ def index(*objects)
171
+ session.index(*objects)
172
+ end
173
+
174
+ # Indexes objects on the singleton session and commits immediately.
175
+ #
176
+ # See: Sunspot.index and Sunspot.commit
177
+ #
178
+ # ==== Parameters
179
+ #
180
+ # objects...<Object>:: objects to index (may pass an array or varargs)
181
+ #
182
+ def index!(*objects)
183
+ session.index!(*objects)
184
+ end
185
+
186
+ # Commits the singleton session
187
+ #
188
+ # When documents are added to or removed from Solr, the changes are
189
+ # initially stored in memory, and are not reflected in Solr's existing
190
+ # searcher instance. When a commit message is sent, the changes are written
191
+ # to disk, and a new searcher is spawned. Commits are thus fairly
192
+ # expensive, so if your application needs to index several documents as part
193
+ # of a single operation, it is advisable to index them all and then call
194
+ # commit at the end of the operation.
195
+ #
196
+ # Note that Solr can also be configured to automatically perform a commit
197
+ # after either a specified interval after the last change, or after a
198
+ # specified number of documents are added. See
199
+ # http://wiki.apache.org/solr/SolrConfigXml
200
+ #
201
+ def commit
202
+ session.commit
203
+ end
204
+
205
+ #
206
+ # Create a new Search instance, but do not execute it immediately. Generally
207
+ # you will want to use the #search method to execute searches using the
208
+ # DSL; however, if you are building searches dynamically (using the Builder
209
+ # pattern, for instance), it may be easier to access the Query API directly.
210
+ #
211
+ # ==== Parameters
212
+ #
213
+ # types<Class>...::
214
+ # One or more types to search for. If no types are passed, all
215
+ # configured types will be searched for.
216
+ #
217
+ # ==== Returns
218
+ #
219
+ # Sunspot::Search::
220
+ # Search object, not yet executed. Query parameters can be added manually;
221
+ # then #execute! should be called.
222
+ #
223
+ def new_search(*types)
224
+ session.new_search(*types)
225
+ end
226
+
227
+
228
+ # Search for objects in the index.
229
+ #
230
+ # ==== Parameters
231
+ #
232
+ # types<Class>...::
233
+ # One or more types to search for. If no types are passed, all
234
+ # configured types will be searched.
235
+ #
236
+ # ==== Returns
237
+ #
238
+ # Sunspot::Search:: Object containing results, facets, count, etc.
239
+ #
240
+ # The fields available for restriction, ordering, etc. are those that meet
241
+ # the following criteria:
242
+ #
243
+ # * They are not of type +text+.
244
+ # * They are defined for at least one of the classes being searched
245
+ # * They have the same data type for all of the classes being searched.
246
+ # * They have the same multiple flag for all of the classes being searched.
247
+ # * They have the same stored flag for all of the classes being searched.
248
+ #
249
+ # The restrictions available are the constants defined in the
250
+ # Sunspot::Restriction class. The standard restrictions are:
251
+ #
252
+ # with(:field_name).equal_to(value)
253
+ # with(:field_name, value) # shorthand for above
254
+ # with(:field_name).less_than(value)
255
+ # with(:field_name).greater_than(value)
256
+ # with(:field_name).between(value1..value2)
257
+ # with(:field_name).any_of([value1, value2, value3])
258
+ # with(:field_name).all_of([value1, value2, value3])
259
+ # without(some_instance) # exclude that particular instance
260
+ #
261
+ # +without+ can be substituted for +with+, causing the restriction to be
262
+ # negated. In the last example above, only +without+ works, as it does not
263
+ # make sense to search only for an instance you already have.
264
+ #
265
+ # Equality restrictions can take +nil+ as a value, which restricts the
266
+ # results to documents that have no value for the given field. Passing +nil+
267
+ # as a value to other restriction types is illegal. Thus:
268
+ #
269
+ # with(:field_name, nil) # ok
270
+ # with(:field_name).equal_to(nil) # ok
271
+ # with(:field_name).less_than(nil) # bad
272
+ #
273
+ # ==== Example
274
+ #
275
+ # Sunspot.search(Post) do
276
+ # keywords 'great pizza'
277
+ # with(:published_at).less_than Time.now
278
+ # with :blog_id, 1
279
+ # without current_post
280
+ # facet :category_ids
281
+ # order_by :published_at, :desc
282
+ # paginate 2, 15
283
+ # end
284
+ #
285
+ # If the block passed to #search takes an argument, that argument will
286
+ # present the DSL, and the block will be evaluated in the calling context.
287
+ # This will come in handy for building searches using instance data or
288
+ # methods, e.g.:
289
+ #
290
+ # Sunspot.search(Post) do |query|
291
+ # query.with(:blog_id, @current_blog.id)
292
+ # end
293
+ #
294
+ # See Sunspot::DSL::Search, Sunspot::DSL::Scope, Sunspot::DSL::FieldQuery
295
+ # and Sunspot::DSL::Query for the full API presented inside the block.
296
+ #
297
+ def search(*types, &block)
298
+ session.search(*types, &block)
299
+ end
300
+
301
+ # Remove objects from the index. Any time an object is destroyed, it must
302
+ # be removed from the index; otherwise, the index will contain broken
303
+ # references to objects that do not exist, which will cause errors when
304
+ # those objects are matched in search results.
305
+ #
306
+ # ==== Parameters
307
+ #
308
+ # objects...<Object>::
309
+ # Objects to remove from the index (may pass an array or varargs)
310
+ #
311
+ # ==== Example
312
+ #
313
+ # post.destroy
314
+ # Sunspot.remove(post)
315
+ #
316
+ def remove(*objects)
317
+ session.remove(*objects)
318
+ end
319
+
320
+ #
321
+ # Remove objects from the index and immediately commit. See Sunspot.remove
322
+ #
323
+ # ==== Parameters
324
+ #
325
+ # objects...<Object>:: Objects to remove from the index
326
+ #
327
+ def remove!(*objects)
328
+ session.remove!(*objects)
329
+ end
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
+
355
+ # Remove all objects of the given classes from the index. There isn't much
356
+ # use for this in general operations but it can be useful for maintenance,
357
+ # testing, etc. If no arguments are passed, remove everything from the
358
+ # index.
359
+ #
360
+ # ==== Parameters
361
+ #
362
+ # classes...<Class>::
363
+ # classes for which to remove all instances from the index (may pass an
364
+ # array or varargs)
365
+ #
366
+ # ==== Example
367
+ #
368
+ # Sunspot.remove_all(Post, Blog)
369
+ #
370
+ def remove_all(*classes)
371
+ session.remove_all(*classes)
372
+ end
373
+
374
+ #
375
+ # Remove all objects of the given classes from the index and immediately
376
+ # commit. See Sunspot.remove_all
377
+ #
378
+ # ==== Parameters
379
+ #
380
+ # classes...<Class>::
381
+ # classes for which to remove all instances from the index
382
+ def remove_all!(*classes)
383
+ session.remove_all!(*classes)
384
+ end
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
+
407
+ #
408
+ # True if documents have been added, updated, or removed since the last
409
+ # commit.
410
+ #
411
+ # ==== Returns
412
+ #
413
+ # Boolean:: Whether there have been any updates since the last commit
414
+ #
415
+ def dirty?
416
+ session.dirty?
417
+ end
418
+
419
+ #
420
+ # Sends a commit if the session is dirty (see #dirty?).
421
+ #
422
+ def commit_if_dirty
423
+ session.commit_if_dirty
424
+ end
425
+
426
+ #
427
+ # True if documents have been removed since the last commit.
428
+ #
429
+ # ==== Returns
430
+ #
431
+ # Boolean:: Whether there have been any deletes since the last commit
432
+ #
433
+ def delete_dirty?
434
+ session.delete_dirty?
435
+ end
436
+
437
+ #
438
+ # Sends a commit if the session has deletes since the last commit (see #delete_dirty?).
439
+ #
440
+ def commit_if_delete_dirty
441
+ session.commit_if_delete_dirty
442
+ end
443
+
444
+ # Returns the configuration associated with the singleton session. See
445
+ # Sunspot::Configuration for details.
446
+ #
447
+ # ==== Returns
448
+ #
449
+ # LightConfig::Configuration:: configuration for singleton session
450
+ #
451
+ def config
452
+ session.config
453
+ end
454
+
455
+ #
456
+ # Resets the singleton session. This is useful for clearing out all
457
+ # static data between tests, but probably nowhere else.
458
+ #
459
+ # ==== Parameters
460
+ #
461
+ # keep_config<Boolean>::
462
+ # Whether to retain the configuration used by the current singleton
463
+ # session. Default false.
464
+ #
465
+ def reset!(keep_config = false)
466
+ config =
467
+ if keep_config
468
+ session.config
469
+ else
470
+ Configuration.build
471
+ end
472
+ @session = Session.new(config)
473
+ end
474
+
475
+ private
476
+
477
+ #
478
+ # Get the singleton session, creating it if none yet exists.
479
+ #
480
+ # ==== Returns
481
+ #
482
+ # Sunspot::Session:: the singleton session
483
+ #
484
+ def session #:nodoc:
485
+ @session ||= Session.new
486
+ end
487
+ end
488
+ end