benjaminkrause-sunspot 0.9.7
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +107 -0
- data/LICENSE +18 -0
- data/README.rdoc +159 -0
- data/Rakefile +9 -0
- data/TODO +11 -0
- data/VERSION.yml +4 -0
- data/bin/sunspot-configure-solr +46 -0
- data/bin/sunspot-solr +86 -0
- data/lib/light_config.rb +40 -0
- data/lib/sunspot/adapters.rb +265 -0
- data/lib/sunspot/composite_setup.rb +184 -0
- data/lib/sunspot/configuration.rb +49 -0
- data/lib/sunspot/data_extractor.rb +50 -0
- data/lib/sunspot/dsl/field_query.rb +77 -0
- data/lib/sunspot/dsl/fields.rb +95 -0
- data/lib/sunspot/dsl/fulltext.rb +106 -0
- data/lib/sunspot/dsl/query.rb +107 -0
- data/lib/sunspot/dsl/query_facet.rb +31 -0
- data/lib/sunspot/dsl/restriction.rb +25 -0
- data/lib/sunspot/dsl/scope.rb +193 -0
- data/lib/sunspot/dsl/search.rb +30 -0
- data/lib/sunspot/dsl.rb +4 -0
- data/lib/sunspot/facet.rb +24 -0
- data/lib/sunspot/facet_data.rb +152 -0
- data/lib/sunspot/facet_row.rb +12 -0
- data/lib/sunspot/field.rb +148 -0
- data/lib/sunspot/field_factory.rb +141 -0
- data/lib/sunspot/indexer.rb +129 -0
- data/lib/sunspot/instantiated_facet.rb +45 -0
- data/lib/sunspot/instantiated_facet_row.rb +27 -0
- data/lib/sunspot/query/base_query.rb +55 -0
- data/lib/sunspot/query/boost_query.rb +20 -0
- data/lib/sunspot/query/connective.rb +148 -0
- data/lib/sunspot/query/dynamic_query.rb +61 -0
- data/lib/sunspot/query/field_facet.rb +129 -0
- data/lib/sunspot/query/field_query.rb +69 -0
- data/lib/sunspot/query/fulltext_base_query.rb +86 -0
- data/lib/sunspot/query/highlighting.rb +36 -0
- data/lib/sunspot/query/local.rb +24 -0
- data/lib/sunspot/query/pagination.rb +39 -0
- data/lib/sunspot/query/query_facet.rb +78 -0
- data/lib/sunspot/query/query_facet_row.rb +19 -0
- data/lib/sunspot/query/query_field_facet.rb +20 -0
- data/lib/sunspot/query/restriction.rb +272 -0
- data/lib/sunspot/query/scope.rb +185 -0
- data/lib/sunspot/query/sort.rb +105 -0
- data/lib/sunspot/query/sort_composite.rb +33 -0
- data/lib/sunspot/query/text_field_boost.rb +15 -0
- data/lib/sunspot/query.rb +108 -0
- data/lib/sunspot/schema.rb +147 -0
- data/lib/sunspot/search/highlight.rb +38 -0
- data/lib/sunspot/search/hit.rb +113 -0
- data/lib/sunspot/search.rb +240 -0
- data/lib/sunspot/session.rb +206 -0
- data/lib/sunspot/setup.rb +312 -0
- data/lib/sunspot/text_field_setup.rb +29 -0
- data/lib/sunspot/type.rb +200 -0
- data/lib/sunspot/util.rb +190 -0
- data/lib/sunspot.rb +459 -0
- data/solr/etc/jetty.xml +212 -0
- data/solr/etc/webdefault.xml +379 -0
- data/solr/lib/jetty-6.1.3.jar +0 -0
- data/solr/lib/jetty-util-6.1.3.jar +0 -0
- data/solr/lib/jsp-2.1/ant-1.6.5.jar +0 -0
- data/solr/lib/jsp-2.1/core-3.1.1.jar +0 -0
- data/solr/lib/jsp-2.1/jsp-2.1.jar +0 -0
- data/solr/lib/jsp-2.1/jsp-api-2.1.jar +0 -0
- data/solr/lib/servlet-api-2.5-6.1.3.jar +0 -0
- data/solr/solr/conf/elevate.xml +36 -0
- data/solr/solr/conf/protwords.txt +21 -0
- data/solr/solr/conf/schema.xml +64 -0
- data/solr/solr/conf/solrconfig.xml +726 -0
- data/solr/solr/conf/stopwords.txt +57 -0
- data/solr/solr/conf/synonyms.txt +31 -0
- data/solr/start.jar +0 -0
- data/solr/webapps/solr.war +0 -0
- data/spec/api/adapters_spec.rb +33 -0
- data/spec/api/indexer/attributes_spec.rb +100 -0
- data/spec/api/indexer/batch_spec.rb +46 -0
- data/spec/api/indexer/dynamic_fields_spec.rb +33 -0
- data/spec/api/indexer/fixed_fields_spec.rb +57 -0
- data/spec/api/indexer/fulltext_spec.rb +43 -0
- data/spec/api/indexer/removal_spec.rb +46 -0
- data/spec/api/indexer/spec_helper.rb +1 -0
- data/spec/api/indexer_spec.rb +4 -0
- data/spec/api/query/connectives_spec.rb +161 -0
- data/spec/api/query/dsl_spec.rb +12 -0
- data/spec/api/query/dynamic_fields_spec.rb +148 -0
- data/spec/api/query/faceting_spec.rb +272 -0
- data/spec/api/query/fulltext_spec.rb +152 -0
- data/spec/api/query/highlighting_spec.rb +82 -0
- data/spec/api/query/local_spec.rb +37 -0
- data/spec/api/query/ordering_pagination_spec.rb +95 -0
- data/spec/api/query/scope_spec.rb +253 -0
- data/spec/api/query/spec_helper.rb +1 -0
- data/spec/api/query/text_field_scoping_spec.rb +30 -0
- data/spec/api/query/types_spec.rb +20 -0
- data/spec/api/search/dynamic_fields_spec.rb +27 -0
- data/spec/api/search/faceting_spec.rb +206 -0
- data/spec/api/search/highlighting_spec.rb +65 -0
- data/spec/api/search/hits_spec.rb +62 -0
- data/spec/api/search/results_spec.rb +52 -0
- data/spec/api/search/search_spec.rb +11 -0
- data/spec/api/search/spec_helper.rb +1 -0
- data/spec/api/session_spec.rb +157 -0
- data/spec/api/spec_helper.rb +1 -0
- data/spec/api/sunspot_spec.rb +18 -0
- data/spec/helpers/indexer_helper.rb +29 -0
- data/spec/helpers/query_helper.rb +13 -0
- data/spec/helpers/search_helper.rb +78 -0
- data/spec/integration/dynamic_fields_spec.rb +55 -0
- data/spec/integration/faceting_spec.rb +169 -0
- data/spec/integration/highlighting_spec.rb +22 -0
- data/spec/integration/keyword_search_spec.rb +148 -0
- data/spec/integration/local_search_spec.rb +47 -0
- data/spec/integration/scoped_search_spec.rb +303 -0
- data/spec/integration/spec_helper.rb +1 -0
- data/spec/integration/stored_fields_spec.rb +10 -0
- data/spec/integration/test_pagination.rb +32 -0
- data/spec/mocks/adapters.rb +32 -0
- data/spec/mocks/blog.rb +3 -0
- data/spec/mocks/comment.rb +19 -0
- data/spec/mocks/connection.rb +84 -0
- data/spec/mocks/mock_adapter.rb +30 -0
- data/spec/mocks/mock_record.rb +48 -0
- data/spec/mocks/photo.rb +8 -0
- data/spec/mocks/post.rb +75 -0
- data/spec/mocks/super_class.rb +2 -0
- data/spec/mocks/user.rb +8 -0
- data/spec/spec_helper.rb +60 -0
- data/tasks/gemspec.rake +35 -0
- data/tasks/rcov.rake +28 -0
- data/tasks/rdoc.rake +22 -0
- data/tasks/schema.rake +19 -0
- data/tasks/spec.rake +24 -0
- data/tasks/todo.rake +4 -0
- data/templates/schema.xml.erb +36 -0
- metadata +312 -0
@@ -0,0 +1,206 @@
|
|
1
|
+
module Sunspot
|
2
|
+
#
|
3
|
+
# A Sunspot session encapsulates a connection to Solr and a set of
|
4
|
+
# configuration choices. Though users of Sunspot may manually instantiate
|
5
|
+
# Session objects, in the general case it's easier to use the singleton
|
6
|
+
# stored in the Sunspot module. Since the Sunspot module provides all of
|
7
|
+
# the instance methods of Session as class methods, they are not documented
|
8
|
+
# again here.
|
9
|
+
#
|
10
|
+
class Session
|
11
|
+
class <<self
|
12
|
+
attr_writer :connection_class #:nodoc:
|
13
|
+
|
14
|
+
#
|
15
|
+
# For testing purposes
|
16
|
+
#
|
17
|
+
def connection_class #:nodoc:
|
18
|
+
@connection_class ||= RSolr::Connection
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
#
|
23
|
+
# Sunspot::Configuration object for this session
|
24
|
+
#
|
25
|
+
attr_reader :config
|
26
|
+
|
27
|
+
#
|
28
|
+
# Sessions are initialized with a Sunspot configuration and a Solr
|
29
|
+
# connection. Usually you will want to stick with the default arguments
|
30
|
+
# when instantiating your own sessions.
|
31
|
+
#
|
32
|
+
def initialize(config = Configuration.build, connection = nil)
|
33
|
+
@config = config
|
34
|
+
yield(@config) if block_given?
|
35
|
+
@connection = connection
|
36
|
+
@updates = 0
|
37
|
+
end
|
38
|
+
|
39
|
+
#
|
40
|
+
# See Sunspot.new_search
|
41
|
+
#
|
42
|
+
def new_search(*types)
|
43
|
+
types.flatten!
|
44
|
+
if types.empty?
|
45
|
+
raise(ArgumentError, "You must specify at least one type to search")
|
46
|
+
end
|
47
|
+
setup =
|
48
|
+
if types.length == 1
|
49
|
+
Setup.for(types.first)
|
50
|
+
else
|
51
|
+
CompositeSetup.for(types)
|
52
|
+
end
|
53
|
+
Search.new(connection, setup, Query::Query.new(types, setup, @config))
|
54
|
+
end
|
55
|
+
|
56
|
+
#
|
57
|
+
# See Sunspot.search
|
58
|
+
#
|
59
|
+
def search(*types, &block)
|
60
|
+
search = new_search(*types)
|
61
|
+
search.build(&block) if block
|
62
|
+
search.execute!
|
63
|
+
end
|
64
|
+
|
65
|
+
#
|
66
|
+
# See Sunspot.index
|
67
|
+
#
|
68
|
+
def index(*objects)
|
69
|
+
objects.flatten!
|
70
|
+
@updates += objects.length
|
71
|
+
indexer.add(objects)
|
72
|
+
end
|
73
|
+
|
74
|
+
#
|
75
|
+
# See Sunspot.index!
|
76
|
+
#
|
77
|
+
def index!(*objects)
|
78
|
+
index(*objects)
|
79
|
+
commit
|
80
|
+
end
|
81
|
+
|
82
|
+
#
|
83
|
+
# See Sunspot.commit
|
84
|
+
#
|
85
|
+
def commit
|
86
|
+
@updates = 0
|
87
|
+
connection.commit
|
88
|
+
end
|
89
|
+
|
90
|
+
#
|
91
|
+
# See Sunspot.remove
|
92
|
+
#
|
93
|
+
def remove(*objects)
|
94
|
+
objects.flatten!
|
95
|
+
@updates += objects.length
|
96
|
+
for object in objects
|
97
|
+
indexer.remove(object)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
#
|
102
|
+
# See Sunspot.remove!
|
103
|
+
#
|
104
|
+
def remove!(*objects)
|
105
|
+
remove(*objects)
|
106
|
+
commit
|
107
|
+
end
|
108
|
+
|
109
|
+
#
|
110
|
+
# See Sunspot.remove_by_id
|
111
|
+
#
|
112
|
+
def remove_by_id(clazz, id)
|
113
|
+
class_name =
|
114
|
+
if clazz.is_a?(Class)
|
115
|
+
clazz.name
|
116
|
+
else
|
117
|
+
clazz.to_s
|
118
|
+
end
|
119
|
+
indexer.remove_by_id(class_name, id)
|
120
|
+
end
|
121
|
+
|
122
|
+
#
|
123
|
+
# See Sunspot.remove_by_id!
|
124
|
+
#
|
125
|
+
def remove_by_id!(clazz, id)
|
126
|
+
remove_by_id(clazz, id)
|
127
|
+
commit
|
128
|
+
end
|
129
|
+
|
130
|
+
#
|
131
|
+
# See Sunspot.remove_all
|
132
|
+
#
|
133
|
+
def remove_all(*classes)
|
134
|
+
classes.flatten!
|
135
|
+
if classes.empty?
|
136
|
+
@updates += 1
|
137
|
+
Indexer.remove_all(connection)
|
138
|
+
else
|
139
|
+
@updates += classes.length
|
140
|
+
for clazz in classes
|
141
|
+
indexer.remove_all(clazz)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
#
|
147
|
+
# See Sunspot.remove_all!
|
148
|
+
#
|
149
|
+
def remove_all!(*classes)
|
150
|
+
remove_all(*classes)
|
151
|
+
commit
|
152
|
+
end
|
153
|
+
|
154
|
+
#
|
155
|
+
# See Sunspot.dirty?
|
156
|
+
#
|
157
|
+
def dirty?
|
158
|
+
@updates > 0
|
159
|
+
end
|
160
|
+
|
161
|
+
#
|
162
|
+
# See Sunspot.commit_if_dirty
|
163
|
+
#
|
164
|
+
def commit_if_dirty
|
165
|
+
commit if dirty?
|
166
|
+
end
|
167
|
+
|
168
|
+
#
|
169
|
+
# See Sunspot.batch
|
170
|
+
#
|
171
|
+
def batch
|
172
|
+
indexer.start_batch
|
173
|
+
yield
|
174
|
+
indexer.flush_batch
|
175
|
+
end
|
176
|
+
|
177
|
+
private
|
178
|
+
|
179
|
+
#
|
180
|
+
# Retrieve the Solr connection for this session, creating one if it does not
|
181
|
+
# already exist.
|
182
|
+
#
|
183
|
+
# ==== Returns
|
184
|
+
#
|
185
|
+
# Solr::Connection:: The connection for this session
|
186
|
+
#
|
187
|
+
def connection
|
188
|
+
@connection ||=
|
189
|
+
begin
|
190
|
+
connection = self.class.connection_class.new(
|
191
|
+
RSolr::Adapter::HTTP.new(:url => config.solr.url)
|
192
|
+
)
|
193
|
+
connection.adapter.connector.adapter_name = config.http_client
|
194
|
+
connection
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
#
|
199
|
+
# Return an indexer pointing at this session's connection. One is created
|
200
|
+
# and saved for each sesion.
|
201
|
+
#
|
202
|
+
def indexer
|
203
|
+
@indexer ||= Indexer.new(connection)
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|
@@ -0,0 +1,312 @@
|
|
1
|
+
module Sunspot
|
2
|
+
#
|
3
|
+
# This class encapsulates the search/indexing setup for a given class. Its
|
4
|
+
# contents are built using the Sunspot.setup method.
|
5
|
+
#
|
6
|
+
class Setup #:nodoc:
|
7
|
+
def initialize(clazz)
|
8
|
+
@clazz = clazz
|
9
|
+
@class_name = clazz.name
|
10
|
+
@field_factories, @text_field_factories, @dynamic_field_factories,
|
11
|
+
@field_factories_cache, @text_field_factories_cache,
|
12
|
+
@dynamic_field_factories_cache = *Array.new(6) { Hash.new }
|
13
|
+
@dsl = DSL::Fields.new(self)
|
14
|
+
add_field_factory(:class, Type::ClassType)
|
15
|
+
end
|
16
|
+
|
17
|
+
def type_names
|
18
|
+
[@class_name]
|
19
|
+
end
|
20
|
+
|
21
|
+
#
|
22
|
+
# Add field_factories for scope/ordering
|
23
|
+
#
|
24
|
+
# ==== Parameters
|
25
|
+
#
|
26
|
+
# field_factories<Array>:: Array of Sunspot::Field objects
|
27
|
+
#
|
28
|
+
def add_field_factory(name, type, options = {}, &block)
|
29
|
+
field_factory = FieldFactory::Static.new(name, type, options, &block)
|
30
|
+
@field_factories[field_factory.signature] = field_factory
|
31
|
+
@field_factories_cache[field_factory.name] = field_factory
|
32
|
+
end
|
33
|
+
|
34
|
+
#
|
35
|
+
# Add field_factories for fulltext search
|
36
|
+
#
|
37
|
+
# ==== Parameters
|
38
|
+
#
|
39
|
+
# field_factories<Array>:: Array of Sunspot::Field objects
|
40
|
+
#
|
41
|
+
def add_text_field_factory(name, options = {}, &block)
|
42
|
+
field_factory = FieldFactory::Static.new(name, Type::TextType, options, &block)
|
43
|
+
@text_field_factories[name] = field_factory
|
44
|
+
@text_field_factories_cache[field_factory.name] = field_factory
|
45
|
+
end
|
46
|
+
|
47
|
+
#
|
48
|
+
# Add dynamic field_factories
|
49
|
+
#
|
50
|
+
# ==== Parameters
|
51
|
+
#
|
52
|
+
# field_factories<Array>:: Array of dynamic field objects
|
53
|
+
#
|
54
|
+
def add_dynamic_field_factory(name, type, options = {}, &block)
|
55
|
+
field_factory = FieldFactory::Dynamic.new(name, type, options, &block)
|
56
|
+
@dynamic_field_factories[field_factory.signature] = field_factory
|
57
|
+
@dynamic_field_factories_cache[field_factory.name] = field_factory
|
58
|
+
end
|
59
|
+
|
60
|
+
#
|
61
|
+
# The coordinates field factory is used for populating the coordinate fields
|
62
|
+
# of documents during index, but does not actually generate fields (since
|
63
|
+
# the field names used in search are static).
|
64
|
+
#
|
65
|
+
def set_coordinates_field(name)
|
66
|
+
@coordinates_field_factory = FieldFactory::Coordinates.new(name)
|
67
|
+
end
|
68
|
+
|
69
|
+
#
|
70
|
+
# Add a document boost to documents at index time. Document boost can be
|
71
|
+
# static (the same for all documents of this class), or extracted on a per-
|
72
|
+
# document basis using either attribute or block extraction as per usual.
|
73
|
+
#
|
74
|
+
def add_document_boost(attr_name, &block)
|
75
|
+
@document_boost_extractor =
|
76
|
+
if attr_name
|
77
|
+
if attr_name.respond_to?(:to_f)
|
78
|
+
DataExtractor::Constant.new(attr_name)
|
79
|
+
else
|
80
|
+
DataExtractor::AttributeExtractor.new(attr_name)
|
81
|
+
end
|
82
|
+
else
|
83
|
+
DataExtractor::BlockExtractor.new(&block)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
#
|
88
|
+
# Builder method for evaluating the setup DSL
|
89
|
+
#
|
90
|
+
def setup(&block)
|
91
|
+
@dsl.instance_eval(&block)
|
92
|
+
end
|
93
|
+
|
94
|
+
#
|
95
|
+
# Return the Field with the given (public-facing) name
|
96
|
+
#
|
97
|
+
def field(field_name)
|
98
|
+
if field_factory = @field_factories_cache[field_name.to_sym]
|
99
|
+
field_factory.build
|
100
|
+
else
|
101
|
+
raise(
|
102
|
+
UnrecognizedFieldError,
|
103
|
+
"No field configured for #{@clazz.name} with name '#{field_name}'"
|
104
|
+
)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
#
|
109
|
+
# Return one or more text fields with the given public-facing name. This
|
110
|
+
# implementation will always return a single field (in an array), but
|
111
|
+
# CompositeSetup objects might return more than one.
|
112
|
+
#
|
113
|
+
def text_fields(field_name)
|
114
|
+
text_field =
|
115
|
+
if field_factory = @text_field_factories_cache[field_name.to_sym]
|
116
|
+
field_factory.build
|
117
|
+
else
|
118
|
+
raise(
|
119
|
+
UnrecognizedFieldError,
|
120
|
+
"No text field configured for #{@clazz.name} with name '#{field_name}'"
|
121
|
+
)
|
122
|
+
end
|
123
|
+
[text_field]
|
124
|
+
end
|
125
|
+
|
126
|
+
#
|
127
|
+
# Return the DynamicFieldFactory with the given base name
|
128
|
+
#
|
129
|
+
def dynamic_field_factory(field_name)
|
130
|
+
@dynamic_field_factories_cache[field_name.to_sym] || raise(
|
131
|
+
UnrecognizedFieldError,
|
132
|
+
"No dynamic field configured for #{@clazz.name} with name '#{field_name}'"
|
133
|
+
)
|
134
|
+
end
|
135
|
+
|
136
|
+
#
|
137
|
+
# Return all attribute fields
|
138
|
+
#
|
139
|
+
def fields
|
140
|
+
field_factories.map { |field_factory| field_factory.build }
|
141
|
+
end
|
142
|
+
|
143
|
+
#
|
144
|
+
# Return all text fields
|
145
|
+
#
|
146
|
+
def all_text_fields
|
147
|
+
text_field_factories.map { |text_field_factory| text_field_factory.build }
|
148
|
+
end
|
149
|
+
|
150
|
+
#
|
151
|
+
# Get the field_factories associated with this setup as well as all inherited field_factories
|
152
|
+
#
|
153
|
+
# ==== Returns
|
154
|
+
#
|
155
|
+
# Array:: Collection of all field_factories associated with this setup
|
156
|
+
#
|
157
|
+
def field_factories
|
158
|
+
collection_from_inheritable_hash(:field_factories)
|
159
|
+
end
|
160
|
+
|
161
|
+
#
|
162
|
+
# Get the text field_factories associated with this setup as well as all inherited
|
163
|
+
# text field_factories
|
164
|
+
#
|
165
|
+
# ==== Returns
|
166
|
+
#
|
167
|
+
# Array:: Collection of all text field_factories associated with this setup
|
168
|
+
#
|
169
|
+
def text_field_factories
|
170
|
+
collection_from_inheritable_hash(:text_field_factories)
|
171
|
+
end
|
172
|
+
|
173
|
+
#
|
174
|
+
# Get all static, dynamic, and text field_factories associated with this setup as
|
175
|
+
# well as all inherited field_factories
|
176
|
+
#
|
177
|
+
# ==== Returns
|
178
|
+
#
|
179
|
+
# Array:: Collection of all text and scope field_factories associated with this setup
|
180
|
+
#
|
181
|
+
def all_field_factories
|
182
|
+
all_field_factories = []
|
183
|
+
all_field_factories.concat(field_factories).concat(text_field_factories).concat(dynamic_field_factories)
|
184
|
+
all_field_factories << @coordinates_field_factory if @coordinates_field_factory
|
185
|
+
all_field_factories
|
186
|
+
end
|
187
|
+
|
188
|
+
#
|
189
|
+
# Get all dynamic field_factories for this and parent setups
|
190
|
+
#
|
191
|
+
# ==== Returns
|
192
|
+
#
|
193
|
+
# Array:: Dynamic field_factories
|
194
|
+
#
|
195
|
+
def dynamic_field_factories
|
196
|
+
collection_from_inheritable_hash(:dynamic_field_factories)
|
197
|
+
end
|
198
|
+
|
199
|
+
def coordinates_field
|
200
|
+
@coordinates_field
|
201
|
+
end
|
202
|
+
|
203
|
+
#
|
204
|
+
# Return the class associated with this setup.
|
205
|
+
#
|
206
|
+
# ==== Returns
|
207
|
+
#
|
208
|
+
# clazz<Class>:: Class setup is configured for
|
209
|
+
#
|
210
|
+
def clazz
|
211
|
+
Util.full_const_get(@class_name)
|
212
|
+
end
|
213
|
+
|
214
|
+
#
|
215
|
+
# Get the document boost for a given model
|
216
|
+
#
|
217
|
+
def document_boost_for(model)
|
218
|
+
if @document_boost_extractor
|
219
|
+
@document_boost_extractor.value_for(model)
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
protected
|
224
|
+
|
225
|
+
#
|
226
|
+
# Get the nearest inherited setup, if any
|
227
|
+
#
|
228
|
+
# ==== Returns
|
229
|
+
#
|
230
|
+
# Sunspot::Setup:: Setup for the nearest ancestor of this setup's class
|
231
|
+
#
|
232
|
+
def parent
|
233
|
+
Setup.for(clazz.superclass)
|
234
|
+
end
|
235
|
+
|
236
|
+
def get_inheritable_hash(name)
|
237
|
+
hash = instance_variable_get(:"@#{name}")
|
238
|
+
parent.get_inheritable_hash(name).each_pair do |key, value|
|
239
|
+
hash[key] = value unless hash.has_key?(key)
|
240
|
+
end if parent
|
241
|
+
hash
|
242
|
+
end
|
243
|
+
|
244
|
+
private
|
245
|
+
|
246
|
+
def collection_from_inheritable_hash(name)
|
247
|
+
get_inheritable_hash(name).values
|
248
|
+
end
|
249
|
+
|
250
|
+
class <<self
|
251
|
+
#
|
252
|
+
# Retrieve or create the Setup instance for the given class, evaluating
|
253
|
+
# the given block to add to the setup's configuration
|
254
|
+
#
|
255
|
+
def setup(clazz, &block) #:nodoc:
|
256
|
+
self.for!(clazz).setup(&block)
|
257
|
+
end
|
258
|
+
|
259
|
+
#
|
260
|
+
# Retrieve the setup instance for the given class, or for the nearest
|
261
|
+
# ancestor that has a setup, if any.
|
262
|
+
#
|
263
|
+
# ==== Parameters
|
264
|
+
#
|
265
|
+
# clazz<Class>:: Class for which to retrieve a setup
|
266
|
+
#
|
267
|
+
# ==== Returns
|
268
|
+
#
|
269
|
+
# Sunspot::Setup::
|
270
|
+
# Setup instance associated with the given class or its nearest ancestor
|
271
|
+
#
|
272
|
+
def for(clazz) #:nodoc:
|
273
|
+
class_name =
|
274
|
+
if clazz.respond_to?(:name)
|
275
|
+
clazz.name
|
276
|
+
else
|
277
|
+
clazz
|
278
|
+
end
|
279
|
+
setups[class_name.to_sym] || self.for(clazz.superclass) if clazz
|
280
|
+
end
|
281
|
+
|
282
|
+
protected
|
283
|
+
|
284
|
+
#
|
285
|
+
# Retrieve or create a Setup instance for this class
|
286
|
+
#
|
287
|
+
# ==== Parameters
|
288
|
+
#
|
289
|
+
# clazz<Class>:: Class for which to retrieve a setup
|
290
|
+
#
|
291
|
+
# ==== Returns
|
292
|
+
#
|
293
|
+
# Sunspot::Setup:: New or existing setup for this class
|
294
|
+
#
|
295
|
+
def for!(clazz) #:nodoc:
|
296
|
+
setups[clazz.name.to_sym] ||= new(clazz)
|
297
|
+
end
|
298
|
+
|
299
|
+
private
|
300
|
+
|
301
|
+
# Singleton hash of class names to Setup instances
|
302
|
+
#
|
303
|
+
# ==== Returns
|
304
|
+
#
|
305
|
+
# Hash:: Class names keyed to Setup instances
|
306
|
+
#
|
307
|
+
def setups
|
308
|
+
@setups ||= {}
|
309
|
+
end
|
310
|
+
end
|
311
|
+
end
|
312
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Sunspot
|
2
|
+
#
|
3
|
+
# A TextFieldSetup encapsulates a regular (or composite) setup, and exposes
|
4
|
+
# the #field() method returning text fields instead of attribute fields.
|
5
|
+
#
|
6
|
+
class TextFieldSetup #:nodoc:
|
7
|
+
def initialize(setup)
|
8
|
+
@setup = setup
|
9
|
+
end
|
10
|
+
|
11
|
+
#
|
12
|
+
# Return a text field with the given name. Duck-type compatible with
|
13
|
+
# Setup and CompositeSetup, but return text fields instead.
|
14
|
+
#
|
15
|
+
def field(name)
|
16
|
+
fields = @setup.text_fields(name)
|
17
|
+
if fields
|
18
|
+
if fields.length == 1
|
19
|
+
fields.first
|
20
|
+
else
|
21
|
+
raise(
|
22
|
+
Sunspot::UnrecognizedFieldError,
|
23
|
+
"The text field with name #{name} has incompatible configurations for the classes #{@setup.type_names.join(', ')}"
|
24
|
+
)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|