robsharp-sunspot_rails 1.1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. data/History.txt +40 -0
  2. data/LICENSE +18 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +256 -0
  5. data/Rakefile +27 -0
  6. data/TODO +8 -0
  7. data/VERSION.yml +4 -0
  8. data/dev_tasks/gemspec.rake +33 -0
  9. data/dev_tasks/rdoc.rake +24 -0
  10. data/dev_tasks/release.rake +4 -0
  11. data/dev_tasks/todo.rake +4 -0
  12. data/generators/sunspot/sunspot_generator.rb +9 -0
  13. data/generators/sunspot/templates/sunspot.yml +18 -0
  14. data/install.rb +1 -0
  15. data/lib/sunspot/rails.rb +58 -0
  16. data/lib/sunspot/rails/adapters.rb +160 -0
  17. data/lib/sunspot/rails/configuration.rb +272 -0
  18. data/lib/sunspot/rails/request_lifecycle.rb +31 -0
  19. data/lib/sunspot/rails/searchable.rb +464 -0
  20. data/lib/sunspot/rails/server.rb +173 -0
  21. data/lib/sunspot/rails/solr_logging.rb +58 -0
  22. data/lib/sunspot/rails/spec_helper.rb +19 -0
  23. data/lib/sunspot/rails/stub_session_proxy.rb +88 -0
  24. data/lib/sunspot/rails/tasks.rb +62 -0
  25. data/lib/sunspot/rails/version.rb +5 -0
  26. data/rails/init.rb +10 -0
  27. data/spec/configuration_spec.rb +102 -0
  28. data/spec/mock_app/app/controllers/application.rb +10 -0
  29. data/spec/mock_app/app/controllers/application_controller.rb +10 -0
  30. data/spec/mock_app/app/controllers/posts_controller.rb +6 -0
  31. data/spec/mock_app/app/models/author.rb +8 -0
  32. data/spec/mock_app/app/models/blog.rb +12 -0
  33. data/spec/mock_app/app/models/location.rb +2 -0
  34. data/spec/mock_app/app/models/photo_post.rb +2 -0
  35. data/spec/mock_app/app/models/post.rb +10 -0
  36. data/spec/mock_app/app/models/post_with_auto.rb +10 -0
  37. data/spec/mock_app/config/boot.rb +110 -0
  38. data/spec/mock_app/config/database.yml +4 -0
  39. data/spec/mock_app/config/environment.rb +42 -0
  40. data/spec/mock_app/config/environments/development.rb +27 -0
  41. data/spec/mock_app/config/environments/test.rb +27 -0
  42. data/spec/mock_app/config/initializers/new_rails_defaults.rb +19 -0
  43. data/spec/mock_app/config/initializers/session_store.rb +15 -0
  44. data/spec/mock_app/config/routes.rb +43 -0
  45. data/spec/mock_app/config/sunspot.yml +19 -0
  46. data/spec/mock_app/db/schema.rb +27 -0
  47. data/spec/model_lifecycle_spec.rb +63 -0
  48. data/spec/model_spec.rb +409 -0
  49. data/spec/request_lifecycle_spec.rb +52 -0
  50. data/spec/schema.rb +27 -0
  51. data/spec/server_spec.rb +36 -0
  52. data/spec/session_spec.rb +24 -0
  53. data/spec/spec_helper.rb +51 -0
  54. data/spec/stub_session_proxy_spec.rb +122 -0
  55. metadata +170 -0
