erichummel-sunspot 1.2.1a
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +10 -0
- data/Gemfile.lock +32 -0
- data/History.txt +222 -0
- data/LICENSE +18 -0
- data/Rakefile +11 -0
- data/TODO +13 -0
- data/VERSION.yml +4 -0
- data/bin/sunspot-installer +19 -0
- data/bin/sunspot-solr +74 -0
- data/installer/config/schema.yml +95 -0
- data/lib/light_config.rb +40 -0
- data/lib/sunspot/adapters.rb +265 -0
- data/lib/sunspot/composite_setup.rb +202 -0
- data/lib/sunspot/configuration.rb +46 -0
- data/lib/sunspot/data_extractor.rb +50 -0
- data/lib/sunspot/dsl/adjustable.rb +47 -0
- data/lib/sunspot/dsl/field_query.rb +266 -0
- data/lib/sunspot/dsl/fields.rb +103 -0
- data/lib/sunspot/dsl/fulltext.rb +243 -0
- data/lib/sunspot/dsl/function.rb +14 -0
- data/lib/sunspot/dsl/functional.rb +41 -0
- data/lib/sunspot/dsl/more_like_this_query.rb +56 -0
- data/lib/sunspot/dsl/paginatable.rb +28 -0
- data/lib/sunspot/dsl/query_facet.rb +36 -0
- data/lib/sunspot/dsl/restriction.rb +25 -0
- data/lib/sunspot/dsl/restriction_with_near.rb +121 -0
- data/lib/sunspot/dsl/scope.rb +217 -0
- data/lib/sunspot/dsl/search.rb +30 -0
- data/lib/sunspot/dsl/standard_query.rb +121 -0
- data/lib/sunspot/dsl.rb +5 -0
- data/lib/sunspot/field.rb +193 -0
- data/lib/sunspot/field_factory.rb +129 -0
- data/lib/sunspot/indexer.rb +131 -0
- data/lib/sunspot/installer/library_installer.rb +45 -0
- data/lib/sunspot/installer/schema_builder.rb +219 -0
- data/lib/sunspot/installer/solrconfig_updater.rb +76 -0
- data/lib/sunspot/installer/task_helper.rb +18 -0
- data/lib/sunspot/installer.rb +31 -0
- data/lib/sunspot/query/abstract_field_facet.rb +52 -0
- data/lib/sunspot/query/boost_query.rb +24 -0
- data/lib/sunspot/query/common_query.rb +85 -0
- data/lib/sunspot/query/composite_fulltext.rb +36 -0
- data/lib/sunspot/query/connective.rb +206 -0
- data/lib/sunspot/query/date_field_facet.rb +14 -0
- data/lib/sunspot/query/dismax.rb +128 -0
- data/lib/sunspot/query/field_facet.rb +41 -0
- data/lib/sunspot/query/filter.rb +38 -0
- data/lib/sunspot/query/function_query.rb +52 -0
- data/lib/sunspot/query/geo.rb +53 -0
- data/lib/sunspot/query/highlighting.rb +55 -0
- data/lib/sunspot/query/more_like_this.rb +61 -0
- data/lib/sunspot/query/more_like_this_query.rb +12 -0
- data/lib/sunspot/query/pagination.rb +38 -0
- data/lib/sunspot/query/query_facet.rb +16 -0
- data/lib/sunspot/query/restriction.rb +262 -0
- data/lib/sunspot/query/scope.rb +9 -0
- data/lib/sunspot/query/sort.rb +95 -0
- data/lib/sunspot/query/sort_composite.rb +33 -0
- data/lib/sunspot/query/standard_query.rb +16 -0
- data/lib/sunspot/query/text_field_boost.rb +17 -0
- data/lib/sunspot/query.rb +11 -0
- data/lib/sunspot/schema.rb +151 -0
- data/lib/sunspot/search/abstract_search.rb +296 -0
- data/lib/sunspot/search/date_facet.rb +35 -0
- data/lib/sunspot/search/facet_row.rb +27 -0
- data/lib/sunspot/search/field_facet.rb +88 -0
- data/lib/sunspot/search/highlight.rb +38 -0
- data/lib/sunspot/search/hit.rb +136 -0
- data/lib/sunspot/search/more_like_this_search.rb +31 -0
- data/lib/sunspot/search/query_facet.rb +67 -0
- data/lib/sunspot/search/standard_search.rb +21 -0
- data/lib/sunspot/search.rb +9 -0
- data/lib/sunspot/server.rb +152 -0
- data/lib/sunspot/session.rb +260 -0
- data/lib/sunspot/session_proxy/abstract_session_proxy.rb +29 -0
- data/lib/sunspot/session_proxy/class_sharding_session_proxy.rb +66 -0
- data/lib/sunspot/session_proxy/id_sharding_session_proxy.rb +89 -0
- data/lib/sunspot/session_proxy/master_slave_session_proxy.rb +43 -0
- data/lib/sunspot/session_proxy/sharding_session_proxy.rb +222 -0
- data/lib/sunspot/session_proxy/silent_fail_session_proxy.rb +42 -0
- data/lib/sunspot/session_proxy/thread_local_session_proxy.rb +37 -0
- data/lib/sunspot/session_proxy.rb +87 -0
- data/lib/sunspot/setup.rb +350 -0
- data/lib/sunspot/text_field_setup.rb +29 -0
- data/lib/sunspot/type.rb +372 -0
- data/lib/sunspot/util.rb +243 -0
- data/lib/sunspot/version.rb +3 -0
- data/lib/sunspot.rb +569 -0
- data/solr/etc/jetty.xml +214 -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/admin-extra.html +31 -0
- data/solr/solr/conf/elevate.xml +36 -0
- data/solr/solr/conf/mapping-ISOLatin1Accent.txt +246 -0
- data/solr/solr/conf/protwords.txt +21 -0
- data/solr/solr/conf/schema.xml +238 -0
- data/solr/solr/conf/scripts.conf +24 -0
- data/solr/solr/conf/solrconfig.xml +934 -0
- data/solr/solr/conf/spellings.txt +2 -0
- data/solr/solr/conf/stopwords.txt +58 -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/binding_spec.rb +50 -0
- data/spec/api/indexer/attributes_spec.rb +149 -0
- data/spec/api/indexer/batch_spec.rb +46 -0
- data/spec/api/indexer/dynamic_fields_spec.rb +42 -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 +53 -0
- data/spec/api/indexer/spec_helper.rb +1 -0
- data/spec/api/indexer_spec.rb +14 -0
- data/spec/api/query/advanced_manipulation_examples.rb +35 -0
- data/spec/api/query/connectives_examples.rb +189 -0
- data/spec/api/query/dsl_spec.rb +18 -0
- data/spec/api/query/dynamic_fields_examples.rb +165 -0
- data/spec/api/query/faceting_examples.rb +399 -0
- data/spec/api/query/fulltext_examples.rb +315 -0
- data/spec/api/query/function_spec.rb +70 -0
- data/spec/api/query/geo_examples.rb +69 -0
- data/spec/api/query/highlighting_examples.rb +225 -0
- data/spec/api/query/more_like_this_spec.rb +140 -0
- data/spec/api/query/ordering_pagination_examples.rb +97 -0
- data/spec/api/query/scope_examples.rb +275 -0
- data/spec/api/query/spec_helper.rb +1 -0
- data/spec/api/query/standard_spec.rb +28 -0
- data/spec/api/query/text_field_scoping_examples.rb +30 -0
- data/spec/api/query/types_spec.rb +20 -0
- data/spec/api/search/dynamic_fields_spec.rb +33 -0
- data/spec/api/search/faceting_spec.rb +360 -0
- data/spec/api/search/highlighting_spec.rb +69 -0
- data/spec/api/search/hits_spec.rb +140 -0
- data/spec/api/search/results_spec.rb +79 -0
- data/spec/api/search/search_spec.rb +23 -0
- data/spec/api/search/spec_helper.rb +1 -0
- data/spec/api/server_spec.rb +91 -0
- data/spec/api/session_proxy/class_sharding_session_proxy_spec.rb +85 -0
- data/spec/api/session_proxy/id_sharding_session_proxy_spec.rb +30 -0
- data/spec/api/session_proxy/master_slave_session_proxy_spec.rb +41 -0
- data/spec/api/session_proxy/sharding_session_proxy_spec.rb +77 -0
- data/spec/api/session_proxy/silent_fail_session_proxy_spec.rb +24 -0
- data/spec/api/session_proxy/spec_helper.rb +9 -0
- data/spec/api/session_proxy/thread_local_session_proxy_spec.rb +50 -0
- data/spec/api/session_spec.rb +220 -0
- data/spec/api/spec_helper.rb +3 -0
- data/spec/api/sunspot_spec.rb +18 -0
- data/spec/ext.rb +11 -0
- data/spec/helpers/indexer_helper.rb +29 -0
- data/spec/helpers/query_helper.rb +38 -0
- data/spec/helpers/search_helper.rb +80 -0
- data/spec/integration/dynamic_fields_spec.rb +55 -0
- data/spec/integration/faceting_spec.rb +238 -0
- data/spec/integration/highlighting_spec.rb +22 -0
- data/spec/integration/indexing_spec.rb +33 -0
- data/spec/integration/keyword_search_spec.rb +317 -0
- data/spec/integration/local_search_spec.rb +64 -0
- data/spec/integration/more_like_this_spec.rb +43 -0
- data/spec/integration/scoped_search_spec.rb +354 -0
- data/spec/integration/spec_helper.rb +7 -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 +21 -0
- data/spec/mocks/connection.rb +126 -0
- data/spec/mocks/mock_adapter.rb +30 -0
- data/spec/mocks/mock_class_sharding_session_proxy.rb +24 -0
- data/spec/mocks/mock_record.rb +52 -0
- data/spec/mocks/mock_sharding_session_proxy.rb +15 -0
- data/spec/mocks/photo.rb +11 -0
- data/spec/mocks/post.rb +85 -0
- data/spec/mocks/super_class.rb +2 -0
- data/spec/mocks/user.rb +13 -0
- data/spec/spec_helper.rb +42 -0
- data/tasks/rdoc.rake +27 -0
- data/tasks/schema.rake +19 -0
- data/tasks/todo.rake +4 -0
- metadata +342 -0
data/lib/sunspot.rb
ADDED
@@ -0,0 +1,569 @@
|
|
1
|
+
require 'set'
|
2
|
+
require 'time'
|
3
|
+
require 'date'
|
4
|
+
require 'enumerator'
|
5
|
+
require 'cgi'
|
6
|
+
begin
|
7
|
+
require 'rsolr'
|
8
|
+
rescue LoadError
|
9
|
+
require 'rubygems'
|
10
|
+
require 'rsolr'
|
11
|
+
end
|
12
|
+
|
13
|
+
require File.join(File.dirname(__FILE__), 'light_config')
|
14
|
+
|
15
|
+
%w(util adapters configuration setup composite_setup text_field_setup field
|
16
|
+
field_factory data_extractor indexer query search session session_proxy
|
17
|
+
type dsl).each do |filename|
|
18
|
+
require File.join(File.dirname(__FILE__), 'sunspot', filename)
|
19
|
+
end
|
20
|
+
|
21
|
+
#
|
22
|
+
# The Sunspot module provides class-method entry points to most of the
|
23
|
+
# functionality provided by the Sunspot library. Internally, the Sunspot
|
24
|
+
# singleton class contains a (non-thread-safe!) instance of Sunspot::Session,
|
25
|
+
# to which it delegates most of the class methods it exposes. In the method
|
26
|
+
# documentation below, this instance is referred to as the "singleton session".
|
27
|
+
#
|
28
|
+
# Though the singleton session provides a convenient entry point to Sunspot,
|
29
|
+
# it is by no means required to use the Sunspot class methods. Multiple sessions
|
30
|
+
# may be instantiated and used (if you need to connect to multiple Solr
|
31
|
+
# instances, for example.)
|
32
|
+
#
|
33
|
+
# Note that the configuration of classes for index/search (the +setup+
|
34
|
+
# method) is _not_ session-specific, but rather global.
|
35
|
+
#
|
36
|
+
module Sunspot
|
37
|
+
UnrecognizedFieldError = Class.new(StandardError)
|
38
|
+
UnrecognizedRestrictionError = Class.new(StandardError)
|
39
|
+
NoAdapterError = Class.new(StandardError)
|
40
|
+
NoSetupError = Class.new(StandardError)
|
41
|
+
IllegalSearchError = Class.new(StandardError)
|
42
|
+
NotImplementedError = Class.new(StandardError)
|
43
|
+
|
44
|
+
autoload :Server, File.join(File.dirname(__FILE__), 'sunspot', 'server')
|
45
|
+
autoload :Installer, File.join(File.dirname(__FILE__), 'sunspot', 'installer')
|
46
|
+
|
47
|
+
class <<self
|
48
|
+
#
|
49
|
+
# Clients can inject a session proxy, allowing them to implement custom
|
50
|
+
# session-management logic while retaining the Sunspot singleton API as
|
51
|
+
# an available interface. The object assigned to this attribute must
|
52
|
+
# respond to all of the public methods of the Sunspot::Session class.
|
53
|
+
#
|
54
|
+
attr_writer :session
|
55
|
+
|
56
|
+
# Configures indexing and search for a given class.
|
57
|
+
#
|
58
|
+
# ==== Parameters
|
59
|
+
#
|
60
|
+
# clazz<Class>:: class to configure
|
61
|
+
#
|
62
|
+
# ==== Example
|
63
|
+
#
|
64
|
+
# Sunspot.setup(Post) do
|
65
|
+
# text :title, :body
|
66
|
+
# string :author_name
|
67
|
+
# integer :blog_id
|
68
|
+
# integer :category_ids
|
69
|
+
# float :average_rating, :using => :ratings_average
|
70
|
+
# time :published_at
|
71
|
+
# string :sort_title do
|
72
|
+
# title.downcase.sub(/^(an?|the)\W+/, ''/) if title = self.title
|
73
|
+
# end
|
74
|
+
# end
|
75
|
+
#
|
76
|
+
# ====== Attribute Fields vs. Virtual Fields
|
77
|
+
#
|
78
|
+
# Attribute fields call a method on the indexed object and index the
|
79
|
+
# return value. All of the fields defined above except for the last one are
|
80
|
+
# attribute fields. By default, the field name will also be the attribute
|
81
|
+
# used; this can be overriden with the +:using+ option, as in
|
82
|
+
# +:average_rating+ above. In that case, the attribute +:ratings_average+
|
83
|
+
# will be indexed with the field name +:average_rating+.
|
84
|
+
#
|
85
|
+
# +:sort_title+ is a virtual field, which evaluates the block inside the
|
86
|
+
# context of the instance being indexed, and indexes the value returned
|
87
|
+
# by the block. If the block you pass takes an argument, it will be passed
|
88
|
+
# the instance rather than being evaluated inside of it; so, the following
|
89
|
+
# example is equivalent to the one above (assuming #title is public):
|
90
|
+
#
|
91
|
+
# Sunspot.setup(Post) do
|
92
|
+
# string :sort_title do |post|
|
93
|
+
# post.title.downcase.sub(/^(an?|the)\W+/, ''/) if title = self.title
|
94
|
+
# end
|
95
|
+
# end
|
96
|
+
#
|
97
|
+
# ===== Field Types
|
98
|
+
#
|
99
|
+
# The available types are:
|
100
|
+
#
|
101
|
+
# * +text+
|
102
|
+
# * +string+
|
103
|
+
# * +integer+
|
104
|
+
# * +float+
|
105
|
+
# * +time+
|
106
|
+
# * +boolean+
|
107
|
+
#
|
108
|
+
# Note that the +text+ type behaves quite differently from the others -
|
109
|
+
# this is the type that is indexed as fulltext, and is searched using the
|
110
|
+
# +keywords+ method inside the search DSL. Text fields cannot have
|
111
|
+
# restrictions set on them, nor can they be used in order statements or
|
112
|
+
# for facets. All other types are indexed literally, and thus can be used
|
113
|
+
# for all of those operations. They will not, however, be searched in
|
114
|
+
# fulltext. In this way, Sunspot provides a complete barrier between
|
115
|
+
# fulltext fields and value fields.
|
116
|
+
#
|
117
|
+
# It is fine to specify a field both as a text field and a string field;
|
118
|
+
# internally, the fields will have different names so there is no danger
|
119
|
+
# of conflict.
|
120
|
+
#
|
121
|
+
# ===== Dynamic Fields
|
122
|
+
#
|
123
|
+
# For use cases which have highly dynamic data models (for instance, an
|
124
|
+
# open set of key-value pairs attached to a model), it may be useful to
|
125
|
+
# defer definition of fields until indexing time. Sunspot exposes dynamic
|
126
|
+
# fields, which define a data accessor (either attribute or virtual, see
|
127
|
+
# above), which accepts a hash of field names to values. Note that the field
|
128
|
+
# names in the hash are internally scoped to the base name of the dynamic
|
129
|
+
# field, so any time they are referred to, they are referred to using both
|
130
|
+
# the base name and the dynamic (runtime-specified) name.
|
131
|
+
#
|
132
|
+
# Dynamic fields are speficied in the setup block using the type name
|
133
|
+
# prefixed by +dynamic_+. For example:
|
134
|
+
#
|
135
|
+
# Sunspot.setup(Post) do
|
136
|
+
# dynamic_string :custom_values do
|
137
|
+
# key_value_pairs.inject({}) do |hash, key_value_pair|
|
138
|
+
# hash[key_value_pair.key.to_sym] = key_value_pair.value
|
139
|
+
# end
|
140
|
+
# end
|
141
|
+
# end
|
142
|
+
#
|
143
|
+
# If you later wanted to facet all of the values for the key "cuisine",
|
144
|
+
# you could issue:
|
145
|
+
#
|
146
|
+
# Sunspot.search(Post) do
|
147
|
+
# dynamic :custom_values do
|
148
|
+
# facet :cuisine
|
149
|
+
# end
|
150
|
+
# end
|
151
|
+
#
|
152
|
+
# In the documentation, +:custom_values+ is referred to as the "base name" -
|
153
|
+
# that is, the one specified statically - and +:cuisine+ is referred to as
|
154
|
+
# the dynamic name, which is the part that is specified at indexing time.
|
155
|
+
#
|
156
|
+
def setup(clazz, &block)
|
157
|
+
Setup.setup(clazz, &block)
|
158
|
+
end
|
159
|
+
|
160
|
+
# Indexes objects on the singleton session.
|
161
|
+
#
|
162
|
+
# ==== Parameters
|
163
|
+
#
|
164
|
+
# objects...<Object>:: objects to index (may pass an array or varargs)
|
165
|
+
#
|
166
|
+
# ==== Example
|
167
|
+
#
|
168
|
+
# post1, post2 = new Array(2) { Post.create }
|
169
|
+
# Sunspot.index(post1, post2)
|
170
|
+
#
|
171
|
+
# Note that indexed objects won't be reflected in search until a commit is
|
172
|
+
# sent - see Sunspot.index! and Sunspot.commit
|
173
|
+
#
|
174
|
+
def index(*objects)
|
175
|
+
session.index(*objects)
|
176
|
+
end
|
177
|
+
|
178
|
+
# Indexes objects on the singleton session and commits immediately.
|
179
|
+
#
|
180
|
+
# See: Sunspot.index and Sunspot.commit
|
181
|
+
#
|
182
|
+
# ==== Parameters
|
183
|
+
#
|
184
|
+
# objects...<Object>:: objects to index (may pass an array or varargs)
|
185
|
+
#
|
186
|
+
def index!(*objects)
|
187
|
+
session.index!(*objects)
|
188
|
+
end
|
189
|
+
|
190
|
+
# Commits the singleton session
|
191
|
+
#
|
192
|
+
# When documents are added to or removed from Solr, the changes are
|
193
|
+
# initially stored in memory, and are not reflected in Solr's existing
|
194
|
+
# searcher instance. When a commit message is sent, the changes are written
|
195
|
+
# to disk, and a new searcher is spawned. Commits are thus fairly
|
196
|
+
# expensive, so if your application needs to index several documents as part
|
197
|
+
# of a single operation, it is advisable to index them all and then call
|
198
|
+
# commit at the end of the operation.
|
199
|
+
#
|
200
|
+
# Note that Solr can also be configured to automatically perform a commit
|
201
|
+
# after either a specified interval after the last change, or after a
|
202
|
+
# specified number of documents are added. See
|
203
|
+
# http://wiki.apache.org/solr/SolrConfigXml
|
204
|
+
#
|
205
|
+
def commit
|
206
|
+
session.commit
|
207
|
+
end
|
208
|
+
|
209
|
+
# Optimizes the index on the singletion session.
|
210
|
+
#
|
211
|
+
# Frequently adding and deleting documents to Solr, leaves the index in a
|
212
|
+
# fragmented state. The optimize command merges all index segments into
|
213
|
+
# a single segment and removes any deleted documents, making it faster to
|
214
|
+
# search. Since optimize rebuilds the index from scratch, it takes some
|
215
|
+
# time and requires double the space on the hard disk while it's rebuilding.
|
216
|
+
# Note that optimize also commits.
|
217
|
+
def optimize
|
218
|
+
session.optimize
|
219
|
+
end
|
220
|
+
|
221
|
+
#
|
222
|
+
# Create a new Search instance, but do not execute it immediately. Generally
|
223
|
+
# you will want to use the #search method to build and execute searches in
|
224
|
+
# one step, but if you are building searches piecemeal you may call
|
225
|
+
# #new_search and then call #build one or more times to add components to
|
226
|
+
# the query.
|
227
|
+
#
|
228
|
+
# ==== Example
|
229
|
+
#
|
230
|
+
# search = Sunspot.new_search do
|
231
|
+
# with(:blog_id, 1)
|
232
|
+
# end
|
233
|
+
# search.build do
|
234
|
+
# keywords('some keywords')
|
235
|
+
# end
|
236
|
+
# search.build do
|
237
|
+
# order_by(:published_at, :desc)
|
238
|
+
# end
|
239
|
+
# search.execute
|
240
|
+
#
|
241
|
+
# # This is equivalent to:
|
242
|
+
# Sunspot.search do
|
243
|
+
# with(:blog_id, 1)
|
244
|
+
# keywords('some keywords')
|
245
|
+
# order_by(:published_at, :desc)
|
246
|
+
# end
|
247
|
+
#
|
248
|
+
# ==== Parameters
|
249
|
+
#
|
250
|
+
# types<Class>...::
|
251
|
+
# One or more types to search for. If no types are passed, all
|
252
|
+
# configured types will be searched for.
|
253
|
+
#
|
254
|
+
# ==== Returns
|
255
|
+
#
|
256
|
+
# Sunspot::Search::
|
257
|
+
# Search object, not yet executed. Query parameters can be added manually;
|
258
|
+
# then #execute should be called.
|
259
|
+
#
|
260
|
+
def new_search(*types, &block)
|
261
|
+
session.new_search(*types, &block)
|
262
|
+
end
|
263
|
+
|
264
|
+
|
265
|
+
# Search for objects in the index.
|
266
|
+
#
|
267
|
+
# ==== Parameters
|
268
|
+
#
|
269
|
+
# types<Class>...::
|
270
|
+
# One or more types to search for. If no types are passed, all
|
271
|
+
# configured types will be searched.
|
272
|
+
#
|
273
|
+
# ==== Returns
|
274
|
+
#
|
275
|
+
# Sunspot::Search:: Object containing results, facets, count, etc.
|
276
|
+
#
|
277
|
+
# The fields available for restriction, ordering, etc. are those that meet
|
278
|
+
# the following criteria:
|
279
|
+
#
|
280
|
+
# * They are not of type +text+.
|
281
|
+
# * They are defined for at least one of the classes being searched
|
282
|
+
# * They have the same data type for all of the classes being searched.
|
283
|
+
# * They have the same multiple flag for all of the classes being searched.
|
284
|
+
# * They have the same stored flag for all of the classes being searched.
|
285
|
+
#
|
286
|
+
# The restrictions available are the constants defined in the
|
287
|
+
# Sunspot::Restriction class. The standard restrictions are:
|
288
|
+
#
|
289
|
+
# with(:field_name).equal_to(value)
|
290
|
+
# with(:field_name, value) # shorthand for above
|
291
|
+
# with(:field_name).less_than(value)
|
292
|
+
# with(:field_name).greater_than(value)
|
293
|
+
# with(:field_name).between(value1..value2)
|
294
|
+
# with(:field_name).any_of([value1, value2, value3])
|
295
|
+
# with(:field_name).all_of([value1, value2, value3])
|
296
|
+
# without(some_instance) # exclude that particular instance
|
297
|
+
#
|
298
|
+
# +without+ can be substituted for +with+, causing the restriction to be
|
299
|
+
# negated. In the last example above, only +without+ works, as it does not
|
300
|
+
# make sense to search only for an instance you already have.
|
301
|
+
#
|
302
|
+
# Equality restrictions can take +nil+ as a value, which restricts the
|
303
|
+
# results to documents that have no value for the given field. Passing +nil+
|
304
|
+
# as a value to other restriction types is illegal. Thus:
|
305
|
+
#
|
306
|
+
# with(:field_name, nil) # ok
|
307
|
+
# with(:field_name).equal_to(nil) # ok
|
308
|
+
# with(:field_name).less_than(nil) # bad
|
309
|
+
#
|
310
|
+
# ==== Example
|
311
|
+
#
|
312
|
+
# Sunspot.search(Post) do
|
313
|
+
# keywords 'great pizza'
|
314
|
+
# with(:published_at).less_than Time.now
|
315
|
+
# with :blog_id, 1
|
316
|
+
# without current_post
|
317
|
+
# facet :category_ids
|
318
|
+
# order_by :published_at, :desc
|
319
|
+
# paginate 2, 15
|
320
|
+
# end
|
321
|
+
#
|
322
|
+
# If the block passed to #search takes an argument, that argument will
|
323
|
+
# present the DSL, and the block will be evaluated in the calling context.
|
324
|
+
# This will come in handy for building searches using instance data or
|
325
|
+
# methods, e.g.:
|
326
|
+
#
|
327
|
+
# Sunspot.search(Post) do |query|
|
328
|
+
# query.with(:blog_id, @current_blog.id)
|
329
|
+
# end
|
330
|
+
#
|
331
|
+
# See Sunspot::DSL::Search, Sunspot::DSL::Scope, Sunspot::DSL::FieldQuery
|
332
|
+
# and Sunspot::DSL::Query for the full API presented inside the block.
|
333
|
+
#
|
334
|
+
def search(*types, &block)
|
335
|
+
session.search(*types, &block)
|
336
|
+
end
|
337
|
+
|
338
|
+
def new_more_like_this(object, *types, &block)
|
339
|
+
session.new_more_like_this(object, *types, &block)
|
340
|
+
end
|
341
|
+
|
342
|
+
#
|
343
|
+
# Initiate a MoreLikeThis search. MoreLikeThis is a special type of search
|
344
|
+
# that finds similar documents using fulltext comparison. The fields to be
|
345
|
+
# compared are `text` fields set up with the `:more_like_this` option set to
|
346
|
+
# `true`. By default, more like this returns objects of the same type as the
|
347
|
+
# object used for comparison, but a list of types can optionally be passed
|
348
|
+
# to this method to return similar documents of other types. This will only
|
349
|
+
# work for types that have common fields.
|
350
|
+
#
|
351
|
+
# The DSL for MoreLikeThis search exposes several methods for setting
|
352
|
+
# options specific to this type of search. See the
|
353
|
+
# Sunspot::DSL::MoreLikeThis class and the MoreLikeThis documentation on
|
354
|
+
# the Solr wiki: http://wiki.apache.org/solr/MoreLikeThis
|
355
|
+
#
|
356
|
+
# MoreLikeThis searches have all of the same scoping, ordering, and faceting
|
357
|
+
# functionality as standard searches; the only thing you can't do in a MLT
|
358
|
+
# search is fulltext matching (since the MLT itself is a fulltext query).
|
359
|
+
#
|
360
|
+
# ==== Example
|
361
|
+
#
|
362
|
+
# post = Post.first
|
363
|
+
# Sunspot.more_like_this(post, Post, Page) do
|
364
|
+
# fields :title, :body
|
365
|
+
# with(:updated_at).greater_than(1.month.ago)
|
366
|
+
# facet(:category_ids)
|
367
|
+
# end
|
368
|
+
#
|
369
|
+
#
|
370
|
+
def more_like_this(object, *types, &block)
|
371
|
+
session.more_like_this(object, *types, &block)
|
372
|
+
end
|
373
|
+
|
374
|
+
# Remove objects from the index. Any time an object is destroyed, it must
|
375
|
+
# be removed from the index; otherwise, the index will contain broken
|
376
|
+
# references to objects that do not exist, which will cause errors when
|
377
|
+
# those objects are matched in search results.
|
378
|
+
#
|
379
|
+
# If a block is passed, it is evaluated as a search scope; in this way,
|
380
|
+
# documents can be removed by an arbitrary query. In this case, the
|
381
|
+
# arguments to the method should be the classes to run the query on.
|
382
|
+
#
|
383
|
+
# ==== Parameters
|
384
|
+
#
|
385
|
+
# objects...<Object>::
|
386
|
+
# Objects to remove from the index (may pass an array or varargs)
|
387
|
+
#
|
388
|
+
# ==== Example (remove a document)
|
389
|
+
#
|
390
|
+
# post.destroy
|
391
|
+
# Sunspot.remove(post)
|
392
|
+
#
|
393
|
+
# ==== Example (remove by query)
|
394
|
+
#
|
395
|
+
# Sunspot.remove(Post) do
|
396
|
+
# with(:created_at).less_than(Time.now - 14.days)
|
397
|
+
# end
|
398
|
+
#
|
399
|
+
def remove(*objects, &block)
|
400
|
+
session.remove(*objects, &block)
|
401
|
+
end
|
402
|
+
|
403
|
+
#
|
404
|
+
# Remove objects from the index and immediately commit. See Sunspot.remove
|
405
|
+
#
|
406
|
+
# ==== Parameters
|
407
|
+
#
|
408
|
+
# objects...<Object>:: Objects to remove from the index
|
409
|
+
#
|
410
|
+
def remove!(*objects)
|
411
|
+
session.remove!(*objects)
|
412
|
+
end
|
413
|
+
|
414
|
+
#
|
415
|
+
# Remove an object from the index using its class name and primary key.
|
416
|
+
# Useful if you know this information and want to remove an object without
|
417
|
+
# instantiating it from persistent storage
|
418
|
+
#
|
419
|
+
# ==== Parameters
|
420
|
+
#
|
421
|
+
# clazz<Class>:: Class of the object, or class name as a string or symbol
|
422
|
+
# id::
|
423
|
+
# Primary key of the object. This should be the same id that would be
|
424
|
+
# returned by the class's instance adapter.
|
425
|
+
#
|
426
|
+
def remove_by_id(clazz, id)
|
427
|
+
session.remove_by_id(clazz, id)
|
428
|
+
end
|
429
|
+
|
430
|
+
#
|
431
|
+
# Remove an object by class name and primary key, and immediately commit.
|
432
|
+
# See #remove_by_id and #commit
|
433
|
+
#
|
434
|
+
def remove_by_id!(clazz, id)
|
435
|
+
session.remove_by_id!(clazz, id)
|
436
|
+
end
|
437
|
+
|
438
|
+
# Remove all objects of the given classes from the index. There isn't much
|
439
|
+
# use for this in general operations but it can be useful for maintenance,
|
440
|
+
# testing, etc. If no arguments are passed, remove everything from the
|
441
|
+
# index.
|
442
|
+
#
|
443
|
+
# ==== Parameters
|
444
|
+
#
|
445
|
+
# classes...<Class>::
|
446
|
+
# classes for which to remove all instances from the index (may pass an
|
447
|
+
# array or varargs)
|
448
|
+
#
|
449
|
+
# ==== Example
|
450
|
+
#
|
451
|
+
# Sunspot.remove_all(Post, Blog)
|
452
|
+
#
|
453
|
+
def remove_all(*classes)
|
454
|
+
session.remove_all(*classes)
|
455
|
+
end
|
456
|
+
|
457
|
+
#
|
458
|
+
# Remove all objects of the given classes from the index and immediately
|
459
|
+
# commit. See Sunspot.remove_all
|
460
|
+
#
|
461
|
+
# ==== Parameters
|
462
|
+
#
|
463
|
+
# classes...<Class>::
|
464
|
+
# classes for which to remove all instances from the index
|
465
|
+
def remove_all!(*classes)
|
466
|
+
session.remove_all!(*classes)
|
467
|
+
end
|
468
|
+
|
469
|
+
#
|
470
|
+
# Process all adds in a batch. Any Sunspot adds initiated inside the block
|
471
|
+
# will be sent in bulk when the block finishes. Useful if your application
|
472
|
+
# initiates index adds from various places in code as part of a single
|
473
|
+
# operation; doing a batch add will give better performance.
|
474
|
+
#
|
475
|
+
# ==== Example
|
476
|
+
#
|
477
|
+
# Sunspot.batch do
|
478
|
+
# post = Post.new
|
479
|
+
# Sunspot.add(post)
|
480
|
+
# comment = Comment.new
|
481
|
+
# Sunspot.add(comment)
|
482
|
+
# end
|
483
|
+
#
|
484
|
+
# Sunspot will send both the post and the comment in a single request.
|
485
|
+
#
|
486
|
+
def batch(&block)
|
487
|
+
session.batch(&block)
|
488
|
+
end
|
489
|
+
|
490
|
+
#
|
491
|
+
# True if documents have been added, updated, or removed since the last
|
492
|
+
# commit.
|
493
|
+
#
|
494
|
+
# ==== Returns
|
495
|
+
#
|
496
|
+
# Boolean:: Whether there have been any updates since the last commit
|
497
|
+
#
|
498
|
+
def dirty?
|
499
|
+
session.dirty?
|
500
|
+
end
|
501
|
+
|
502
|
+
#
|
503
|
+
# Sends a commit if the session is dirty (see #dirty?).
|
504
|
+
#
|
505
|
+
def commit_if_dirty
|
506
|
+
session.commit_if_dirty
|
507
|
+
end
|
508
|
+
|
509
|
+
#
|
510
|
+
# True if documents have been removed since the last commit.
|
511
|
+
#
|
512
|
+
# ==== Returns
|
513
|
+
#
|
514
|
+
# Boolean:: Whether there have been any deletes since the last commit
|
515
|
+
#
|
516
|
+
def delete_dirty?
|
517
|
+
session.delete_dirty?
|
518
|
+
end
|
519
|
+
|
520
|
+
#
|
521
|
+
# Sends a commit if the session has deletes since the last commit (see #delete_dirty?).
|
522
|
+
#
|
523
|
+
def commit_if_delete_dirty
|
524
|
+
session.commit_if_delete_dirty
|
525
|
+
end
|
526
|
+
|
527
|
+
# Returns the configuration associated with the singleton session. See
|
528
|
+
# Sunspot::Configuration for details.
|
529
|
+
#
|
530
|
+
# ==== Returns
|
531
|
+
#
|
532
|
+
# LightConfig::Configuration:: configuration for singleton session
|
533
|
+
#
|
534
|
+
def config
|
535
|
+
session.config
|
536
|
+
end
|
537
|
+
|
538
|
+
#
|
539
|
+
# Resets the singleton session. This is useful for clearing out all
|
540
|
+
# static data between tests, but probably nowhere else.
|
541
|
+
#
|
542
|
+
# ==== Parameters
|
543
|
+
#
|
544
|
+
# keep_config<Boolean>::
|
545
|
+
# Whether to retain the configuration used by the current singleton
|
546
|
+
# session. Default false.
|
547
|
+
#
|
548
|
+
def reset!(keep_config = false)
|
549
|
+
config =
|
550
|
+
if keep_config
|
551
|
+
session.config
|
552
|
+
else
|
553
|
+
Configuration.build
|
554
|
+
end
|
555
|
+
@session = Session.new(config)
|
556
|
+
end
|
557
|
+
|
558
|
+
#
|
559
|
+
# Get the singleton session, creating it if none yet exists.
|
560
|
+
#
|
561
|
+
# ==== Returns
|
562
|
+
#
|
563
|
+
# Sunspot::Session:: the singleton session
|
564
|
+
#
|
565
|
+
def session #:nodoc:
|
566
|
+
@session ||= Session.new
|
567
|
+
end
|
568
|
+
end
|
569
|
+
end
|