Skip to content

Commit 070d46d

Browse files
committed
BACKWARD-INCOMPATIBLE: All "concrete" SearchIndex classes must now mixin indexes.Indexable as well in order to be included in the index.
1 parent 8892a5f commit 070d46d

28 files changed

+126
-98
lines changed

docs/autocomplete.rst

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,22 @@ Example (continuing from the tutorial)::
2929

3030
import datetime
3131
from haystack import indexes
32-
from haystack import site
3332
from myapp.models import Note
3433
3534
36-
class NoteIndex(indexes.SearchIndex):
35+
class NoteIndex(indexes.SearchIndex, indexes.Indexable):
3736
text = indexes.CharField(document=True, use_template=True)
3837
author = indexes.CharField(model_attr='user')
3938
pub_date = indexes.DateTimeField(model_attr='pub_date')
4039
# We add this for autocomplete.
4140
content_auto = indexes.EdgeNgramField(model_attr='content')
4241
42+
def get_model(self):
43+
return Note
44+
4345
def index_queryset(self):
4446
"""Used when the entire index for model is updated."""
4547
return Note.objects.filter(pub_date__lte=datetime.datetime.now())
46-
47-
48-
site.register(Note, NoteIndex)
4948

5049
As with all schema changes, you'll need to rebuild/update your index after
5150
making this change.

docs/boost.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Document boosting is done by adding a ``boost`` field to the prepared data
6565
from notes.models import Note
6666
6767
68-
class NoteSearchIndex(indexes.SearchIndex):
68+
class NoteSearchIndex(indexes.SearchIndex, indexes.Indexable):
6969
# Your regular fields here then...
7070
7171
def prepare(self, obj):
@@ -88,6 +88,9 @@ An example of this might be increasing the significance of a ``title``::
8888
from notes.models import Note
8989
9090
91-
class NoteSearchIndex(indexes.SearchIndex):
91+
class NoteSearchIndex(indexes.SearchIndex, indexes.Indexable):
9292
text = indexes.CharField(document=True, use_template=True)
9393
title = indexes.CharField(model_attr='title', boost=1.125)
94+
95+
def get_model(self):
96+
return Note

docs/debugging.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ A way to solve this is to subclass ``SearchIndex`` instead::
8181
8282
# Change from:
8383
#
84-
# class MySearchIndex(indexes.RealTimeSearchIndex):
84+
# class MySearchIndex(indexes.RealTimeSearchIndex, indexes.Indexable):
8585
#
8686
# to:
87-
class MySearchIndex(indexes.SearchIndex):
87+
class MySearchIndex(indexes.SearchIndex, indexes.Indexable):
8888
...
8989

9090
The final step is to set up a cron job that runs

docs/faceting.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ searchable in the standard ways.
5050
To inform Haystack of this, you simply pass along a ``faceted=True`` parameter
5151
on the field(s) you wish to facet on. So to modify our existing example::
5252

53-
class NoteIndex(SearchIndex):
53+
class NoteIndex(SearchIndex, indexes.Indexable):
5454
text = CharField(document=True, use_template=True)
5555
author = CharField(model_attr='user', faceted=True)
5656
pub_date = DateTimeField(model_attr='pub_date')

docs/installing_search_engines.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ that mirrors the ``text`` field, but has ``indexed=False`` on it. This disables
5656
the post-processing that Solr does, which can mess up your suggestions.
5757
Something like the following is suggested::
5858

59-
class MySearchIndex(indexes.SearchIndex):
59+
class MySearchIndex(indexes.SearchIndex, indexes.Indexable):
6060
text = indexes.CharField(document=True, use_template=True)
6161
# ... normal fields then...
6262
suggestions = indexes.CharField()

