Skip to content

Commit 041c480

Browse files
committed
BACKWARD-INCOMPATIBLE: Removed RealTimeSearchIndex in favor of SignalProcessors.
This only affects people who were using ``RealTimeSearchIndex`` (or a queuing variant) to perform near real-time updates. Those users should refer to the Migration documentation.
1 parent 6065c85 commit 041c480

25 files changed

+585
-310
lines changed

docs/best_practices.rst

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,10 @@ Those include files might look like::
170170
# search/includes/blog/post.html
171171
<div class="post_result">
172172
<h3><a href="{{ result.object.get_absolute_url }}">{{ result.object.title }}</a></h3>
173-
173+
174174
<p>{{ result.object.tease }}</p>
175175
</div>
176-
176+
177177
# search/includes/media/photo.html
178178
<div class="photo_result">
179179
<a href="{{ result.object.get_absolute_url }}">
@@ -197,21 +197,23 @@ might looks something like::
197197
Real-Time Search
198198
================
199199

200-
If your site sees heavy search traffic and up-to-date information is very important,
201-
Haystack provides a way to constantly keep your index up to date. By using the
202-
``RealTimeSearchIndex`` class instead of the ``SearchIndex`` class, Haystack will
203-
automatically update the index whenever a model is saved/deleted.
200+
If your site sees heavy search traffic and up-to-date information is very
201+
important, Haystack provides a way to constantly keep your index up to date.
202+
203+
You can enable the ``RealtimeSignalProcessor`` within your settings, which
204+
will allow Haystack to automatically update the index whenever a model is
205+
saved/deleted.
204206

205-
You can find more information within the :doc:`searchindex_api` documentation.
207+
You can find more information within the :doc:`signal_processors` documentation.
206208

207209

208210
Use Of A Queue For A Better User Experience
209211
===========================================
210212

211213
By default, you have to manually reindex content, Haystack immediately tries to merge
212214
it into the search index. If you have a write-heavy site, this could mean your
213-
search engine may spend most of its time churning on constant merges. If you can
214-
afford a small delay between when a model is saved and when it appears in the
215+
search engine may spend most of its time churning on constant merges. If you can
216+
afford a small delay between when a model is saved and when it appears in the
215217
search results, queuing these merges is a good idea.
216218

