websolr-sunspot_rails 0.1.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +1 -0
- data/README.rdoc +183 -17
- data/Rakefile +3 -5
- data/VERSION +1 -1
- data/lib/websolr-sunspot_rails.rb +3 -13
- data/websolr-sunspot_rails.gemspec +6 -12
- metadata +5 -23
data/LICENSE
CHANGED
data/README.rdoc
CHANGED
@@ -1,26 +1,192 @@
|
|
1
|
-
|
1
|
+
== About ==
|
2
2
|
|
3
|
-
This
|
4
|
-
WEBSOLR_URL as the Solr Search server, provided that it's defined.
|
3
|
+
This is the gem to install for the supported version of sunspot_rails on websolr.
|
5
4
|
|
6
|
-
|
7
|
-
app by either starting the webapp like:
|
5
|
+
== Installation ==
|
8
6
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
7
|
+
cd your_rails_app
|
8
|
+
sudo gem install websolr-sunspot_rails
|
9
|
+
config.gem "websolr-sunspot_rails" # into config.environment.rb
|
10
|
+
echo websolr-sunspot_rails >> .gems # if using Heroku
|
11
|
+
./script/generate sunspot # Installs config/sunspot.yml
|
12
|
+
require 'sunspot/rails/tasks' >> Rakefile # Installs local development tasks
|
13
13
|
|
14
|
-
|
15
|
-
ENV["WEBSOLR_URL"] = "http://index.websolr.com/solr/<api-key>"
|
14
|
+
== Usage
|
16
15
|
|
17
|
-
|
18
|
-
require "websolr-sunspot_rails"
|
16
|
+
=== Setup
|
19
17
|
|
20
|
-
|
18
|
+
In order for an ActiveRecord model to be indexable and searchable, it must be
|
19
|
+
configured for search. For example:
|
21
20
|
|
22
|
-
|
21
|
+
class Post < ActiveRecord::Base
|
22
|
+
searchable do
|
23
|
+
text :title, :body
|
24
|
+
integer :blog_id
|
25
|
+
time :updated_at
|
26
|
+
string :sort_title do
|
27
|
+
title.downcase.sub(/^(an?|the) /, '')
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
23
31
|
|
24
|
-
|
32
|
+
See the documentation for Sunspot.setup for full details on what can go in the
|
33
|
+
configuration block.
|
25
34
|
|
26
|
-
|
35
|
+
=== Indexing
|
36
|
+
|
37
|
+
By default, models are indexed whenever they are saved, and removed from the
|
38
|
+
index whenever they are destroyed. This behavior can be disabled:
|
39
|
+
|
40
|
+
class Post < ActiveRecord::Base
|
41
|
+
searchable :auto_index => false, :auto_remove => false do
|
42
|
+
# setup...
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
Note that <b>using the <code>:auto_remove</code> option is not recommended
|
47
|
+
</b>, as destroying an object without removing it from the index will
|
48
|
+
create an orphaned document in the index, which is a Bad Thing. Turning off
|
49
|
+
<code>:auto_index</code> is perfectly safe if you prefer to manage indexing manually
|
50
|
+
(perhaps using a background job).
|
51
|
+
|
52
|
+
If you have disabled lifecycle indexing hooks, you can invoke indexing
|
53
|
+
operations directly on your model:
|
54
|
+
|
55
|
+
post = Post.create
|
56
|
+
post.index
|
57
|
+
post.remove_from_index
|
58
|
+
|
59
|
+
=== Committing
|
60
|
+
|
61
|
+
When data is changed in Solr, it is initially stored in memory and not made
|
62
|
+
available to the currently running searcher instance. Issuing a +commit+ to Solr
|
63
|
+
will cause it to write the changes to disk, and instantiate a new searcher
|
64
|
+
instance. This operation is fairly expensive, so rather than issuing a commit
|
65
|
+
every time a document is added or removed, Sunspot::Rails issues a commit at
|
66
|
+
the end of any request where data has been added to or removed from Solr. If
|
67
|
+
you need to immediately issue a commit, bang!-versions of the methods are
|
68
|
+
available:
|
69
|
+
|
70
|
+
post = Post.create
|
71
|
+
post.index!
|
72
|
+
# this is the same as...
|
73
|
+
post.index
|
74
|
+
Sunspot.commit
|
75
|
+
|
76
|
+
When writing tests outside of the context of a controller request, you will want
|
77
|
+
to use one of these two approaches.
|
78
|
+
|
79
|
+
=== Searching
|
80
|
+
|
81
|
+
Do it like this:
|
82
|
+
|
83
|
+
Post.search do
|
84
|
+
with :blog_id, 1
|
85
|
+
with(:updated_at).greater_than(Time.now - 2.weeks)
|
86
|
+
order :sort_title, :asc
|
87
|
+
paginate :page => 1, :per_page => 15
|
88
|
+
end
|
89
|
+
|
90
|
+
See the documentation for <code>Sunspot.search</code> for all the options
|
91
|
+
available in the search block, and the information available in the result
|
92
|
+
block.
|
93
|
+
|
94
|
+
=== Searching for IDs
|
95
|
+
|
96
|
+
In some situations, you may want to get the IDs for models returned by a search
|
97
|
+
without actually loading the models out of the database. For that, you can
|
98
|
+
call +search_ids+, using the same block format as #search. This will return an
|
99
|
+
array of IDs.
|
100
|
+
|
101
|
+
|
102
|
+
=== Searching for multiple types
|
103
|
+
|
104
|
+
Sunspot is entirely agnostic about whether searches are for one or more types;
|
105
|
+
the only restriction is that columns used for restriction, ordering, etc. are
|
106
|
+
defined in the same way for all types being searched. Sunspot::Rails does not
|
107
|
+
provide any additional support for this, since there is not anything useful to
|
108
|
+
be added, so just use the interface provided by Sunspot:
|
109
|
+
|
110
|
+
Sunspot.search(Post, Comment) do
|
111
|
+
with :blog_id, 1
|
112
|
+
order :created_at, :asc
|
113
|
+
end
|
114
|
+
|
115
|
+
Be sure to check out the Sunspot documentation for all the details.
|
116
|
+
|
117
|
+
=== Adding search functionality in mixins
|
118
|
+
|
119
|
+
Sunspot does not require that search setup for a given class happen all in one
|
120
|
+
place; it is perfectly acceptable to call the <code>Sunspot.setup</code> method
|
121
|
+
more than once. This capability is particularly useful for adding search
|
122
|
+
functionality in mixins. For instance, if you have a +Ratable+ module, you may
|
123
|
+
wish to add additional search fields for searchable classes that mix in that
|
124
|
+
module. For example:
|
125
|
+
|
126
|
+
module Ratable
|
127
|
+
def self.included(base)
|
128
|
+
if base.searchable?
|
129
|
+
base.searchable do
|
130
|
+
float :average_rating do
|
131
|
+
ratings.average(:value)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
Note the use of <code>base.searchable?</code> - this ensures that only classes
|
139
|
+
that already have search enabled will have the additional configuration added.
|
140
|
+
The above pattern requires that the class be declared searchable before the
|
141
|
+
module is mixed in; other patterns (such as passing a :searchable option to an
|
142
|
+
acts_as_-style declaration) may be more flexible.
|
143
|
+
|
144
|
+
=== Utility methods
|
145
|
+
|
146
|
+
If you need to completely reindex all of the instances of a given model class,
|
147
|
+
you can issue:
|
148
|
+
|
149
|
+
Post.reindex
|
150
|
+
|
151
|
+
If for some reason models get deleted from the database, but not from the index,
|
152
|
+
they will become index orphans - not a good situation. To get IDs that exist in
|
153
|
+
the index but not the database, you can use the +index_orphans+ method; to
|
154
|
+
remove those documents from the index, use +clean_index_orphans+. Note that
|
155
|
+
neither of these operations should be needed if Sunspot and Sunspot::Rails are
|
156
|
+
used as intended.
|
157
|
+
|
158
|
+
|
159
|
+
== Testing Solr integration using RSpec
|
160
|
+
|
161
|
+
To disable the sunspot-solr integration for your active record models, add the
|
162
|
+
following line to your spec_helper.rb
|
163
|
+
|
164
|
+
require 'sunspot/spec/extension'
|
165
|
+
|
166
|
+
This will disable all automatic after_save/after_destroy solr-requests generated
|
167
|
+
via the #searchable method. This will not disable/mock explicit calls in your code.
|
168
|
+
|
169
|
+
If you want to test the sunspot-solr integration with active record, you can
|
170
|
+
reenable the after_save/after_destroy hooks by adding 'integrate_sunspot' in your
|
171
|
+
examples.
|
172
|
+
|
173
|
+
describe Searches do
|
174
|
+
integrate_sunspot
|
175
|
+
|
176
|
+
before(:each) do
|
177
|
+
@movie = Factory.create :movie
|
178
|
+
end
|
179
|
+
|
180
|
+
it "should find a movie" do
|
181
|
+
Movie.search { keywords @movie.title }.first.should == @movie
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
== Further Reading
|
188
|
+
|
189
|
+
Reading the {Sunspot documentation}[http://outoftime.github.com/sunspot/docs] is
|
190
|
+
highly recommended. Sunspot::Rails exists to wrap Sunspot with a Rails-friendly
|
191
|
+
API, but almost all of the functionality you use in Sunspot::Rails is
|
192
|
+
implemented in Sunspot.
|
data/Rakefile
CHANGED
@@ -9,12 +9,10 @@ begin
|
|
9
9
|
gem.description = %Q{websolr to sunspot_rails shim}
|
10
10
|
gem.email = "kyle@kylemaxwell.com"
|
11
11
|
gem.homepage = "http://github.com/fizx/websolr-sunspot_rails"
|
12
|
-
gem.authors = ["Kyle Maxwell"]
|
13
|
-
gem.add_dependency "
|
14
|
-
gem.add_dependency "sunspot", "=0.10.8"
|
15
|
-
gem.add_dependency "sunspot_rails", "=0.11.5"
|
12
|
+
gem.authors = ["Kyle Maxwell", "John Barnette", "Mat Brown"]
|
13
|
+
gem.add_dependency "sunspot_rails", "0.11.4"
|
16
14
|
gem.add_dependency "rest-client"
|
17
|
-
gem.add_development_dependency "rspec"
|
15
|
+
gem.add_development_dependency "rspec"
|
18
16
|
end
|
19
17
|
Jeweler::GemcutterTasks.new
|
20
18
|
rescue LoadError
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.9.0
|
@@ -1,12 +1,5 @@
|
|
1
|
-
require "rubygems"
|
2
|
-
gem "sunspot", :version => "0.10.8"
|
3
|
-
gem "sunspot_rails", :version => "0.11.5"
|
4
|
-
|
5
|
-
# Post-require hooks and sunspot if WEBSOLR_URL is defined.
|
6
1
|
if ENV["WEBSOLR_URL"]
|
7
2
|
|
8
|
-
CLIENT_KEY = "sunspot-0.10"
|
9
|
-
|
10
3
|
require "rest_client"
|
11
4
|
require "uri"
|
12
5
|
require "sunspot/rails"
|
@@ -15,10 +8,7 @@ if ENV["WEBSOLR_URL"]
|
|
15
8
|
require "sunspot/rails/request_lifecycle"
|
16
9
|
|
17
10
|
api_key = ENV["WEBSOLR_URL"][/[0-9a-f]{11}/] or raise "Invalid WEBSOLR_URL: bad or no api key"
|
18
|
-
|
19
|
-
STDOUT.flush
|
20
|
-
RestClient.post("http://www.websolr.com/schema/#{api_key}", :client => CLIENT_KEY)
|
21
|
-
puts "done"
|
11
|
+
RestClient.post("http://www.websolr.com/schema/#{api_key}", :client => "sunspot-0.10")
|
22
12
|
|
23
13
|
module Sunspot #:nodoc:
|
24
14
|
module Rails #:nodoc:
|
@@ -71,7 +61,7 @@ if ENV["WEBSOLR_URL"]
|
|
71
61
|
end
|
72
62
|
end
|
73
63
|
|
74
|
-
|
64
|
+
# This code makes saves go though even though solr is down.
|
75
65
|
module Sunspot
|
76
66
|
module Rails
|
77
67
|
module Searchable
|
@@ -94,4 +84,4 @@ if ENV["WEBSOLR_URL"]
|
|
94
84
|
end
|
95
85
|
end
|
96
86
|
end
|
97
|
-
end
|
87
|
+
end
|
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{websolr-sunspot_rails}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.9.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = ["Kyle Maxwell"]
|
12
|
-
s.date = %q{2010-
|
11
|
+
s.authors = ["Kyle Maxwell", "John Barnette", "Mat Brown"]
|
12
|
+
s.date = %q{2010-02-02}
|
13
13
|
s.description = %q{websolr to sunspot_rails shim}
|
14
14
|
s.email = %q{kyle@kylemaxwell.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -37,22 +37,16 @@ Gem::Specification.new do |s|
|
|
37
37
|
s.specification_version = 3
|
38
38
|
|
39
39
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
40
|
-
s.add_runtime_dependency(%q<
|
41
|
-
s.add_runtime_dependency(%q<sunspot>, ["= 0.10.8"])
|
42
|
-
s.add_runtime_dependency(%q<sunspot_rails>, ["= 0.11.5"])
|
40
|
+
s.add_runtime_dependency(%q<sunspot_rails>, ["= 0.11.4"])
|
43
41
|
s.add_runtime_dependency(%q<rest-client>, [">= 0"])
|
44
42
|
s.add_development_dependency(%q<rspec>, [">= 0"])
|
45
43
|
else
|
46
|
-
s.add_dependency(%q<
|
47
|
-
s.add_dependency(%q<sunspot>, ["= 0.10.8"])
|
48
|
-
s.add_dependency(%q<sunspot_rails>, ["= 0.11.5"])
|
44
|
+
s.add_dependency(%q<sunspot_rails>, ["= 0.11.4"])
|
49
45
|
s.add_dependency(%q<rest-client>, [">= 0"])
|
50
46
|
s.add_dependency(%q<rspec>, [">= 0"])
|
51
47
|
end
|
52
48
|
else
|
53
|
-
s.add_dependency(%q<
|
54
|
-
s.add_dependency(%q<sunspot>, ["= 0.10.8"])
|
55
|
-
s.add_dependency(%q<sunspot_rails>, ["= 0.11.5"])
|
49
|
+
s.add_dependency(%q<sunspot_rails>, ["= 0.11.4"])
|
56
50
|
s.add_dependency(%q<rest-client>, [">= 0"])
|
57
51
|
s.add_dependency(%q<rspec>, [">= 0"])
|
58
52
|
end
|
metadata
CHANGED
@@ -1,37 +1,19 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: websolr-sunspot_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kyle Maxwell
|
8
|
+
- John Barnette
|
9
|
+
- Mat Brown
|
8
10
|
autorequire:
|
9
11
|
bindir: bin
|
10
12
|
cert_chain: []
|
11
13
|
|
12
|
-
date: 2010-
|
14
|
+
date: 2010-02-02 00:00:00 -08:00
|
13
15
|
default_executable:
|
14
16
|
dependencies:
|
15
|
-
- !ruby/object:Gem::Dependency
|
16
|
-
name: plain_option_parser
|
17
|
-
type: :runtime
|
18
|
-
version_requirement:
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
20
|
-
requirements:
|
21
|
-
- - ">="
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: "0"
|
24
|
-
version:
|
25
|
-
- !ruby/object:Gem::Dependency
|
26
|
-
name: sunspot
|
27
|
-
type: :runtime
|
28
|
-
version_requirement:
|
29
|
-
version_requirements: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - "="
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: 0.10.8
|
34
|
-
version:
|
35
17
|
- !ruby/object:Gem::Dependency
|
36
18
|
name: sunspot_rails
|
37
19
|
type: :runtime
|
@@ -40,7 +22,7 @@ dependencies:
|
|
40
22
|
requirements:
|
41
23
|
- - "="
|
42
24
|
- !ruby/object:Gem::Version
|
43
|
-
version: 0.11.
|
25
|
+
version: 0.11.4
|
44
26
|
version:
|
45
27
|
- !ruby/object:Gem::Dependency
|
46
28
|
name: rest-client
|