lunr 2.0.2 → 2.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +7 -0
- data/README.rdoc +20 -17
- data/lib/lunr.rb +1 -1
- data/lib/lunr/model.rb +1 -1
- data/lib/lunr/model/klass.rb +27 -7
- data/lib/lunr/search.rb +14 -27
- data/lib/lunr/sunspot.rb +3 -7
- metadata +9 -11
data/CHANGELOG.rdoc
CHANGED
data/README.rdoc
CHANGED
@@ -6,13 +6,11 @@
|
|
6
6
|
|
7
7
|
A simple read-only interface to Solr, built on Sunspot.
|
8
8
|
|
9
|
-
Lunr makes it easy to query and create objects from a
|
10
|
-
without requiring all the knowledge, code, and data
|
11
|
-
build the index in the first place.
|
12
|
-
|
13
|
-
|
14
|
-
access those fields without access to the original data store, Lunr
|
15
|
-
might be what you're looking for.
|
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.
|
16
14
|
|
17
15
|
== A Lunr Model
|
18
16
|
|
@@ -31,11 +29,11 @@ might be what you're looking for.
|
|
31
29
|
string :state
|
32
30
|
end
|
33
31
|
|
34
|
-
scope do
|
35
|
-
|
36
|
-
|
32
|
+
scope do
|
33
|
+
order_by :hot, :desc
|
34
|
+
order_by :accepted_at, :desc
|
37
35
|
|
38
|
-
|
36
|
+
with :state, "accepted"
|
39
37
|
end
|
40
38
|
|
41
39
|
scope :hot do
|
@@ -83,11 +81,11 @@ Sunspot's DSL that involves indexing.
|
|
83
81
|
Call <code>scope</code> with no name to set default filtering,
|
84
82
|
ordering, and pagination properties.
|
85
83
|
|
86
|
-
scope do
|
87
|
-
|
88
|
-
|
84
|
+
scope do
|
85
|
+
order_by :hot, :desc
|
86
|
+
order_by :accepted_at, :desc
|
89
87
|
|
90
|
-
|
88
|
+
with :state, "accepted"
|
91
89
|
end
|
92
90
|
|
93
91
|
=== Named Scopes
|
@@ -111,9 +109,14 @@ first param must be the Sunspot query object.
|
|
111
109
|
|
112
110
|
== Searching
|
113
111
|
|
112
|
+
Use Sunspot's excellent search DSL for ad hock searches.
|
113
|
+
|
114
114
|
My::SimpleTrack.search
|
115
115
|
My::SimpleTrack.all # same thing
|
116
116
|
|
117
|
+
My::SimpleTrack.search { with :title, "Hello" }
|
118
|
+
My::SimpleTrack.first { with :title, "First!" }
|
119
|
+
|
117
120
|
All searches return a <code>Lunr::Search</code> object, and searches
|
118
121
|
can be narrowed using the Sunspot query DSL or named scopes. Searches
|
119
122
|
aren't run until an <code>Enumerable</code>, arraylike, or pagination
|
@@ -134,8 +137,8 @@ method is called on the search object. For convenience,
|
|
134
137
|
|
135
138
|
== Development
|
136
139
|
|
137
|
-
|
138
|
-
|
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
|
139
142
|
bit. Contributions and suggestions are very welcome.
|
140
143
|
|
141
144
|
Install the <code>isolate</code> gem and run <code>rake</code>. All
|
data/lib/lunr.rb
CHANGED
data/lib/lunr/model.rb
CHANGED
data/lib/lunr/model/klass.rb
CHANGED
@@ -8,6 +8,26 @@ module Lunr
|
|
8
8
|
end
|
9
9
|
|
10
10
|
module Klass
|
11
|
+
def create hit
|
12
|
+
new.tap do |instance|
|
13
|
+
instance.id = hit.primary_key
|
14
|
+
|
15
|
+
properties.each do |name, type|
|
16
|
+
value = hit.stored name
|
17
|
+
|
18
|
+
# For text fields, which always appear to be multiple.
|
19
|
+
|
20
|
+
if Array === value && value.length == 1 && type == :text
|
21
|
+
value = value.first
|
22
|
+
end
|
23
|
+
|
24
|
+
instance.send "#{name}=", value
|
25
|
+
end
|
26
|
+
|
27
|
+
instance.freeze
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
11
31
|
def first &block
|
12
32
|
search(&block).first
|
13
33
|
end
|
@@ -20,12 +40,12 @@ module Lunr
|
|
20
40
|
@scopes ||= {}
|
21
41
|
end
|
22
42
|
|
23
|
-
def scope
|
24
|
-
scopes[
|
43
|
+
def scope name = :all, &block
|
44
|
+
scopes[name] = block
|
25
45
|
|
26
|
-
unless
|
46
|
+
unless name == :all
|
27
47
|
class_eval <<-END, __FILE__, __LINE__ + 1
|
28
|
-
def self.#{
|
48
|
+
def self.#{name}; search.#{name} end
|
29
49
|
END
|
30
50
|
end
|
31
51
|
end
|
@@ -36,12 +56,12 @@ module Lunr
|
|
36
56
|
|
37
57
|
alias_method :all, :search
|
38
58
|
|
39
|
-
def searches classname, &block
|
40
|
-
Sunspot::TypeField.alias self, classname
|
59
|
+
def searches classname = nil, &block
|
60
|
+
Sunspot::TypeField.alias self, classname if classname
|
41
61
|
Sunspot.setup self, &block
|
42
62
|
|
43
63
|
properties.each do |name, type|
|
44
|
-
|
64
|
+
attr_accessor name
|
45
65
|
end
|
46
66
|
end
|
47
67
|
end
|
data/lib/lunr/search.rb
CHANGED
@@ -17,8 +17,8 @@ module Lunr
|
|
17
17
|
|
18
18
|
all = @klass.scopes[:all]
|
19
19
|
|
20
|
-
scope
|
21
|
-
scope
|
20
|
+
scope(&all) if all
|
21
|
+
scope(&block) if block_given?
|
22
22
|
end
|
23
23
|
|
24
24
|
def each &block
|
@@ -37,15 +37,15 @@ module Lunr
|
|
37
37
|
@executed
|
38
38
|
end
|
39
39
|
|
40
|
-
def method_missing
|
41
|
-
return super unless scope = klass.scopes[
|
40
|
+
def method_missing name, *args
|
41
|
+
return super unless scope = klass.scopes[name]
|
42
42
|
|
43
43
|
executable!
|
44
44
|
|
45
45
|
dsl = @search.send :dsl
|
46
46
|
|
47
47
|
if args.empty?
|
48
|
-
dsl.instance_eval
|
48
|
+
dsl.instance_eval(&scope)
|
49
49
|
else
|
50
50
|
scope.call dsl, args
|
51
51
|
end
|
@@ -58,8 +58,8 @@ module Lunr
|
|
58
58
|
end
|
59
59
|
|
60
60
|
def pages
|
61
|
-
total / per +
|
62
|
-
(total_entries % per_page > 0 ? 1 : 0)
|
61
|
+
@pages ||= total / per +
|
62
|
+
((total_entries % per_page) > 0 ? 1 : 0)
|
63
63
|
end
|
64
64
|
|
65
65
|
def params
|
@@ -70,13 +70,15 @@ module Lunr
|
|
70
70
|
@per ||= execute && @search.query.per_page
|
71
71
|
end
|
72
72
|
|
73
|
-
def respond_to
|
74
|
-
klass.scopes.key?(
|
73
|
+
def respond_to name, include_private = false
|
74
|
+
klass.scopes.key?(name) || super
|
75
75
|
end
|
76
76
|
|
77
77
|
def scope &block
|
78
78
|
executable!
|
79
|
-
@search.build
|
79
|
+
@search.build(&block)
|
80
|
+
|
81
|
+
self
|
80
82
|
end
|
81
83
|
|
82
84
|
def total
|
@@ -95,27 +97,12 @@ module Lunr
|
|
95
97
|
private
|
96
98
|
|
97
99
|
def execute
|
98
|
-
unless
|
100
|
+
unless executed?
|
99
101
|
@executed = true
|
100
102
|
@search.execute
|
101
103
|
|
102
104
|
@results = @search.hits.map do |hit|
|
103
|
-
|
104
|
-
klass.new.tap do |model|
|
105
|
-
model.instance_variable_set :"@id", hit.primary_key
|
106
|
-
|
107
|
-
klass.properties.each do |name, type|
|
108
|
-
value = hit.stored name
|
109
|
-
|
110
|
-
# For text fields, which always appear to be multiple.
|
111
|
-
|
112
|
-
if Array === value && value.length == 1 && type == :text
|
113
|
-
value = value.first
|
114
|
-
end
|
115
|
-
|
116
|
-
model.instance_variable_set :"@#{name}", value
|
117
|
-
end
|
118
|
-
end
|
105
|
+
klass.create hit
|
119
106
|
end
|
120
107
|
end
|
121
108
|
|
data/lib/lunr/sunspot.rb
CHANGED
@@ -10,18 +10,12 @@ module Sunspot
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
-
class Field
|
14
|
-
def stored?
|
15
|
-
@stored
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
13
|
module Search
|
20
14
|
class Hit
|
21
15
|
alias_method :original_initialize, :initialize
|
22
16
|
|
23
17
|
def initialize *args
|
24
|
-
original_initialize
|
18
|
+
original_initialize(*args)
|
25
19
|
|
26
20
|
if clazz = Sunspot::TypeField.aliases_inverted[@class_name]
|
27
21
|
@class_name = clazz.name
|
@@ -46,6 +40,8 @@ module Sunspot
|
|
46
40
|
end
|
47
41
|
end
|
48
42
|
|
43
|
+
alias_method :old_to_indexed, :to_indexed
|
44
|
+
|
49
45
|
def to_indexed clazz
|
50
46
|
self.class.aliases[clazz] || clazz.name
|
51
47
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lunr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 9
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 2
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 2.0.
|
9
|
+
- 3
|
10
|
+
version: 2.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- John Barnette
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-09-
|
18
|
+
date: 2010-09-07 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -165,13 +165,11 @@ dependencies:
|
|
165
165
|
description: |-
|
166
166
|
A simple read-only interface to Solr, built on Sunspot.
|
167
167
|
|
168
|
-
Lunr makes it easy to query and create objects from a
|
169
|
-
without requiring all the knowledge, code, and data
|
170
|
-
build the index in the first place.
|
171
|
-
|
172
|
-
|
173
|
-
access those fields without access to the original data store, Lunr
|
174
|
-
might be what you're looking for.
|
168
|
+
Lunr makes it easy to query and create objects from a Sunspot-managed
|
169
|
+
Solr index without requiring all the knowledge, code, and data used to
|
170
|
+
build the index in the first place. If you have complex indexes with a
|
171
|
+
stored fields and need to search / access those fields without access
|
172
|
+
to the original data store, Lunr might be what you're looking for.
|
175
173
|
email:
|
176
174
|
- code@jbarnette.com
|
177
175
|
executables: []
|