docs/migration_from_1_to_2.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ A converted Haystack 2.X index should look like::
146146
from myapp.models import Note
147147
148148
149-
class NoteIndex(indexes.SearchIndex):
149+
class NoteIndex(indexes.SearchIndex, indexes.Indexable):
150150
text = indexes.CharField(document=True, use_template=True)
151151
author = indexes.CharField(model_attr='user')
152152
pub_date = indexes.DateTimeField(model_attr='pub_date')
@@ -162,6 +162,13 @@ Note the import on ``site`` & the registration statements are gone. Newly added
162162
are is the ``NoteIndex.get_model`` method. This is a **required** method &
163163
should simply return the ``Model`` class the index is for.
164164

165+
There's also a new, additional class added to the ``class`` definition. The
166+
``indexes.Indexable`` class is a simple mixin that serves to identify the
167+
classes Haystack should automatically discover & use. If you have a custom
168+
base class (say ``QueuedSearchIndex``) that other indexes inherit from, simply
169+
leave the ``indexes.Indexable`` off that declaration & Haystack won't try to
170+
use it.
171+
165172
Additionally, the name of the ``document=True`` field is now enforced to be
166173
``text`` across all indexes. If you need it named something else, you should
167174
set the ``HAYSTACK_DOCUMENT_FIELD`` setting. For example::

docs/searchfield_api.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,16 @@ fields in ``django.forms.Form`` or ``django.db.models.Model`` objects. For
6262
example::
6363

6464
from haystack import indexes
65+
from myapp.models import Note
6566
6667
67-
class NoteIndex(indexes.SearchIndex):
68+
class NoteIndex(indexes.SearchIndex, indexes.Indexable):
6869
text = indexes.CharField(document=True, use_template=True)
6970
author = indexes.CharField(model_attr='user')
7071
pub_date = indexes.DateTimeField(model_attr='pub_date')
72+
73+
def get_model(self):
74+
return Note
7175

7276
This will hook up those fields with the index and, when updating a ``Model``
7377
object, pull the relevant data out and prepare it for storage in the index.

docs/searchindex_api.rst

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ For the impatient::
2626
from myapp.models import Note
2727
2828
29-
class NoteIndex(indexes.SearchIndex):
29+
class NoteIndex(indexes.SearchIndex, indexes.Indexable):
3030
text = indexes.CharField(document=True, use_template=True)
3131
author = indexes.CharField(model_attr='user')
3232
pub_date = indexes.DateTimeField(model_attr='pub_date')
@@ -124,13 +124,13 @@ contents of that field, which avoids the database hit.:
124124

125125
Within ``myapp/search_indexes.py``::
126126

127-
class NoteIndex(SearchIndex):
127+
class NoteIndex(SearchIndex, indexes.Indexable):
128128
text = CharField(document=True, use_template=True)
129129
author = CharField(model_attr='user')
130130
pub_date = DateTimeField(model_attr='pub_date')
131131
# Define the additional field.
132132
rendered = CharField(use_template=True, indexed=False)
133-
133+
134134
Then, inside a template named ``search/indexes/myapp/note_rendered.txt``::
135135

136136
<h2>{{ object.title }}</h2>
@@ -224,11 +224,14 @@ To keep with our existing example, one use case might be altering the name
224224
inside the ``author`` field to be "firstname lastname <email>". In this case,
225225
you might write the following code::
226226

227-
class NoteIndex(SearchIndex):
227+
class NoteIndex(SearchIndex, indexes.Indexable):
228228
text = CharField(document=True, use_template=True)
229229
author = CharField(model_attr='user')
230230
pub_date = DateTimeField(model_attr='pub_date')
231231
232+
def get_model(self):
233+
return Note
234+
232235
def prepare_author(self, obj):
233236
return "%s <%s>" % (obj.user.get_full_name(), obj.user.email)
234237

@@ -239,11 +242,14 @@ data may come from the field itself.
239242
Just like ``Form.clean_FOO``, the field's ``prepare`` runs before the
240243
``prepare_FOO``, allowing you to access ``self.prepared_data``. For example::
241244

