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,265 @@
|
|
1
|
+
module Sunspot
|
2
|
+
#
|
3
|
+
# Sunspot works by saving references to the primary key (or natural ID) of
|
4
|
+
# each indexed object, and then retrieving the objects from persistent storage
|
5
|
+
# when their IDs are referenced in search results. In order for Sunspot to
|
6
|
+
# know what an object's primary key is, and how to retrieve objects from
|
7
|
+
# persistent storage given a primary key, an adapter must be registered for
|
8
|
+
# that object's class or one of its superclasses (for instance, an adapter
|
9
|
+
# registered for ActiveRecord::Base would be used for all ActiveRecord
|
10
|
+
# models).
|
11
|
+
#
|
12
|
+
# To provide Sunspot with this ability, adapters must have two roles:
|
13
|
+
#
|
14
|
+
# Data accessor::
|
15
|
+
# A subclass of Sunspot::Adapters::DataAccessor, this object is instantiated
|
16
|
+
# with a particular class and must respond to the #load() method, which
|
17
|
+
# returns an object from persistent storage given that object's primary key.
|
18
|
+
# It can also optionally implement the #load_all() method, which returns
|
19
|
+
# a collection of objects given a collection of primary keys, if that can be
|
20
|
+
# done more efficiently than calling #load() on each key.
|
21
|
+
# Instance adapter::
|
22
|
+
# A subclass of Sunspot::Adapters::InstanceAdapter, this object is
|
23
|
+
# instantiated with a particular instance. Its only job is to tell Sunspot
|
24
|
+
# what the object's primary key is, by implementing the #id() method.
|
25
|
+
#
|
26
|
+
# Adapters are registered by registering their two components, telling Sunspot
|
27
|
+
# that they are available for one or more classes, and all of their
|
28
|
+
# subclasses. See Sunspot::Adapters::DataAccessor.register and
|
29
|
+
# Sunspot::Adapters::InstanceAdapter.register for the details.
|
30
|
+
#
|
31
|
+
# See spec/mocks/mock_adapter.rb for an example of how adapter classes should
|
32
|
+
# be implemented.
|
33
|
+
#
|
34
|
+
module Adapters
|
35
|
+
# Subclasses of the InstanceAdapter class should implement the #id method,
|
36
|
+
# which returns the primary key of the instance stored in the @instance
|
37
|
+
# variable. The primary key must be unique within the scope of the
|
38
|
+
# instance's class.
|
39
|
+
#
|
40
|
+
# ==== Example:
|
41
|
+
#
|
42
|
+
# class FileAdapter < Sunspot::Adapters::InstanceAdapter
|
43
|
+
# def id
|
44
|
+
# File.expand_path(@instance.path)
|
45
|
+
# end
|
46
|
+
# end
|
47
|
+
#
|
48
|
+
# # then in your initializer
|
49
|
+
# Sunspot::Adapters::InstanceAdapter.register(MyAdapter, File)
|
50
|
+
#
|
51
|
+
class InstanceAdapter
|
52
|
+
def initialize(instance) #:nodoc:
|
53
|
+
@instance = instance
|
54
|
+
end
|
55
|
+
|
56
|
+
#
|
57
|
+
# The universally-unique ID for this instance that will be stored in solr
|
58
|
+
#
|
59
|
+
# ==== Returns
|
60
|
+
#
|
61
|
+
# String:: ID for use in Solr
|
62
|
+
#
|
63
|
+
def index_id #:nodoc:
|
64
|
+
InstanceAdapter.index_id_for(@instance.class.name, id)
|
65
|
+
end
|
66
|
+
|
67
|
+
class <<self
|
68
|
+
# Instantiate an InstanceAdapter for the given object, searching for
|
69
|
+
# registered adapters for the object's class.
|
70
|
+
#
|
71
|
+
# ==== Parameters
|
72
|
+
#
|
73
|
+
# instance<Object>:: The instance to adapt
|
74
|
+
#
|
75
|
+
# ==== Returns
|
76
|
+
#
|
77
|
+
# InstanceAdapter::
|
78
|
+
# An instance of an InstanceAdapter implementation that
|
79
|
+
# wraps the given instance
|
80
|
+
#
|
81
|
+
def adapt(instance) #:nodoc:
|
82
|
+
self.for(instance.class).new(instance)
|
83
|
+
end
|
84
|
+
|
85
|
+
# Register an instance adapter for a set of classes. When searching for
|
86
|
+
# an adapter for a given instance, Sunspot starts with the instance's
|
87
|
+
# class, and then searches for registered adapters up the class's
|
88
|
+
# ancestor chain.
|
89
|
+
#
|
90
|
+
# ==== Parameters
|
91
|
+
#
|
92
|
+
# instance_adapter<Class>:: The instance adapter class to register
|
93
|
+
# classes...<Class>::
|
94
|
+
# One or more classes that this instance adapter adapts
|
95
|
+
#
|
96
|
+
def register(instance_adapter, *classes)
|
97
|
+
for clazz in classes
|
98
|
+
instance_adapters[clazz.name.to_sym] = instance_adapter
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
# Find the best InstanceAdapter implementation that adapts the given
|
103
|
+
# class. Starting with the class and then moving up the ancestor chain,
|
104
|
+
# looks for registered InstanceAdapter implementations.
|
105
|
+
#
|
106
|
+
# ==== Parameters
|
107
|
+
#
|
108
|
+
# clazz<Class>:: The class to find an InstanceAdapter for
|
109
|
+
#
|
110
|
+
# ==== Returns
|
111
|
+
#
|
112
|
+
# Class:: Subclass of InstanceAdapter, or nil if none found
|
113
|
+
#
|
114
|
+
# ==== Raises
|
115
|
+
#
|
116
|
+
# Sunspot::NoAdapterError:: If no adapter is registered for this class
|
117
|
+
#
|
118
|
+
def for(clazz) #:nodoc:
|
119
|
+
original_class_name = clazz.name
|
120
|
+
clazz.ancestors.each do |ancestor_class|
|
121
|
+
next if ancestor_class.name.nil? || ancestor_class.name.empty?
|
122
|
+
class_name = ancestor_class.name.to_sym
|
123
|
+
return instance_adapters[class_name] if instance_adapters[class_name]
|
124
|
+
end
|
125
|
+
|
126
|
+
raise(Sunspot::NoAdapterError,
|
127
|
+
"No adapter is configured for #{original_class_name} or its superclasses. See the documentation for Sunspot::Adapters")
|
128
|
+
end
|
129
|
+
|
130
|
+
def index_id_for(class_name, id) #:nodoc:
|
131
|
+
"#{class_name} #{id}"
|
132
|
+
end
|
133
|
+
|
134
|
+
protected
|
135
|
+
|
136
|
+
# Lazy-initialize the hash of registered instance adapters
|
137
|
+
#
|
138
|
+
# ==== Returns
|
139
|
+
#
|
140
|
+
# Hash:: Hash containing class names keyed to instance adapter classes
|
141
|
+
#
|
142
|
+
def instance_adapters #:nodoc:
|
143
|
+
@instance_adapters ||= {}
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
# Subclasses of the DataAccessor class take care of retreiving instances of
|
149
|
+
# the adapted class from (usually persistent) storage. Subclasses must
|
150
|
+
# implement the #load method, which takes an id (the value returned by
|
151
|
+
# InstanceAdapter#id, as a string), and returns the instance referenced by
|
152
|
+
# that ID. Optionally, it can also override the #load_all method, which
|
153
|
+
# takes an array of IDs and returns an array of instances in the order
|
154
|
+
# given. #load_all need only be implemented if it can be done more
|
155
|
+
# efficiently than simply iterating over the IDs and calling #load on each
|
156
|
+
# individually.
|
157
|
+
#
|
158
|
+
# ==== Example
|
159
|
+
#
|
160
|
+
# class FileAccessor < Sunspot::Adapters::InstanceAdapter
|
161
|
+
# def load(id)
|
162
|
+
# @clazz.open(id)
|
163
|
+
# end
|
164
|
+
# end
|
165
|
+
#
|
166
|
+
# Sunspot::Adapters::DataAccessor.register(FileAccessor, File)
|
167
|
+
#
|
168
|
+
class DataAccessor
|
169
|
+
def initialize(clazz) #:nodoc:
|
170
|
+
@clazz = clazz
|
171
|
+
end
|
172
|
+
|
173
|
+
# Subclasses can override this class to provide more efficient bulk
|
174
|
+
# loading of instances. Instances must be returned in the same order
|
175
|
+
# that the IDs were given.
|
176
|
+
#
|
177
|
+
# ==== Parameters
|
178
|
+
#
|
179
|
+
# ids<Array>:: collection of IDs
|
180
|
+
#
|
181
|
+
# ==== Returns
|
182
|
+
#
|
183
|
+
# Array:: collection of instances, in order of IDs given
|
184
|
+
#
|
185
|
+
def load_all(ids)
|
186
|
+
ids.map { |id| self.load(id) }
|
187
|
+
end
|
188
|
+
|
189
|
+
class <<self
|
190
|
+
# Create a DataAccessor for the given class, searching registered
|
191
|
+
# adapters for the best match. See InstanceAdapter#adapt for discussion
|
192
|
+
# of inheritence.
|
193
|
+
#
|
194
|
+
# ==== Parameters
|
195
|
+
#
|
196
|
+
# clazz<Class>:: Class to create DataAccessor for
|
197
|
+
#
|
198
|
+
# ==== Returns
|
199
|
+
#
|
200
|
+
# DataAccessor::
|
201
|
+
# DataAccessor implementation which provides access to given class
|
202
|
+
#
|
203
|
+
def create(clazz) #:nodoc:
|
204
|
+
self.for(clazz).new(clazz)
|
205
|
+
end
|
206
|
+
|
207
|
+
# Register data accessor for a set of classes. When searching for
|
208
|
+
# an accessor for a given class, Sunspot starts with the class,
|
209
|
+
# and then searches for registered adapters up the class's ancestor
|
210
|
+
# chain.
|
211
|
+
#
|
212
|
+
# ==== Parameters
|
213
|
+
#
|
214
|
+
# data_accessor<Class>:: The data accessor class to register
|
215
|
+
# classes...<Class>::
|
216
|
+
# One or more classes that this data accessor providess access to
|
217
|
+
#
|
218
|
+
def register(data_accessor, *classes)
|
219
|
+
for clazz in classes
|
220
|
+
data_accessors[clazz.name.to_sym] = data_accessor
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
# Find the best DataAccessor implementation that adapts the given class.
|
225
|
+
# Starting with the class and then moving up the ancestor chain, looks
|
226
|
+
# for registered DataAccessor implementations.
|
227
|
+
#
|
228
|
+
# ==== Parameters
|
229
|
+
#
|
230
|
+
# clazz<Class>:: The class to find a DataAccessor for
|
231
|
+
#
|
232
|
+
# ==== Returns
|
233
|
+
#
|
234
|
+
# Class:: Implementation of DataAccessor
|
235
|
+
#
|
236
|
+
# ==== Raises
|
237
|
+
#
|
238
|
+
# Sunspot::NoAdapterError:: If no data accessor exists for the given class
|
239
|
+
#
|
240
|
+
def for(clazz) #:nodoc:
|
241
|
+
original_class_name = clazz.name
|
242
|
+
clazz.ancestors.each do |ancestor_class|
|
243
|
+
next if ancestor_class.name.nil? || ancestor_class.name.empty?
|
244
|
+
class_name = ancestor_class.name.to_sym
|
245
|
+
return data_accessors[class_name] if data_accessors[class_name]
|
246
|
+
end
|
247
|
+
raise(Sunspot::NoAdapterError,
|
248
|
+
"No data accessor is configured for #{original_class_name} or its superclasses. See the documentation for Sunspot::Adapters")
|
249
|
+
end
|
250
|
+
|
251
|
+
protected
|
252
|
+
|
253
|
+
# Lazy-initialize the hash of registered data accessors
|
254
|
+
#
|
255
|
+
# ==== Returns
|
256
|
+
#
|
257
|
+
# Hash:: Hash containing class names keyed to data accessor classes
|
258
|
+
#
|
259
|
+
def data_accessors #:nodoc:
|
260
|
+
@adapters ||= {}
|
261
|
+
end
|
262
|
+
end
|
263
|
+
end
|
264
|
+
end
|
265
|
+
end
|
@@ -0,0 +1,184 @@
|
|
1
|
+
module Sunspot
|
2
|
+
#
|
3
|
+
# The CompositeSetup class encapsulates a collection of setups, and responds
|
4
|
+
# to a subset of the methods that Setup responds to (in particular, the
|
5
|
+
# methods required to build queries).
|
6
|
+
#
|
7
|
+
class CompositeSetup #:nodoc:
|
8
|
+
class << self
|
9
|
+
alias_method :for, :new
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(types)
|
13
|
+
@types = types
|
14
|
+
end
|
15
|
+
|
16
|
+
#
|
17
|
+
# Collection of Setup objects for the enclosed types
|
18
|
+
#
|
19
|
+
# ==== Returns
|
20
|
+
#
|
21
|
+
# Array:: Collection of Setup objects
|
22
|
+
#
|
23
|
+
def setups
|
24
|
+
@setups ||= @types.map { |type| Setup.for(type) }
|
25
|
+
end
|
26
|
+
|
27
|
+
#
|
28
|
+
# Return the names of the encapsulated types
|
29
|
+
#
|
30
|
+
# ==== Returns
|
31
|
+
#
|
32
|
+
# Array:: Collection of class names
|
33
|
+
#
|
34
|
+
def type_names
|
35
|
+
@type_names ||= @types.map { |clazz| clazz.name }
|
36
|
+
end
|
37
|
+
|
38
|
+
#
|
39
|
+
# Get a text field object by its public name. A field will be returned if
|
40
|
+
# it is configured for any of the enclosed types.
|
41
|
+
#
|
42
|
+
# ==== Returns
|
43
|
+
#
|
44
|
+
# Sunspot::FulltextField:: Text field with the given public name
|
45
|
+
#
|
46
|
+
# ==== Raises
|
47
|
+
#
|
48
|
+
# UnrecognizedFieldError::
|
49
|
+
# If no field with that name is configured for any of the enclosed types.
|
50
|
+
#
|
51
|
+
def text_fields(field_name)
|
52
|
+
text_fields_hash[field_name.to_sym] || raise(
|
53
|
+
UnrecognizedFieldError,
|
54
|
+
"No text field configured for #{@types * ', '} with name '#{field_name}'"
|
55
|
+
)
|
56
|
+
end
|
57
|
+
|
58
|
+
#
|
59
|
+
# Get a Sunspot::AttributeField instance corresponding to the given field name
|
60
|
+
#
|
61
|
+
# ==== Parameters
|
62
|
+
#
|
63
|
+
# field_name<Symbol>:: The public field name for which to find a field
|
64
|
+
#
|
65
|
+
# ==== Returns
|
66
|
+
#
|
67
|
+
# Sunspot::AttributeField The field object corresponding to the given name
|
68
|
+
#
|
69
|
+
# ==== Raises
|
70
|
+
#
|
71
|
+
# ArgumentError::
|
72
|
+
# If the given field name is not configured for the types being queried
|
73
|
+
#
|
74
|
+
def field(field_name) #:nodoc:
|
75
|
+
fields_hash[field_name.to_sym] || raise(
|
76
|
+
UnrecognizedFieldError,
|
77
|
+
"No field configured for #{@types * ', '} with name '#{field_name}'"
|
78
|
+
)
|
79
|
+
end
|
80
|
+
|
81
|
+
#
|
82
|
+
# Get a dynamic field factory for the given base name.
|
83
|
+
#
|
84
|
+
# ==== Returns
|
85
|
+
#
|
86
|
+
# DynamicFieldFactory:: Factory for dynamic fields with the given base name
|
87
|
+
#
|
88
|
+
# ==== Raises
|
89
|
+
#
|
90
|
+
# UnrecognizedFieldError::
|
91
|
+
# If the given base name is not configured as a dynamic field for the types being queried
|
92
|
+
#
|
93
|
+
def dynamic_field_factory(field_name)
|
94
|
+
dynamic_field_factories_hash[field_name.to_sym] || raise(
|
95
|
+
UnrecognizedFieldError,
|
96
|
+
"No dynamic field configured for #{@types * ', '} with name #{field_name.inspect}"
|
97
|
+
)
|
98
|
+
end
|
99
|
+
|
100
|
+
#
|
101
|
+
# Collection of all text fields configured for any of the enclosed types.
|
102
|
+
#
|
103
|
+
# === Returns
|
104
|
+
#
|
105
|
+
# Array:: Text fields configured for the enclosed types
|
106
|
+
#
|
107
|
+
def all_text_fields
|
108
|
+
@text_fields ||= text_fields_hash.values.map { |set| set.to_a }.flatten
|
109
|
+
end
|
110
|
+
|
111
|
+
private
|
112
|
+
|
113
|
+
#
|
114
|
+
# Return a hash of field names to text field objects, containing all fields
|
115
|
+
# that are configured for any of the types enclosed.
|
116
|
+
#
|
117
|
+
# ==== Returns
|
118
|
+
#
|
119
|
+
# Hash:: Hash of field names to text field objects.
|
120
|
+
#
|
121
|
+
def text_fields_hash
|
122
|
+
@text_fields_hash ||=
|
123
|
+
setups.inject({}) do |hash, setup|
|
124
|
+
setup.all_text_fields.each do |text_field|
|
125
|
+
(hash[text_field.name] ||= Set.new) << text_field
|
126
|
+
end
|
127
|
+
hash
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
#
|
132
|
+
# Return a hash of field names to field objects, containing all fields
|
133
|
+
# that are common to all of the classes enclosed. In order for fields
|
134
|
+
# to be common, they must be of the same type and have the same
|
135
|
+
# value for allow_multiple? and stored?. This method is memoized.
|
136
|
+
#
|
137
|
+
# ==== Returns
|
138
|
+
#
|
139
|
+
# Hash:: field names keyed to field objects
|
140
|
+
#
|
141
|
+
def fields_hash
|
142
|
+
@fields_hash ||=
|
143
|
+
begin
|
144
|
+
field_sets_hash = Hash.new { |h, k| h[k] = Set.new }
|
145
|
+
@types.each do |type|
|
146
|
+
Setup.for(type).fields.each do |field|
|
147
|
+
field_sets_hash[field.name.to_sym] << field
|
148
|
+
end
|
149
|
+
end
|
150
|
+
fields_hash = {}
|
151
|
+
field_sets_hash.each_pair do |field_name, set|
|
152
|
+
if set.length == 1
|
153
|
+
fields_hash[field_name] = set.to_a.first
|
154
|
+
end
|
155
|
+
end
|
156
|
+
fields_hash
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
#
|
161
|
+
# Return a hash of dynamic field base names to dynamic field factories for
|
162
|
+
# those base names. Criteria for the inclusion are the same as for
|
163
|
+
# #fields_hash()
|
164
|
+
#
|
165
|
+
def dynamic_field_factories_hash
|
166
|
+
@dynamic_field_factories_hash ||=
|
167
|
+
begin
|
168
|
+
dynamic_field_factories_hash = @types.inject({}) do |hash, type|
|
169
|
+
Setup.for(type).dynamic_field_factories.each do |field_factory|
|
170
|
+
(hash[field_factory.name.to_sym] ||= {})[type.name] = field_factory
|
171
|
+
end
|
172
|
+
hash
|
173
|
+
end
|
174
|
+
dynamic_field_factories_hash.each_pair do |field_name, field_configurations_hash|
|
175
|
+
if @types.any? { |type| field_configurations_hash[type.name].nil? }
|
176
|
+
dynamic_field_factories_hash.delete(field_name)
|
177
|
+
else
|
178
|
+
dynamic_field_factories_hash[field_name] = field_configurations_hash.values.first
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module Sunspot
|
2
|
+
# The Sunspot::Configuration module provides a factory method for Sunspot
|
3
|
+
# configuration objects. Available properties are:
|
4
|
+
#
|
5
|
+
# Sunspot.config.http_client::
|
6
|
+
# The client to use for HTTP communication with Solr. Available options are
|
7
|
+
# :net_http, which is the default and uses Ruby's built-in pure-Ruby HTTP
|
8
|
+
# library; and :curb, which uses Ruby's libcurl bindings and requires
|
9
|
+
# installation of the 'curb' gem.
|
10
|
+
# Sunspot.config.solr.url::
|
11
|
+
# The URL at which to connect to Solr
|
12
|
+
# (default: 'http://localhost:8983/solr')
|
13
|
+
# Sunspot.config.pagination.default_per_page::
|
14
|
+
# Solr always paginates its results. This sets Sunspot's default result
|
15
|
+
# count per page if it is not explicitly specified in the query.
|
16
|
+
#
|
17
|
+
module Configuration
|
18
|
+
class <<self
|
19
|
+
# Factory method to build configuration instances.
|
20
|
+
#
|
21
|
+
# ==== Returns
|
22
|
+
#
|
23
|
+
# LightConfig::Configuration:: new configuration instance with defaults
|
24
|
+
#
|
25
|
+
def build #:nodoc:
|
26
|
+
LightConfig.build do
|
27
|
+
http_client :net_http
|
28
|
+
solr do
|
29
|
+
url 'http://127.0.0.1:8983/solr'
|
30
|
+
end
|
31
|
+
pagination do
|
32
|
+
default_per_page 30
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# Location for the default solr configuration files,
|
38
|
+
# required for bootstrapping a new solr installation
|
39
|
+
#
|
40
|
+
# ==== Returns
|
41
|
+
#
|
42
|
+
# String:: Directory with default solr config files
|
43
|
+
#
|
44
|
+
def solr_default_configuration_location
|
45
|
+
File.join( File.dirname(__FILE__), '../../solr/solr/conf' )
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module Sunspot
|
2
|
+
#
|
3
|
+
# DataExtractors present an internal API for the indexer to use to extract
|
4
|
+
# field values from models for indexing. They must implement the #value_for
|
5
|
+
# method, which takes an object and returns the value extracted from it.
|
6
|
+
#
|
7
|
+
module DataExtractor #:nodoc: all
|
8
|
+
#
|
9
|
+
# AttributeExtractors extract data by simply calling a method on the block.
|
10
|
+
#
|
11
|
+
class AttributeExtractor
|
12
|
+
def initialize(attribute_name)
|
13
|
+
@attribute_name = attribute_name
|
14
|
+
end
|
15
|
+
|
16
|
+
def value_for(object)
|
17
|
+
object.send(@attribute_name)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
#
|
22
|
+
# BlockExtractors extract data by evaluating a block in the context of the
|
23
|
+
# object instance, or if the block takes an argument, by passing the object
|
24
|
+
# as the argument to the block. Either way, the return value of the block is
|
25
|
+
# the value returned by the extractor.
|
26
|
+
#
|
27
|
+
class BlockExtractor
|
28
|
+
def initialize(&block)
|
29
|
+
@block = block
|
30
|
+
end
|
31
|
+
|
32
|
+
def value_for(object)
|
33
|
+
Util.instance_eval_or_call(object, &@block)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
#
|
38
|
+
# Constant data extractors simply return the same value for every object.
|
39
|
+
#
|
40
|
+
class Constant
|
41
|
+
def initialize(value)
|
42
|
+
@value = value
|
43
|
+
end
|
44
|
+
|
45
|
+
def value_for(object)
|
46
|
+
@value
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
module Sunspot
|
2
|
+
module DSL
|
3
|
+
#
|
4
|
+
# Provides an API for areas of the query DSL that operate on specific
|
5
|
+
# fields. This functionality is provided by the query DSL and the dynamic
|
6
|
+
# query DSL.
|
7
|
+
#
|
8
|
+
class FieldQuery < Scope
|
9
|
+
# Specify the order that results should be returned in. This method can
|
10
|
+
# be called multiple times; precedence will be in the order given.
|
11
|
+
#
|
12
|
+
# ==== Parameters
|
13
|
+
#
|
14
|
+
# field_name<Symbol>:: the field to use for ordering
|
15
|
+
# direction<Symbol>:: :asc or :desc (default :asc)
|
16
|
+
#
|
17
|
+
def order_by(field_name, direction = nil)
|
18
|
+
@query.order_by(field_name, direction)
|
19
|
+
end
|
20
|
+
|
21
|
+
#
|
22
|
+
# DEPRECATED Use <code>order_by(:random)</code>
|
23
|
+
#
|
24
|
+
def order_by_random
|
25
|
+
@query.order_by(:random)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Request facets on the given field names. If the last argument is a hash,
|
29
|
+
# the given options will be applied to all specified fields. See
|
30
|
+
# Sunspot::Search#facet and Sunspot::Facet for information on what is
|
31
|
+
# returned.
|
32
|
+
#
|
33
|
+
# ==== Parameters
|
34
|
+
#
|
35
|
+
# field_names...<Symbol>:: fields for which to return field facets
|
36
|
+
#
|
37
|
+
# ==== Options
|
38
|
+
#
|
39
|
+
# :sort<Symbol>::
|
40
|
+
# Either :count (values matching the most terms first) or :index (lexical)
|
41
|
+
# :limit<Integer>::
|
42
|
+
# The maximum number of facet rows to return
|
43
|
+
# :minimum_count<Integer>::
|
44
|
+
# The minimum count a facet row must have to be returned
|
45
|
+
# :zeros<Boolean>::
|
46
|
+
# Return facet rows for which there are no matches (equivalent to
|
47
|
+
# :minimum_count => 0). Default is false.
|
48
|
+
#
|
49
|
+
def facet(*field_names, &block)
|
50
|
+
if block
|
51
|
+
options =
|
52
|
+
if field_names.last.is_a?(Hash)
|
53
|
+
field_names.pop
|
54
|
+
else
|
55
|
+
{}
|
56
|
+
end
|
57
|
+
if field_names.length != 1
|
58
|
+
raise(
|
59
|
+
ArgumentError,
|
60
|
+
"wrong number of arguments (#{field_names.length} for 1)"
|
61
|
+
)
|
62
|
+
end
|
63
|
+
name = field_names.first
|
64
|
+
DSL::QueryFacet.new(@query.add_query_facet(name, options)).instance_eval(&block)
|
65
|
+
else
|
66
|
+
options =
|
67
|
+
if field_names.last.is_a?(Hash)
|
68
|
+
field_names.pop
|
69
|
+
end
|
70
|
+
for field_name in field_names
|
71
|
+
@query.add_field_facet(field_name, options)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|