lannon-lunr 2.0.10
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +5 -0
- data/.rvmrc +1 -0
- data/CHANGELOG.rdoc +59 -0
- data/Gemfile +16 -0
- data/Gemfile.lock +37 -0
- data/README.rdoc +168 -0
- data/Rakefile +52 -0
- data/VERSION +1 -0
- data/lannon-lunr.gemspec +98 -0
- data/lib/lunr.rb +12 -0
- data/lib/lunr/errors.rb +19 -0
- data/lib/lunr/model.rb +21 -0
- data/lib/lunr/model/klass.rb +74 -0
- data/lib/lunr/search.rb +126 -0
- data/lib/lunr/sunspot.rb +57 -0
- data/test/helper.rb +6 -0
- data/test/test_lunr.rb +12 -0
- data/test/test_lunr_model.rb +7 -0
- data/test/test_lunr_search.rb +59 -0
- metadata +268 -0
data/.autotest
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm gemset use lunr
|
data/CHANGELOG.rdoc
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
=== 2.0.9 / 2011-03-15
|
2
|
+
|
3
|
+
* Bundler & Jeweler added, Isolate removed
|
4
|
+
|
5
|
+
=== 2.0.8 / 2010-09-13
|
6
|
+
|
7
|
+
* Store pagination and total info in default Search JSON.
|
8
|
+
|
9
|
+
=== 2.0.8 / 2010-09-13
|
10
|
+
|
11
|
+
* Add a default Lunr::Search#as_json.
|
12
|
+
|
13
|
+
=== 2.0.7 / 2010-09-11
|
14
|
+
|
15
|
+
* Stop freezing, it's annoying.
|
16
|
+
|
17
|
+
=== 2.0.6 / 2010-09-09
|
18
|
+
|
19
|
+
* Improve booleans. Fix Sunspot's cast, add property? sugar method.
|
20
|
+
* Add Search#results (a faster to_a).
|
21
|
+
|
22
|
+
=== 2.0.5 / 2010-09-08
|
23
|
+
|
24
|
+
* Add default as_json method (just returns backing store).
|
25
|
+
|
26
|
+
=== 2.0.4 / 2010-09-07
|
27
|
+
|
28
|
+
* Unbreak model instantiation, switch to Hash backing store.
|
29
|
+
|
30
|
+
=== 2.0.3 / 2010-09-07
|
31
|
+
|
32
|
+
* Cleanups, squash warnings.
|
33
|
+
* Improve the README a bit.
|
34
|
+
* Improve search and scope examples.
|
35
|
+
* Remove unnecessary monkeypatch.
|
36
|
+
|
37
|
+
=== 2.0.2 / 2010-09-02
|
38
|
+
|
39
|
+
* Fix Rakefile typo.
|
40
|
+
* Add ModelClass.first search helper.
|
41
|
+
* Require Ruby >= 1.8.7.
|
42
|
+
* Tighten Sunspot dep to = 1.1.0.
|
43
|
+
* Improve the README a bit.
|
44
|
+
|
45
|
+
=== 2.0.1 / 2010-09-02
|
46
|
+
|
47
|
+
* Clean up model and search.
|
48
|
+
|
49
|
+
=== 2.0.0 / 2010-09-01
|
50
|
+
|
51
|
+
* Reboot.
|
52
|
+
|
53
|
+
=== 1.0.1 / 2010-08-25
|
54
|
+
|
55
|
+
* Handle missing :p value correctly.
|
56
|
+
|
57
|
+
=== 1.0.0 / 2010-08-25
|
58
|
+
|
59
|
+
* Birthday!
|
data/Gemfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
gem "activesupport", "2.3.4"
|
5
|
+
gem "activerecord", "2.3.4"
|
6
|
+
gem "sunspot", "1.2.1"
|
7
|
+
|
8
|
+
# Add dependencies to develop your gem here.
|
9
|
+
# Include everything needed to run rake, tests, features, etc.
|
10
|
+
group :development do
|
11
|
+
gem "bundler", "1.0.10"
|
12
|
+
gem "jeweler", "1.5.2"
|
13
|
+
gem "fakeweb", "1.3.0"
|
14
|
+
gem "shoulda", "2.11.3"
|
15
|
+
gem "mocha", "0.9.12"
|
16
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activerecord (2.3.4)
|
5
|
+
activesupport (= 2.3.4)
|
6
|
+
activesupport (2.3.4)
|
7
|
+
builder (3.0.0)
|
8
|
+
escape (0.0.4)
|
9
|
+
fakeweb (1.3.0)
|
10
|
+
git (1.2.5)
|
11
|
+
jeweler (1.5.2)
|
12
|
+
bundler (~> 1.0.0)
|
13
|
+
git (>= 1.2.5)
|
14
|
+
rake
|
15
|
+
mocha (0.9.12)
|
16
|
+
pr_geohash (1.0.0)
|
17
|
+
rake (0.8.7)
|
18
|
+
rsolr (0.12.1)
|
19
|
+
builder (>= 2.1.2)
|
20
|
+
shoulda (2.11.3)
|
21
|
+
sunspot (1.2.1)
|
22
|
+
escape (= 0.0.4)
|
23
|
+
pr_geohash (~> 1.0)
|
24
|
+
rsolr (= 0.12.1)
|
25
|
+
|
26
|
+
PLATFORMS
|
27
|
+
ruby
|
28
|
+
|
29
|
+
DEPENDENCIES
|
30
|
+
activerecord (= 2.3.4)
|
31
|
+
activesupport (= 2.3.4)
|
32
|
+
bundler (= 1.0.10)
|
33
|
+
fakeweb (= 1.3.0)
|
34
|
+
jeweler (= 1.5.2)
|
35
|
+
mocha (= 0.9.12)
|
36
|
+
shoulda (= 2.11.3)
|
37
|
+
sunspot (= 1.2.1)
|
data/README.rdoc
ADDED
@@ -0,0 +1,168 @@
|
|
1
|
+
= Lunr
|
2
|
+
|
3
|
+
* http://github.com/jbarnette/lunr
|
4
|
+
|
5
|
+
== Description
|
6
|
+
|
7
|
+
A simple read-only interface to Solr, built on Sunspot.
|
8
|
+
|
9
|
+
Lunr makes it easy to query and create objects from a Sunspot-managed
|
10
|
+
Solr index without requiring all the knowledge, code, and data used to
|
11
|
+
build the index in the first place. If you have complex indexes with a
|
12
|
+
stored fields and need to search / access those fields without access
|
13
|
+
to the original data store, Lunr might be what you're looking for.
|
14
|
+
|
15
|
+
== A Lunr Model
|
16
|
+
|
17
|
+
require "lunr"
|
18
|
+
|
19
|
+
class My::SimpleTrack
|
20
|
+
include Lunr::Model
|
21
|
+
|
22
|
+
searches "Track" do
|
23
|
+
property :album, :text
|
24
|
+
property :artist, :text
|
25
|
+
property :title, :text
|
26
|
+
|
27
|
+
time :accepted_at
|
28
|
+
boolean :hot
|
29
|
+
string :state
|
30
|
+
end
|
31
|
+
|
32
|
+
scope do
|
33
|
+
order_by :hot, :desc
|
34
|
+
order_by :accepted_at, :desc
|
35
|
+
|
36
|
+
with :state, "accepted"
|
37
|
+
end
|
38
|
+
|
39
|
+
scope :hot do
|
40
|
+
with :hot, true
|
41
|
+
end
|
42
|
+
|
43
|
+
scope :state do |q, state|
|
44
|
+
q.with :state, state
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
=== Including the Module
|
49
|
+
|
50
|
+
All model classes must <code>include Lunr::Model</code>.
|
51
|
+
|
52
|
+
=== Define the Search Index
|
53
|
+
|
54
|
+
Time to tell Solr what this class searches, and which fields are
|
55
|
+
properties. The DSL in here is exactly the same as Sunspot's, with the
|
56
|
+
addition of a <code>property</code> method. Calling
|
57
|
+
<code>property</code> tells Lunr that you expect access to this field,
|
58
|
+
generates an <code>attr_reader</code> and passes it along to Sunspot's
|
59
|
+
DSL as <code>:stored => true</code>.
|
60
|
+
|
61
|
+
searches "Track" do
|
62
|
+
property :album, :text
|
63
|
+
property :artist, :text
|
64
|
+
property :title, :text
|
65
|
+
|
66
|
+
time :accepted_at
|
67
|
+
boolean :hot
|
68
|
+
string :state
|
69
|
+
end
|
70
|
+
|
71
|
+
The <code>searches</code> method also takes an optional type name,
|
72
|
+
which can be used to map Lunr model types to differing search
|
73
|
+
results. In this case, <code>My::SimpleTrack</code> will search
|
74
|
+
documents with an original type of <code>Track</code>.
|
75
|
+
|
76
|
+
List every criteria you might want to search by, but omit any of
|
77
|
+
Sunspot's DSL that involves indexing.
|
78
|
+
|
79
|
+
=== Default Scope
|
80
|
+
|
81
|
+
Call <code>scope</code> with no name to set default filtering,
|
82
|
+
ordering, and pagination properties.
|
83
|
+
|
84
|
+
scope do
|
85
|
+
order_by :hot, :desc
|
86
|
+
order_by :accepted_at, :desc
|
87
|
+
|
88
|
+
with :state, "accepted"
|
89
|
+
end
|
90
|
+
|
91
|
+
=== Named Scopes
|
92
|
+
|
93
|
+
You can define any number of named scopes to make searching
|
94
|
+
simpler. They're made availble as static methods on the model class,
|
95
|
+
and they're chainable when searching. See the "Searching" section
|
96
|
+
below.
|
97
|
+
|
98
|
+
scope :hot do
|
99
|
+
with :hot, true
|
100
|
+
end
|
101
|
+
|
102
|
+
Scope blocks without parameters are <code>instance_eval</code>ed in
|
103
|
+
the Sunspot query DSL's context. If your scope takes parameters, the
|
104
|
+
first param must be the Sunspot query object.
|
105
|
+
|
106
|
+
scope :state do |q, state|
|
107
|
+
q.with :state, state
|
108
|
+
end
|
109
|
+
|
110
|
+
== Searching
|
111
|
+
|
112
|
+
Use Sunspot's excellent search DSL for ad hoc searches.
|
113
|
+
|
114
|
+
My::SimpleTrack.search
|
115
|
+
My::SimpleTrack.all # same thing
|
116
|
+
|
117
|
+
My::SimpleTrack.search { with :title, "Hello" }
|
118
|
+
My::SimpleTrack.first { with :title, "First!" }
|
119
|
+
|
120
|
+
All searches return a <code>Lunr::Search</code> object, and searches
|
121
|
+
can be narrowed using the Sunspot query DSL or named scopes. Searches
|
122
|
+
aren't run until an <code>Enumerable</code>, arraylike, or pagination
|
123
|
+
method is called on the search object. For convenience,
|
124
|
+
<code>Lunr::Search</code> quacks like
|
125
|
+
<code>WillPaginate::Collection</code>.
|
126
|
+
|
127
|
+
search = My::SimpleTrack.hot.scope { |q| q.fulltext "hello" }
|
128
|
+
p :page => search.page, :total => search.total
|
129
|
+
|
130
|
+
search.each do |track|
|
131
|
+
puts "#{track.title} by #{track.artist} on #{track.album}."
|
132
|
+
end
|
133
|
+
|
134
|
+
== Installation
|
135
|
+
|
136
|
+
$ gem install lunr
|
137
|
+
|
138
|
+
== Development
|
139
|
+
|
140
|
+
Lunr is under pretty heavy development. It was extracted from a Real
|
141
|
+
Application, and barely has any tests of its own. It will be in flux a
|
142
|
+
bit. Contributions and suggestions are very welcome.
|
143
|
+
|
144
|
+
Install the <code>isolate</code> gem and run <code>rake</code>. All
|
145
|
+
dev dependencies will be installed automatically.
|
146
|
+
|
147
|
+
== License
|
148
|
+
|
149
|
+
Copyright 2010 John Barnette (code@jbarnette.com)
|
150
|
+
|
151
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
152
|
+
a copy of this software and associated documentation files (the
|
153
|
+
'Software'), to deal in the Software without restriction, including
|
154
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
155
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
156
|
+
permit persons to whom the Software is furnished to do so, subject to
|
157
|
+
the following conditions:
|
158
|
+
|
159
|
+
The above copyright notice and this permission notice shall be
|
160
|
+
included in all copies or substantial portions of the Software.
|
161
|
+
|
162
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
163
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
164
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
165
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
166
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
167
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
168
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "lannon-lunr"
|
16
|
+
gem.homepage = "http://github.com/lannon/lunr"
|
17
|
+
gem.summary = %Q{A simple read-only interface to Solr, built on Sunspot}
|
18
|
+
gem.description = %Q{A simple read-only interface to Solr, built on Sunspot.
|
19
|
+
Lunr makes it easy to query and create objects from a Sunspot-managed
|
20
|
+
Solr index without requiring all the knowledge, code, and data used to
|
21
|
+
build the index in the first place. If you have complex indexes with a
|
22
|
+
stored fields and need to search / access those fields without access
|
23
|
+
to the original data store, Lunr might be what you're looking for.}
|
24
|
+
gem.email = "code@jbarnette.com"
|
25
|
+
gem.authors = ["John Barnette"]
|
26
|
+
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
27
|
+
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
|
28
|
+
gem.add_runtime_dependency 'activesupport', '2.3.4'
|
29
|
+
gem.add_runtime_dependency 'activerecord', '2.3.4'
|
30
|
+
gem.add_runtime_dependency 'sunspot', '~> 1.2'
|
31
|
+
# gem.add_development_dependency 'rspec', '> 1.2.3'
|
32
|
+
end
|
33
|
+
Jeweler::RubygemsDotOrgTasks.new
|
34
|
+
|
35
|
+
require 'rake/testtask'
|
36
|
+
Rake::TestTask.new(:test) do |test|
|
37
|
+
test.libs << 'lib' << 'test'
|
38
|
+
test.pattern = 'test/**/test_*.rb'
|
39
|
+
test.verbose = true
|
40
|
+
end
|
41
|
+
|
42
|
+
task :default => :test
|
43
|
+
|
44
|
+
require 'rake/rdoctask'
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
46
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
47
|
+
|
48
|
+
rdoc.rdoc_dir = 'rdoc'
|
49
|
+
rdoc.title = "lunr #{version}"
|
50
|
+
rdoc.rdoc_files.include('README*')
|
51
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
52
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.0.10
|
data/lannon-lunr.gemspec
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{lannon-lunr}
|
8
|
+
s.version = "2.0.10"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["John Barnette"]
|
12
|
+
s.date = %q{2011-03-16}
|
13
|
+
s.description = %q{A simple read-only interface to Solr, built on Sunspot.
|
14
|
+
Lunr makes it easy to query and create objects from a Sunspot-managed
|
15
|
+
Solr index without requiring all the knowledge, code, and data used to
|
16
|
+
build the index in the first place. If you have complex indexes with a
|
17
|
+
stored fields and need to search / access those fields without access
|
18
|
+
to the original data store, Lunr might be what you're looking for.}
|
19
|
+
s.email = %q{code@jbarnette.com}
|
20
|
+
s.extra_rdoc_files = [
|
21
|
+
"README.rdoc"
|
22
|
+
]
|
23
|
+
s.files = [
|
24
|
+
".autotest",
|
25
|
+
".rvmrc",
|
26
|
+
"CHANGELOG.rdoc",
|
27
|
+
"Gemfile",
|
28
|
+
"Gemfile.lock",
|
29
|
+
"README.rdoc",
|
30
|
+
"Rakefile",
|
31
|
+
"VERSION",
|
32
|
+
"lannon-lunr.gemspec",
|
33
|
+
"lib/lunr.rb",
|
34
|
+
"lib/lunr/errors.rb",
|
35
|
+
"lib/lunr/model.rb",
|
36
|
+
"lib/lunr/model/klass.rb",
|
37
|
+
"lib/lunr/search.rb",
|
38
|
+
"lib/lunr/sunspot.rb",
|
39
|
+
"test/helper.rb",
|
40
|
+
"test/test_lunr.rb",
|
41
|
+
"test/test_lunr_model.rb",
|
42
|
+
"test/test_lunr_search.rb"
|
43
|
+
]
|
44
|
+
s.homepage = %q{http://github.com/lannon/lunr}
|
45
|
+
s.require_paths = ["lib"]
|
46
|
+
s.rubygems_version = %q{1.3.7}
|
47
|
+
s.summary = %q{A simple read-only interface to Solr, built on Sunspot}
|
48
|
+
s.test_files = [
|
49
|
+
"test/helper.rb",
|
50
|
+
"test/test_lunr.rb",
|
51
|
+
"test/test_lunr_model.rb",
|
52
|
+
"test/test_lunr_search.rb"
|
53
|
+
]
|
54
|
+
|
55
|
+
if s.respond_to? :specification_version then
|
56
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
57
|
+
s.specification_version = 3
|
58
|
+
|
59
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
60
|
+
s.add_runtime_dependency(%q<activesupport>, ["= 2.3.4"])
|
61
|
+
s.add_runtime_dependency(%q<activerecord>, ["= 2.3.4"])
|
62
|
+
s.add_runtime_dependency(%q<sunspot>, ["= 1.2.1"])
|
63
|
+
s.add_development_dependency(%q<bundler>, ["= 1.0.10"])
|
64
|
+
s.add_development_dependency(%q<jeweler>, ["= 1.5.2"])
|
65
|
+
s.add_development_dependency(%q<fakeweb>, ["= 1.3.0"])
|
66
|
+
s.add_development_dependency(%q<shoulda>, ["= 2.11.3"])
|
67
|
+
s.add_development_dependency(%q<mocha>, ["= 0.9.12"])
|
68
|
+
s.add_runtime_dependency(%q<activesupport>, ["= 2.3.4"])
|
69
|
+
s.add_runtime_dependency(%q<activerecord>, ["= 2.3.4"])
|
70
|
+
s.add_runtime_dependency(%q<sunspot>, ["~> 1.2"])
|
71
|
+
else
|
72
|
+
s.add_dependency(%q<activesupport>, ["= 2.3.4"])
|
73
|
+
s.add_dependency(%q<activerecord>, ["= 2.3.4"])
|
74
|
+
s.add_dependency(%q<sunspot>, ["= 1.2.1"])
|
75
|
+
s.add_dependency(%q<bundler>, ["= 1.0.10"])
|
76
|
+
s.add_dependency(%q<jeweler>, ["= 1.5.2"])
|
77
|
+
s.add_dependency(%q<fakeweb>, ["= 1.3.0"])
|
78
|
+
s.add_dependency(%q<shoulda>, ["= 2.11.3"])
|
79
|
+
s.add_dependency(%q<mocha>, ["= 0.9.12"])
|
80
|
+
s.add_dependency(%q<activesupport>, ["= 2.3.4"])
|
81
|
+
s.add_dependency(%q<activerecord>, ["= 2.3.4"])
|
82
|
+
s.add_dependency(%q<sunspot>, ["~> 1.2"])
|
83
|
+
end
|
84
|
+
else
|
85
|
+
s.add_dependency(%q<activesupport>, ["= 2.3.4"])
|
86
|
+
s.add_dependency(%q<activerecord>, ["= 2.3.4"])
|
87
|
+
s.add_dependency(%q<sunspot>, ["= 1.2.1"])
|
88
|
+
s.add_dependency(%q<bundler>, ["= 1.0.10"])
|
89
|
+
s.add_dependency(%q<jeweler>, ["= 1.5.2"])
|
90
|
+
s.add_dependency(%q<fakeweb>, ["= 1.3.0"])
|
91
|
+
s.add_dependency(%q<shoulda>, ["= 2.11.3"])
|
92
|
+
s.add_dependency(%q<mocha>, ["= 0.9.12"])
|
93
|
+
s.add_dependency(%q<activesupport>, ["= 2.3.4"])
|
94
|
+
s.add_dependency(%q<activerecord>, ["= 2.3.4"])
|
95
|
+
s.add_dependency(%q<sunspot>, ["~> 1.2"])
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
data/lib/lunr.rb
ADDED
data/lib/lunr/errors.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
module Lunr
|
2
|
+
class Error < StandardError
|
3
|
+
end
|
4
|
+
|
5
|
+
class AlreadyExecuted < Error
|
6
|
+
attr_reader :search
|
7
|
+
|
8
|
+
def initalize search
|
9
|
+
@search = search
|
10
|
+
super "Can't add more criteria, this search has already been executed!"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class BadModel < Error
|
15
|
+
def initialize klass
|
16
|
+
super "#{klass.name} doesn't include Lunr::Model!"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/lunr/model.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
require "lunr/search"
|
2
|
+
require "lunr/sunspot"
|
3
|
+
|
4
|
+
module Lunr
|
5
|
+
module Model
|
6
|
+
def self.included klass
|
7
|
+
klass.extend Klass
|
8
|
+
end
|
9
|
+
|
10
|
+
module Klass
|
11
|
+
def create hit
|
12
|
+
hash = { :id => hit.primary_key }
|
13
|
+
|
14
|
+
properties.each do |name, type|
|
15
|
+
value = hit.stored name
|
16
|
+
|
17
|
+
if Array === value && value.length == 1 && type == :text
|
18
|
+
# For text fields, which always appear to be multiple.
|
19
|
+
value = value.first
|
20
|
+
end
|
21
|
+
|
22
|
+
hash[name] = value
|
23
|
+
end
|
24
|
+
|
25
|
+
new hash
|
26
|
+
end
|
27
|
+
|
28
|
+
def first &block
|
29
|
+
search(&block).first
|
30
|
+
end
|
31
|
+
|
32
|
+
def properties
|
33
|
+
@properties ||= {}
|
34
|
+
end
|
35
|
+
|
36
|
+
def scopes
|
37
|
+
@scopes ||= {}
|
38
|
+
end
|
39
|
+
|
40
|
+
def scope name = :all, &block
|
41
|
+
scopes[name] = block
|
42
|
+
|
43
|
+
unless name == :all
|
44
|
+
class_eval <<-END, __FILE__, __LINE__ + 1
|
45
|
+
def self.#{name}
|
46
|
+
search.#{name}
|
47
|
+
end
|
48
|
+
END
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def search &block
|
53
|
+
Lunr::Search.new self, &block
|
54
|
+
end
|
55
|
+
|
56
|
+
alias_method :all, :search
|
57
|
+
|
58
|
+
def searches classname = nil, &block
|
59
|
+
Sunspot::TypeField.alias self, classname if classname
|
60
|
+
Sunspot.setup self, &block
|
61
|
+
|
62
|
+
properties.each do |name, type|
|
63
|
+
class_eval <<-END, __FILE__, __LINE__ + 1
|
64
|
+
def #{name}
|
65
|
+
@hash[#{name.inspect}]
|
66
|
+
end
|
67
|
+
END
|
68
|
+
|
69
|
+
alias_method "#{name}?", name if type == :boolean
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
data/lib/lunr/search.rb
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
require "lunr/errors"
|
2
|
+
require "lunr/model"
|
3
|
+
require "lunr/sunspot"
|
4
|
+
|
5
|
+
module Lunr
|
6
|
+
class Search
|
7
|
+
include Enumerable
|
8
|
+
|
9
|
+
attr_reader :klass
|
10
|
+
|
11
|
+
def initialize klass, &block
|
12
|
+
raise Lunr::BadModel.new(klass) unless klass < Lunr::Model
|
13
|
+
|
14
|
+
@executed = false
|
15
|
+
@klass = klass
|
16
|
+
@search = Sunspot.new_search klass
|
17
|
+
|
18
|
+
all = @klass.scopes[:all]
|
19
|
+
|
20
|
+
scope(&all) if all
|
21
|
+
scope(&block) if block_given?
|
22
|
+
end
|
23
|
+
|
24
|
+
def as_json options = nil
|
25
|
+
{}.tap do |j|
|
26
|
+
j[:page] = page
|
27
|
+
j[:total] = total
|
28
|
+
|
29
|
+
key = options.delete(:key) if Hash === options
|
30
|
+
j[key || :entries] = map { |e| e.as_json options }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def each &block
|
35
|
+
execute && @results.each(&block)
|
36
|
+
end
|
37
|
+
|
38
|
+
def empty?
|
39
|
+
0 == total
|
40
|
+
end
|
41
|
+
|
42
|
+
def executable!
|
43
|
+
raise Lunr::AlreadyExecuted.new(self) if executed?
|
44
|
+
end
|
45
|
+
|
46
|
+
def executed?
|
47
|
+
@executed
|
48
|
+
end
|
49
|
+
|
50
|
+
def method_missing name, *args
|
51
|
+
return super unless scope = klass.scopes[name]
|
52
|
+
|
53
|
+
executable!
|
54
|
+
|
55
|
+
dsl = @search.send :dsl
|
56
|
+
|
57
|
+
if args.empty?
|
58
|
+
dsl.instance_eval(&scope)
|
59
|
+
else
|
60
|
+
scope.call dsl, args
|
61
|
+
end
|
62
|
+
|
63
|
+
self
|
64
|
+
end
|
65
|
+
|
66
|
+
def page
|
67
|
+
@page ||= execute && @search.query.page
|
68
|
+
end
|
69
|
+
|
70
|
+
def pages
|
71
|
+
@pages ||= total / per +
|
72
|
+
((total_entries % per_page) > 0 ? 1 : 0)
|
73
|
+
end
|
74
|
+
|
75
|
+
def params
|
76
|
+
@search.query.to_params
|
77
|
+
end
|
78
|
+
|
79
|
+
def per
|
80
|
+
@per ||= execute && @search.query.per_page
|
81
|
+
end
|
82
|
+
|
83
|
+
def respond_to name, include_private = false
|
84
|
+
klass.scopes.key?(name) || super
|
85
|
+
end
|
86
|
+
|
87
|
+
def results
|
88
|
+
execute && @results
|
89
|
+
end
|
90
|
+
|
91
|
+
def scope &block
|
92
|
+
executable!
|
93
|
+
@search.build(&block)
|
94
|
+
|
95
|
+
self
|
96
|
+
end
|
97
|
+
|
98
|
+
def total
|
99
|
+
@total ||= execute && @search.total
|
100
|
+
end
|
101
|
+
|
102
|
+
alias_method :size, :total
|
103
|
+
|
104
|
+
# Quack like WillPaginate::Collection
|
105
|
+
|
106
|
+
alias_method :current_page, :page
|
107
|
+
alias_method :per_page, :per
|
108
|
+
alias_method :total_entries, :total
|
109
|
+
alias_method :total_pages, :pages
|
110
|
+
|
111
|
+
private
|
112
|
+
|
113
|
+
def execute
|
114
|
+
unless executed?
|
115
|
+
@executed = true
|
116
|
+
@search.execute
|
117
|
+
|
118
|
+
@results = @search.hits.map do |hit|
|
119
|
+
klass.create hit
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
true
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
data/lib/lunr/sunspot.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require "sunspot"
|
2
|
+
|
3
|
+
module Sunspot
|
4
|
+
module DSL
|
5
|
+
class Fields
|
6
|
+
def property name, type, options = {}
|
7
|
+
@setup.clazz.properties[name] = type
|
8
|
+
send type, name, options.merge(:stored => true)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module Search
|
14
|
+
class Hit
|
15
|
+
alias_method :original_initialize, :initialize
|
16
|
+
|
17
|
+
def initialize *args
|
18
|
+
original_initialize(*args)
|
19
|
+
|
20
|
+
if clazz = Sunspot::TypeField.aliases_inverted[@class_name]
|
21
|
+
@class_name = clazz.name
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
module Type
|
28
|
+
class BooleanType
|
29
|
+
def cast thing
|
30
|
+
thing == "true" ? true : thing == "false" ? false : !!thing
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class TypeField
|
36
|
+
class << self
|
37
|
+
def alias(dest_class, source_class_name)
|
38
|
+
@@inverted = nil # invalidate cache
|
39
|
+
aliases[dest_class] = source_class_name
|
40
|
+
end
|
41
|
+
|
42
|
+
def aliases
|
43
|
+
@@aliases ||= {}
|
44
|
+
end
|
45
|
+
|
46
|
+
def aliases_inverted
|
47
|
+
@@inverted ||= aliases.invert
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
alias_method :old_to_indexed, :to_indexed
|
52
|
+
|
53
|
+
def to_indexed clazz
|
54
|
+
self.class.aliases[clazz] || clazz.name
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/test/helper.rb
ADDED
data/test/test_lunr.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require "helper"
|
2
|
+
require "mocha"
|
3
|
+
|
4
|
+
class TestLunrSearch < Test::Unit::TestCase
|
5
|
+
|
6
|
+
# FIXME: ugly.
|
7
|
+
class ::Searchable
|
8
|
+
include Lunr::Model
|
9
|
+
end
|
10
|
+
|
11
|
+
Sunspot.setup ::Searchable do
|
12
|
+
string :bar
|
13
|
+
string :foo
|
14
|
+
end
|
15
|
+
|
16
|
+
context "A Searchable model" do
|
17
|
+
|
18
|
+
setup do
|
19
|
+
@searchable = ::Searchable
|
20
|
+
@search = Lunr::Search.new @searchable do
|
21
|
+
with :foo, "bar"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
should "respond to search" do
|
26
|
+
puts @searchable.respond_to?(:search)
|
27
|
+
end
|
28
|
+
|
29
|
+
should "have some stuff" do
|
30
|
+
|
31
|
+
assert_equal @searchable, @search.klass
|
32
|
+
assert_equal "type:Searchable", @search.params[:fq].first
|
33
|
+
assert @search.params[:fq].include?("foo_s:bar")
|
34
|
+
end
|
35
|
+
|
36
|
+
should "have scopes" do
|
37
|
+
|
38
|
+
@search.scope { with :foo, "blergh" }
|
39
|
+
@search.scope { with :bar, "corge" }
|
40
|
+
|
41
|
+
assert @search.params[:fq].include?("foo_s:blergh")
|
42
|
+
assert @search.params[:fq].include?("bar_s:corge")
|
43
|
+
end
|
44
|
+
|
45
|
+
should "only execute search once" do
|
46
|
+
@search.stubs(:executed?).returns true
|
47
|
+
|
48
|
+
assert_raises Lunr::AlreadyExecuted do
|
49
|
+
@search.scope { with :foo, "hello" }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
|
metadata
ADDED
@@ -0,0 +1,268 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lannon-lunr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
- 10
|
10
|
+
version: 2.0.10
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- John Barnette
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-03-16 00:00:00 -04:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
prerelease: false
|
23
|
+
type: :runtime
|
24
|
+
name: activesupport
|
25
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - "="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 11
|
31
|
+
segments:
|
32
|
+
- 2
|
33
|
+
- 3
|
34
|
+
- 4
|
35
|
+
version: 2.3.4
|
36
|
+
requirement: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
prerelease: false
|
39
|
+
type: :runtime
|
40
|
+
name: activerecord
|
41
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - "="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
hash: 11
|
47
|
+
segments:
|
48
|
+
- 2
|
49
|
+
- 3
|
50
|
+
- 4
|
51
|
+
version: 2.3.4
|
52
|
+
requirement: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
prerelease: false
|
55
|
+
type: :runtime
|
56
|
+
name: sunspot
|
57
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - "="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
hash: 29
|
63
|
+
segments:
|
64
|
+
- 1
|
65
|
+
- 2
|
66
|
+
- 1
|
67
|
+
version: 1.2.1
|
68
|
+
requirement: *id003
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
prerelease: false
|
71
|
+
type: :development
|
72
|
+
name: bundler
|
73
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - "="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
hash: 3
|
79
|
+
segments:
|
80
|
+
- 1
|
81
|
+
- 0
|
82
|
+
- 10
|
83
|
+
version: 1.0.10
|
84
|
+
requirement: *id004
|
85
|
+
- !ruby/object:Gem::Dependency
|
86
|
+
prerelease: false
|
87
|
+
type: :development
|
88
|
+
name: jeweler
|
89
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - "="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
hash: 7
|
95
|
+
segments:
|
96
|
+
- 1
|
97
|
+
- 5
|
98
|
+
- 2
|
99
|
+
version: 1.5.2
|
100
|
+
requirement: *id005
|
101
|
+
- !ruby/object:Gem::Dependency
|
102
|
+
prerelease: false
|
103
|
+
type: :development
|
104
|
+
name: fakeweb
|
105
|
+
version_requirements: &id006 !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - "="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
hash: 27
|
111
|
+
segments:
|
112
|
+
- 1
|
113
|
+
- 3
|
114
|
+
- 0
|
115
|
+
version: 1.3.0
|
116
|
+
requirement: *id006
|
117
|
+
- !ruby/object:Gem::Dependency
|
118
|
+
prerelease: false
|
119
|
+
type: :development
|
120
|
+
name: shoulda
|
121
|
+
version_requirements: &id007 !ruby/object:Gem::Requirement
|
122
|
+
none: false
|
123
|
+
requirements:
|
124
|
+
- - "="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
hash: 37
|
127
|
+
segments:
|
128
|
+
- 2
|
129
|
+
- 11
|
130
|
+
- 3
|
131
|
+
version: 2.11.3
|
132
|
+
requirement: *id007
|
133
|
+
- !ruby/object:Gem::Dependency
|
134
|
+
prerelease: false
|
135
|
+
type: :development
|
136
|
+
name: mocha
|
137
|
+
version_requirements: &id008 !ruby/object:Gem::Requirement
|
138
|
+
none: false
|
139
|
+
requirements:
|
140
|
+
- - "="
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
hash: 35
|
143
|
+
segments:
|
144
|
+
- 0
|
145
|
+
- 9
|
146
|
+
- 12
|
147
|
+
version: 0.9.12
|
148
|
+
requirement: *id008
|
149
|
+
- !ruby/object:Gem::Dependency
|
150
|
+
prerelease: false
|
151
|
+
type: :runtime
|
152
|
+
name: activesupport
|
153
|
+
version_requirements: &id009 !ruby/object:Gem::Requirement
|
154
|
+
none: false
|
155
|
+
requirements:
|
156
|
+
- - "="
|
157
|
+
- !ruby/object:Gem::Version
|
158
|
+
hash: 11
|
159
|
+
segments:
|
160
|
+
- 2
|
161
|
+
- 3
|
162
|
+
- 4
|
163
|
+
version: 2.3.4
|
164
|
+
requirement: *id009
|
165
|
+
- !ruby/object:Gem::Dependency
|
166
|
+
prerelease: false
|
167
|
+
type: :runtime
|
168
|
+
name: activerecord
|
169
|
+
version_requirements: &id010 !ruby/object:Gem::Requirement
|
170
|
+
none: false
|
171
|
+
requirements:
|
172
|
+
- - "="
|
173
|
+
- !ruby/object:Gem::Version
|
174
|
+
hash: 11
|
175
|
+
segments:
|
176
|
+
- 2
|
177
|
+
- 3
|
178
|
+
- 4
|
179
|
+
version: 2.3.4
|
180
|
+
requirement: *id010
|
181
|
+
- !ruby/object:Gem::Dependency
|
182
|
+
prerelease: false
|
183
|
+
type: :runtime
|
184
|
+
name: sunspot
|
185
|
+
version_requirements: &id011 !ruby/object:Gem::Requirement
|
186
|
+
none: false
|
187
|
+
requirements:
|
188
|
+
- - ~>
|
189
|
+
- !ruby/object:Gem::Version
|
190
|
+
hash: 11
|
191
|
+
segments:
|
192
|
+
- 1
|
193
|
+
- 2
|
194
|
+
version: "1.2"
|
195
|
+
requirement: *id011
|
196
|
+
description: |-
|
197
|
+
A simple read-only interface to Solr, built on Sunspot.
|
198
|
+
Lunr makes it easy to query and create objects from a Sunspot-managed
|
199
|
+
Solr index without requiring all the knowledge, code, and data used to
|
200
|
+
build the index in the first place. If you have complex indexes with a
|
201
|
+
stored fields and need to search / access those fields without access
|
202
|
+
to the original data store, Lunr might be what you're looking for.
|
203
|
+
email: code@jbarnette.com
|
204
|
+
executables: []
|
205
|
+
|
206
|
+
extensions: []
|
207
|
+
|
208
|
+
extra_rdoc_files:
|
209
|
+
- README.rdoc
|
210
|
+
files:
|
211
|
+
- .autotest
|
212
|
+
- .rvmrc
|
213
|
+
- CHANGELOG.rdoc
|
214
|
+
- Gemfile
|
215
|
+
- Gemfile.lock
|
216
|
+
- README.rdoc
|
217
|
+
- Rakefile
|
218
|
+
- VERSION
|
219
|
+
- lannon-lunr.gemspec
|
220
|
+
- lib/lunr.rb
|
221
|
+
- lib/lunr/errors.rb
|
222
|
+
- lib/lunr/model.rb
|
223
|
+
- lib/lunr/model/klass.rb
|
224
|
+
- lib/lunr/search.rb
|
225
|
+
- lib/lunr/sunspot.rb
|
226
|
+
- test/helper.rb
|
227
|
+
- test/test_lunr.rb
|
228
|
+
- test/test_lunr_model.rb
|
229
|
+
- test/test_lunr_search.rb
|
230
|
+
has_rdoc: true
|
231
|
+
homepage: http://github.com/lannon/lunr
|
232
|
+
licenses: []
|
233
|
+
|
234
|
+
post_install_message:
|
235
|
+
rdoc_options: []
|
236
|
+
|
237
|
+
require_paths:
|
238
|
+
- lib
|
239
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
240
|
+
none: false
|
241
|
+
requirements:
|
242
|
+
- - ">="
|
243
|
+
- !ruby/object:Gem::Version
|
244
|
+
hash: 3
|
245
|
+
segments:
|
246
|
+
- 0
|
247
|
+
version: "0"
|
248
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
249
|
+
none: false
|
250
|
+
requirements:
|
251
|
+
- - ">="
|
252
|
+
- !ruby/object:Gem::Version
|
253
|
+
hash: 3
|
254
|
+
segments:
|
255
|
+
- 0
|
256
|
+
version: "0"
|
257
|
+
requirements: []
|
258
|
+
|
259
|
+
rubyforge_project:
|
260
|
+
rubygems_version: 1.3.7
|
261
|
+
signing_key:
|
262
|
+
specification_version: 3
|
263
|
+
summary: A simple read-only interface to Solr, built on Sunspot
|
264
|
+
test_files:
|
265
|
+
- test/helper.rb
|
266
|
+
- test/test_lunr.rb
|
267
|
+
- test/test_lunr_model.rb
|
268
|
+
- test/test_lunr_search.rb
|