242-
class NoteIndex(SearchIndex):
245+
class NoteIndex(SearchIndex, indexes.Indexable):
243246
text = CharField(document=True, use_template=True)
244247
author = CharField(model_attr='user')
245248
pub_date = DateTimeField(model_attr='pub_date')
246249
250+
def get_model(self):
251+
return Note
252+
247253
def prepare_author(self, obj):
248254
# Say we want last name first, the hard way.
249255
author = u''
@@ -257,12 +263,15 @@ Just like ``Form.clean_FOO``, the field's ``prepare`` runs before the
257263
This method is fully function with ``model_attr``, so if there's no convenient
258264
way to access the data you want, this is an excellent way to prepare it::
259265

260-
class NoteIndex(SearchIndex):
266+
class NoteIndex(SearchIndex, indexes.Indexable):
261267
text = CharField(document=True, use_template=True)
262268
author = CharField(model_attr='user')
263269
categories = MultiValueField()
264270
pub_date = DateTimeField(model_attr='pub_date')
265271
272+
def get_model(self):
273+
return Note
274+
266275
def prepare_categories(self, obj):
267276
# Since we're using a M2M relationship with a complex lookup,
268277
# we can prepare the list here.
@@ -280,11 +289,14 @@ Overriding this method is useful if you need to collect more than one piece
280289
of data or need to incorporate additional data that is not well represented
281290
by a single ``SearchField``. An example might look like::
282291

283-
class NoteIndex(SearchIndex):
292+
class NoteIndex(SearchIndex, indexes.Indexable):
284293
text = CharField(document=True, use_template=True)
285294
author = CharField(model_attr='user')
286295
pub_date = DateTimeField(model_attr='pub_date')
287296
297+
def get_model(self):
298+
return Note
299+
288300
def prepare(self, object):
289301
self.prepared_data = super(NoteIndex, self).prepare(object)
290302
@@ -511,11 +523,14 @@ By default, returns ``all()`` on the model's default manager.
511523

512524
Example::
513525

514-
class NoteIndex(SearchIndex):
526+
class NoteIndex(SearchIndex, indexes.Indexable):
515527
text = CharField(document=True, use_template=True)
516528
author = CharField(model_attr='user')
517529
pub_date = DateTimeField(model_attr='pub_date')
518530
531+
def get_model(self):
532+
return Note
533+
519534
def load_all_queryset(self):
520535
# Pull all objects related to the Note in search results.
521536
return Note.objects.all().select_related()
@@ -585,18 +600,20 @@ For the impatient::
585600
from myapp.models import Note
586601
587602
# All Fields
588-
class AllNoteIndex(indexes.ModelSearchIndex):
603+
class AllNoteIndex(indexes.ModelSearchIndex, indexes.Indexable):
589604
class Meta:
590-
pass
605+
model = Note
591606
592607
# Blacklisted Fields
593-
class LimitedNoteIndex(indexes.ModelSearchIndex):
608+
class LimitedNoteIndex(indexes.ModelSearchIndex, indexes.Indexable):
594609
class Meta:
610+
model = Note
595611
excludes = ['user']
596612
597613
# Whitelisted Fields
598-
class NoteIndex(indexes.ModelSearchIndex):
614+
class NoteIndex(indexes.ModelSearchIndex, indexes.Indexable):
599615
class Meta:
616+
model = Note
600617
fields = ['user', 'pub_date']
601618
602619
# Note that regular ``SearchIndex`` methods apply.

docs/tutorial.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ it applies to, though that is not required. This allows
181181
from myapp.models import Note
182182
183183
184-
class NoteIndex(indexes.SearchIndex):
184+
class NoteIndex(indexes.SearchIndex, indexes.Indexable):
185185
text = indexes.CharField(document=True, use_template=True)
186186
author = indexes.CharField(model_attr='user')
187187
pub_date = indexes.DateTimeField(model_attr='pub_date')

example_project/bare_bones_app/search_indexes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
# `haystack.indexes.BasicSearchIndex`, whose only requirement will be that
77
# you create a `search/indexes/bare_bones_app/cat_text.txt` data template
88
# for indexing.
9-
class CatIndex(indexes.BasicSearchIndex):
9+
class CatIndex(indexes.BasicSearchIndex, indexes.Indexable):
1010
def get_model(self):
1111
return Cat

0 commit comments

Comments
 (0)