@@ -0,0 +1,40 @@
1
+ == 1.0.4 2010-03-19
2
+ * Fix StubSessionProxy to match new internal API
3
+
4
+ == 1.0.2 2010-03-11
5
+ * Allow use of ActiveRecord object for coordinates extraction
6
+ * Works with non-standard primary keys
7
+ * Add :include, :select options to Searchable::search()
8
+ * Allow specification of min_memory and max_memory in config/sunspot.yml
9
+ * Searchable now more considerate of model namespaces
10
+
11
+ == 1.0.1 2010-03-05
12
+ * Fix Solr logging compatibility with RSolr 0.12.1
13
+
14
+ == 1.0.0 2010-03-03
15
+ * Sunspot::Rails::Server subclasses Sunspot::Server; no longer needs to shell out
16
+ * Use Sunspot built-in session proxies in Sunspot::Rails
17
+ * Sunspot::Rails adapter compatible with assumed inconsistency
18
+ * Use session proxy for disconnecting Sunspot in specs
19
+ * Allow specification of first ID for reindex batches.
20
+ * Log Solr requests in Rails logger
21
+ * Use class_inheritable_hash for sunspot_options
22
+ * Add `index` class method, which is like `reindex` but doesn't clear first
23
+ * Rename sunspot:solr:reindex to sunspot:reindex
24
+ * Use Sunspot::Installer to bootstrap Rails Solr home
25
+
26
+ == 0.10.7
27
+ * Added Sunspot::Rails::Server class to start/stop/run/bootstrap the solr server
28
+ * Added log_level key to sunspot.yml, see java docs for valid values (http://java.sun.com/j2se/1.5.0/docs/api/java/util/logging/Level.html)
29
+ * Added log_file key to sunspot.yml. This defaults to RAILS_ROOT/log/solr_<environment>.log.
30
+ * Added support for localsolr in sunspot_rails. You can add custom extension.jar files to your Rails.root/solr/lib directory
31
+ * Added an option to not reindex an object when certain columns have changed.
32
+
33
+ == 0.10.6
34
+ * Added script/generate sunspot support to generate the required sunspot.yml
35
+ file [Brandon Keepers]
36
+
37
+ == 0.10.5
38
+ * Added a auto_commit_after_request option to sunspot.yml. Sunspot will not
39
+ automatically commit any changes in solr if you set this value to false.
40
+ Strongly encouraged for production environment.
data/LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ Permission is hereby granted, free of charge, to any person obtaining
2
+ a copy of this software and associated documentation files (the
3
+ 'Software'), to deal in the Software without restriction, including
4
+ without limitation the rights to use, copy, modify, merge, publish,
5
+ distribute, sublicense, and/or sell copies of the Software, and to
6
+ permit persons to whom the Software is furnished to do so, subject to
7
+ the following conditions:
8
+
9
+ The above copyright notice and this permission notice shall be
10
+ included in all copies or substantial portions of the Software.
11
+
12
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
13
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
15
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
16
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
17
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
18
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Mat Brown
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,256 @@
1
+ = Sunspot::Rails
2
+
3
+ Sunspot::Rails is a Rails plugin that provides drop-in integration of the
4
+ Sunspot[http://outoftime.github.com/sunspot] Solr search library with Rails. It
5
+ provides the following features:
6
+
7
+ * Configure Sunspot using config/sunspot.yml
8
+ * Extend ActiveRecord for easy index configuration, search, and indexing
9
+ * Automatically index ActiveRecord objects when they are saved, and remove them
10
+ from the index when they are destroyed (can be disabled)
11
+ * Automatically commit Solr changes at the end of each request (can be disabled)
12
+ * Provide utility methods to find and fix orphaned documents and rebuild the
13
+ Solr index for a given class
14
+ * Provide rake tasks for starting and stopping the development Solr instance,
15
+ using the configuration in sunspot.yml
16
+
17
+ Sunspot::Rails has been tested with Rails versions 2.1, 2.2, and 2.3
18
+
19
+ == Installation
20
+
21
+ For recent versions of Rails, In your project's <code>config/environment.rb</code>, add the following gem dependencies:
22
+
23
+ config.gem 'sunspot', :lib => 'sunspot'
24
+ config.gem 'sunspot_rails', :lib => 'sunspot/rails'
25
+
26
+ Install the gems with:
27
+
28
+ rake gems:install
29
+
30
+ If you are using an older version of Rails that doesn't support plugins-as-gems, install the gems manually and install the plugin:
31
+
32
+ sudo gem install sunspot sunspot_rails --source=http://gems.github.com
33
+
34
+ script/plugin install git://github.com/outoftime/sunspot_rails.git
35
+
36
+ Generate the file <code>config/sunspot.yml</code>:
37
+
38
+ script/generate sunspot
39
+
40
+ Rails doesn't automatically load rake tasks from plugins installed as gems
41
+ (https://rails.lighthouseapp.com/projects/8994/tickets/59). If you installed
42
+ Sunspot::Rails as a gem, add the following line to your project's Rakefile:
43
+
44
+ require 'sunspot/rails/tasks'
45
+
46
+ If you wish to make modifications to the Solr schema, you can create a custom
47
+ Solr home in your project directory. In order to do so, create the directory
48
+ <code>RAILS_ROOT/solr/conf</code>, and copy in the contents of the Solr gem's
49
+ <code>solr/solr/conf</code> directory. If the files are in the right place,
50
+ Sunspot::Rails will detect them and tell Solr to use your local configurations.
51
+ Use caution when modifying <code>schema.xml</code> - Sunspot relies on the
52
+ field naming scheme in the packaged schema file.
53
+
54
+ To start up a Solr instance, issue the following:
55
+
56
+ rake sunspot:solr:start
57
+
58
+ Note that using the built-in Solr instance packaged with Sunspot is great for
59
+ development, but is not recommended for production. See the Sunspot
60
+ documentation for more information.
61
+
62
+ == Usage
63
+
64
+ === Setup
65
+
66
+ In order for an ActiveRecord model to be indexable and searchable, it must be
67
+ configured for search. For example:
68
+
69
+ class Post < ActiveRecord::Base
70
+ searchable do
71
+ text :title, :body
72
+ integer :blog_id
73
+ time :updated_at
74
+ string :sort_title do
75
+ title.downcase.sub(/^(an?|the) /, '')
76
+ end
77
+ end
78
+ end
79
+
80
+ See the documentation for Sunspot.setup for full details on what can go in the
81
+ configuration block.
82
+
83
+ === Indexing
84
+
85
+ By default, models are indexed whenever they are saved, and removed from the
86
+ index whenever they are destroyed. This behavior can be disabled:
87
+
88
+ class Post < ActiveRecord::Base
89
+ searchable :auto_index => false, :auto_remove => false do
90
+ # setup...
91
+ end
92
+ end
93
+
94
+ Note that <b>using the <code>:auto_remove</code> option is not recommended
95
+ </b>, as destroying an object without removing it from the index will
96
+ create an orphaned document in the index, which is a Bad Thing. Turning off
97
+ <code>:auto_index</code> is perfectly safe if you prefer to manage indexing manually
98
+ (perhaps using a background job).
99
+
100
+ If you have disabled lifecycle indexing hooks, you can invoke indexing
101
+ operations directly on your model:
102
+
103
+ post = Post.create
104
+ post.index
105
+ post.remove_from_index
106
+
107
+ === Committing
108
+
109
+ When data is changed in Solr, it is initially stored in memory and not made
110
+ available to the currently running searcher instance. Issuing a +commit+ to Solr
111
+ will cause it to write the changes to disk, and instantiate a new searcher
112
+ instance. This operation is fairly expensive, so rather than issuing a commit
113
+ every time a document is added or removed, Sunspot::Rails issues a commit at
114
+ the end of any request where data has been added to or removed from Solr. If
115
+ you need to immediately issue a commit, bang!-versions of the methods are
116
+ available:
117
+
118
+ post = Post.create
119
+ post.index!
120
+ # this is the same as...
121
+ post.index
122
+ Sunspot.commit
123
+
124
+ When writing tests outside of the context of a controller request, you will want
125
+ to use one of these two approaches.
126
+
127
+ === Searching
128
+
129
+ Do it like this:
130
+
131
+ Post.search do
132
+ with :blog_id, 1
133
+ with(:updated_at).greater_than(Time.now - 2.weeks)
134
+ order :sort_title, :asc
135
+ paginate :page => 1, :per_page => 15
136
+ end
137
+
138
+ See the documentation for <code>Sunspot.search</code> for all the options
139
+ available in the search block, and the information available in the result
140
+ block.
141
+
142
+ === Searching for IDs
143
+
144
+ In some situations, you may want to get the IDs for models returned by a search
145
+ without actually loading the models out of the database. For that, you can
146
+ call +search_ids+, using the same block format as #search. This will return an
147
+ array of IDs.
148
+
149
+
150
+ === Searching for multiple types
151
+
152
+ Sunspot is entirely agnostic about whether searches are for one or more types;
153
+ the only restriction is that columns used for restriction, ordering, etc. are
154
+ defined in the same way for all types being searched. Sunspot::Rails does not
155
+ provide any additional support for this, since there is not anything useful to
156
+ be added, so just use the interface provided by Sunspot:
157
+
158
+ Sunspot.search(Post, Comment) do
159
+ with :blog_id, 1
160
+ order :created_at, :asc
161
+ end
162
+
163
+ Be sure to check out the Sunspot documentation for all the details.
164
+
165
+ === Adding search functionality in mixins
166
+
167
+ Sunspot does not require that search setup for a given class happen all in one
168
+ place; it is perfectly acceptable to call the <code>Sunspot.setup</code> method
169
+ more than once. This capability is particularly useful for adding search
170
+ functionality in mixins. For instance, if you have a +Ratable+ module, you may
171
+ wish to add additional search fields for searchable classes that mix in that
172
+ module. For example:
173
+
174
+ module Ratable
175
+ def self.included(base)
176
+ if base.searchable?
177
+ base.searchable do
178
+ float :average_rating do
179
+ ratings.average(:value)
180
+ end
181
+ end
182
+ end
183
+ end
184
+ end
185
+
186
+ Note the use of <code>base.searchable?</code> - this ensures that only classes
187
+ that already have search enabled will have the additional configuration added.
188
+ The above pattern requires that the class be declared searchable before the
189
+ module is mixed in; other patterns (such as passing a :searchable option to an
190
+ acts_as_-style declaration) may be more flexible.
191
+
192
+ === Utility methods
193
+
194
+ If you need to completely reindex all of the instances of a given model class,
195
+ you can issue:
196
+
197
+ Post.reindex
198
+
199
+ If for some reason models get deleted from the database, but not from the index,
200
+ they will become index orphans - not a good situation. To get IDs that exist in
201
+ the index but not the database, you can use the +index_orphans+ method; to
202
+ remove those documents from the index, use +clean_index_orphans+. Note that
203
+ neither of these operations should be needed if Sunspot and Sunspot::Rails are
204
+ used as intended.
205
+
206
+
207
+ == Testing Solr integration using RSpec
208
+
209
+ To disable the sunspot-solr integration for your active record models, require
210
+ the file `sunspot/rails/spec_helper`
211
+
212
+ Then, in your spec, use the #disconnect_sunspot method:
213
+
214
+ require 'sunspot/rails/spec_helper'
215
+
216
+ describe Post do
217
+ disconnect_sunspot
218
+
219
+ it 'should have some behavior'
220
+ # ...
221
+ end
222
+ end
223
+
224
+ In all of the examples in this group, all Sunspot calls will be stubbed out. The
225
+ Sunspot#search method will return a stub search object that mimics a search with
226
+ no results.
227
+
228
+ == Further Reading
229
+
230
+ Reading the {Sunspot documentation}[http://outoftime.github.com/sunspot/docs] is
231
+ highly recommended. Sunspot::Rails exists to wrap Sunspot with a Rails-friendly
232
+ API, but almost all of the functionality you use in Sunspot::Rails is
233
+ implemented in Sunspot.
234
+
235
+ Posts about Sunspot on my blog are available at http://outofti.me/tagged/sunspot
236
+
237
+ == Bugs
238
+
239
+ Please submit bug reports to
240
+ http://outoftime.lighthouseapp.com/projects/20339-sunspot
241
+
242
+ == Contributors
243
+
244
+ - Mat Brown (mat@patch.com)
245
+ - Peer Allan (peer.allan@gmail.com)
246
+ - Michael Moen (michael@underpantsgnome.com)
247
+ - Benjamin Krause (bk@benjaminkrause.com)
248
+ - Adam Salter (adam@codebright.net)
249
+ - Brandon Keepers (brandon@opensoul.org)
250
+ - Paul Canavese (paul@canavese.org)
251
+ - John Eberly (jeberly@elctech.com)
252
+ - Gert Thiel (gertthiel@gmail.com)
253
+
254
+ == License
255
+
256
+ Sunspot::Rails is distributed under the MIT License, copyright (c) 2009 Mat Brown
@@ -0,0 +1,27 @@
1
+ require 'rake'
2
+ require 'spec/rake/spectask'
3
+ require 'rake/rdoctask'
4
+
5
+ task :default => :spec
6
+
7
+ if File.exist?(sunspot_lib = File.expand_path(File.join(File.dirname(__FILE__), '..', 'sunspot', 'lib')))
8
+ STDERR.puts("Using sunspot lib at #{sunspot_lib}")
9
+ $: << sunspot_lib
10
+ end
11
+
12
+ desc 'Run all specs'
13
+ Spec::Rake::SpecTask.new(:spec) do |t|
14
+ t.spec_files = FileList['spec/*_spec.rb']
15
+ t.spec_opts << '--color'
16
+ end
17
+
18
+ task :environment do
19
+ if ENV['SUNSPOT_LIB']
20
+ $: << ENV['SUNSPOT_LIB']
21
+ end
22
+ ENV['RAILS_ROOT'] ||= File.join(File.dirname(__FILE__), 'spec', 'mock_app')
23
+ ENV['RAILS_ENV'] ||= 'test'
24
+ require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config', 'environment.rb'))
25
+ end
26
+
27
+ FileList['dev_tasks/*.rake', 'lib/sunspot/rails/tasks.rb'].each { |file| load(file) }
data/TODO ADDED
@@ -0,0 +1,8 @@
1
+ * Delegate new_search method
2
+ =======
3
+ * Fix final status output for reindex
4
+ * Add batch-per-request option
5
+ * Optionally yield from reindex
6
+ * Access to all searchable classes
7
+ * Add a method to disable sunspot updates for certain values, e.g.
8
+ if you just touch a record, sunspot shouldn't index the
@@ -0,0 +1,4 @@
1
+ ---
2
+ :patch: 4
3
+ :major: 0
4
+ :minor: 11
@@ -0,0 +1,33 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'tools', 'gem_tasks')
2
+
3
+ Sunspot::GemTasks.new do |s|
4
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'sunspot', 'rails', 'version')
5
+
6
+ s.name = 'robsharp-sunspot_rails'
7
+ s.version = '1.1.0.2'
8
+ s.summary = 'Rails integration for the Sunspot Solr search library'
9
+ s.email = 'rob@sharp.id.au'
10
+ s.homepage = 'http://github.com/outoftime/sunspot_rails'
11
+ s.description = <<TEXT
12
+ Sunspot::Rails is an extension to the Sunspot library for Solr search.
13
+ Sunspot::Rails adds integration between Sunspot and ActiveRecord, including
14
+ defining search and indexing related methods on ActiveRecord models themselves,
15
+ running a Sunspot-compatible Solr instance for development and test
16
+ environments, and automatically commit Solr index changes at the end of each
17
+ Rails request.
18
+ TEXT
19
+ s.authors = ['Mat Brown', 'Peer Allan', 'Michael Moen', 'Benjamin Krause', 'Adam Salter', 'Brandon Keepers', 'Paul Canavese', 'John Eberly', 'Gert Thiel']
20
+ s.rubyforge_project = 'sunspot'
21
+ s.files = FileList['[A-Z]*',
22
+ '{lib,tasks,dev_tasks}/**/*',
23
+ 'generators/**/*',
24
+ 'install.rb',
25
+ 'MIT-LICENSE',
26
+ 'rails/*',
27
+ 'spec/*.rb',
28
+ 'spec/mock_app/{app,lib,db,vendor,config}/**/*',
29
+ 'spec/mock_app/{tmp,log,solr}']
30
+ s.add_dependency 'sunspot', Sunspot::Rails::VERSION
31
+ s.add_development_dependency 'rspec', '~> 1.2'
32
+ s.add_development_dependency 'rspec-rails', '~> 1.2'
33
+ end
@@ -0,0 +1,24 @@
1
+ begin
2
+ require 'hanna/rdoctask'
3
+ rescue LoadError
4
+ if require 'rubygems'
5
+ retry
6
+ end
7
+ # It's OK if hanna isn't installed.
8
+ end
9
+
10
+ Rake::RDocTask.new(:doc) do |rdoc|
11
+ rdoc.main = 'README.rdoc'
12
+ rdoc.rdoc_files.include('README.rdoc', 'lib/sunspot/rails/**/*.rb', 'lib/sunspot/rails.rb')
13
+ rdoc.rdoc_dir = 'doc'
14
+ end
15
+
16
+ namespace :doc do
17
+ desc 'Generate rdoc and move into pages directory'
18
+ task :publish => :redoc do
19
+ doc_dir = File.join(File.dirname(__FILE__), '..', 'doc')
20
+ publish_dir = File.join(File.dirname(__FILE__), '..', '..', 'pages', 'rails', 'docs')
21
+ FileUtils.rm_rf(publish_dir) if File.exist?(publish_dir)
22
+ FileUtils.cp_r(doc_dir, publish_dir)
23
+ end
24
+ end
@@ -0,0 +1,4 @@
1
+ namespace :release do
2
+ desc 'Release gem on RubyForge and GitHub'
3
+ task :all => [:release, :"rubyforge:release:gem"]
4
+ end