217219
You gain a snappier interface for users as updates go into a queue (a fast
@@ -222,24 +224,40 @@ could live on a completely separate server from your webservers, allowing you
222224
to tune more efficiently.
223225

224226
Implementing this is relatively simple. There are two parts, creating a new
225-
``QueuedSearchIndex`` class and creating a queue processing script to handle the
226-
actual updates.
227-
228-
For the ``QueuedSearchIndex``, simply inherit from the ``SearchIndex`` provided
229-
by Haystack and override the ``_setup_save``/``_setup_delete`` methods. These
230-
methods usually attach themselves to their model's ``post_save``/``post_delete``
231-
signals and call the backend to update or remove a record. You should override
232-
this behavior and place a message in your queue of choice. At a minimum, you'll
233-
want to include the model you're indexing and the id of the model within that
234-
message, so that you can retrieve the proper index from the ``SearchSite`` in
235-
your consumer. Then alter all of your ``SearchIndex`` classes to inherit from
236-
this new class. Now all saves/deletes will be handled by the queue and you
237-
should receive a speed boost.
238-
239-
For the consumer, this is much more specific to the queue used and your desired
240-
setup. At a minimum, you will need to periodically consume the queue, fetch the
241-
correct index from the ``SearchSite`` for your application, load the model from
242-
the message and pass that model to the ``update_object`` or ``remove_object``
243-
methods on the ``SearchIndex``. Proper grouping, batching and intelligent
244-
handling are all additional things that could be applied on top to further
227+
``QueuedSignalProcessor`` class and creating a queue processing script to
228+
handle the actual updates.
229+
230+
For the ``QueuedSignalProcessor``, you should inherit from
231+
``haystack.signals.BaseSignalProcessor``, then alter the ``setup/teardown``
232+
methods to call an enqueuing method instead of directly calling
233+
``handle_save/handle_delete``. For example::
234+
235+
from haystack import signals
236+
237+
238+
class QueuedSignalProcessor(signals.BaseSignalProcessor):
239+
# Override the built-in.
240+
def setup(self):
241+
models.signals.post_save.connect(self.enqueue_save)
242+
models.signals.post_delete.connect(self.enqueue_delete)
243+
244+
# Override the built-in.
245+
def teardown(self):
246+
models.signals.post_save.disconnect(self.enqueue_save)
247+
models.signals.post_delete.disconnect(self.enqueue_delete)
248+
249+
# Add on a queuing method.
250+
def enqueue_save(self, sender, instance, **kwargs):
251+
# Push the save & information onto queue du jour here...
252+
253+
# Add on a queuing method.
254+
def enqueue_delete(self, sender, instance, **kwargs):
255+
# Push the delete & information onto queue du jour here...
256+
257+
For the consumer, this is much more specific to the queue used and your desired
258+
setup. At a minimum, you will need to periodically consume the queue, fetch the
259+
correct index from the ``SearchSite`` for your application, load the model from
260+
the message and pass that model to the ``update_object`` or ``remove_object``
261+
methods on the ``SearchIndex``. Proper grouping, batching and intelligent
262+
handling are all additional things that could be applied on top to further
245263
improve performance.

docs/debugging.rst

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,25 +71,14 @@ not running a ``rebuild_index`` to populate your index or having a blank
7171

7272
This is a Whoosh-specific traceback. It occurs when the Whoosh engine in one
7373
process/thread is locks the index files for writing while another process/thread
74-
tries to access them. This is a common error when using ``RealTimeSearchIndex``
74+
tries to access them. This is a common error when using ``RealtimeSignalProcessor``
7575
with Whoosh under any kind of load, which is why it's only recommended for
7676
small sites or development.
7777

78-
A way to solve this is to subclass ``SearchIndex`` instead::
79-
80-
from haystack import indexes
81-
82-
# Change from:
83-
#
84-
# class MySearchIndex(indexes.RealTimeSearchIndex, indexes.Indexable):
85-
#
86-
# to:
87-
class MySearchIndex(indexes.SearchIndex, indexes.Indexable):
88-
...
89-
90-
The final step is to set up a cron job that runs
78+
The only real solution is to set up a cron job that runs
9179
``./manage.py rebuild_index`` (optionally with ``--age=24``) that runs nightly
92-
(or however often you need) to refresh the search indexes.
80+
(or however often you need) to refresh the search indexes. Then disable the
81+
use of the ``RealtimeSignalProcessor`` within your settings.
9382

9483
The downside to this is that you lose real-time search. For many people, this
9584
isn't an issue and this will allow you to scale Whoosh up to a much higher

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ you may want to include in your application.
6161
faceting
6262
autocomplete
6363
boost
64+
signal_processors
6465
multiple_index
6566
rich_content_extraction
6667
spatial

docs/management_commands.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ Examples::
113113
deletions unless the ``--remove`` flag is provided. You might consider
114114
a queue consumer if the memory requirements for ``--remove`` don't
115115
fit your needs. Alternatively, you can use the
116-
``RealTimeSearchIndex``, which will automatically handle deletions.
116+
``RealtimeSignalProcessor``, which will automatically handle deletions.
117117

118118

119119
``rebuild_index``

docs/migration_from_1_to_2.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ they consisted of:
1212
* The removal of ``SearchSite`` & ``haystack.site``.
1313
* The removal of ``handle_registrations`` & ``autodiscover``.
1414
* The addition of multiple index support.
15+
* The addition of ``SignalProcessors`` & the removal of ``RealTimeSearchIndex``.
1516
* The removal/renaming of various settings.
1617

1718
This guide will help you make the changes needed to be compatible with Haystack
@@ -200,6 +201,28 @@ This allows for reliable swapping of the index that handles a model without
200201
relying on correct import order.
201202

202203

204+
Removal of ``RealTimeSearchIndex``
205+
==================================
206+
207+
Use of the ``haystack.indexes.RealTimeSearchIndex`` is no longer valid. It has
208+
been removed in favor of ``RealtimeSignalProcessor``. To migrate, first change
209+
the inheritance of all your ``RealTimeSearchIndex`` subclasses to use
210+
``SearchIndex`` instead::
211+
212+
# Old.
213+
class MySearchIndex(indexes.RealTimeSearchIndex, indexes.Indexable):
214+
# ...
215+
216+
217+
# New.
218+
class MySearchIndex(indexes.SearchIndex, indexes.Indexable):
219+
# ...
220+
221+
Then update your settings to enable use of the ``RealtimeSignalProcessor``::
222+
223+
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'
224+
225+
203226
Done!
204227
=====
205228

docs/other_apps.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _ref-other_apps:
2+
13
=============================
24
Haystack-Related Applications
35
=============================
@@ -13,7 +15,7 @@ queued_search
1315

1416
http://github.com/toastdriven/queued_search (2.X compatible)
1517

16-
Provides a queue-based setup as an alternative to ``RealTimeSearchIndex`` or
18+
Provides a queue-based setup as an alternative to ``RealtimeSignalProcessor`` or
1719
constantly running the ``update_index`` command. Useful for high-load, short
1820
update time situations.
1921

0 commit comments

Comments
 (0)