SearchField and it’s subclasses provides a way to declare what data
you’re interested in indexing. They are used with
SearchIndexes, much like
forms.*Field are used within forms or
models.*Field within models.
They provide both the means for storing data in the index, as well as preparing
the data before it’s placed in the index. Haystack uses all fields from all
SearchIndex classes to determine what the engine’s index schema ought to
In practice, you’ll likely never actually use the base
SearchField, as the
subclasses are much better at handling real data.
Included with Haystack are the following field types:
And equivalent faceted versions:
There is no faceted variant of the n-gram fields. Because of how the engine
generates n-grams, faceting on these field types (
EdgeNgram) would make very little sense.
SearchField objects can be used on their own, they’re generally used
SearchIndex. You use them in a declarative manner, just like
django.db.models.Model objects. For
from haystack import indexes from myapp.models import Note class NoteIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) author = indexes.CharField(model_attr='user') pub_date = indexes.DateTimeField(model_attr='pub_date') def get_model(self): return Note
This will hook up those fields with the index and, when updating a
object, pull the relevant data out and prepare it for storage in the index.
Provides a means for specifying a fallback value in the event that no data is found for the field. Can be either a value or a callable.
A boolean flag that indicates which of the fields in the
to be the primary field for searching within. Default is
Only one field can be marked as the
document=True field, so you should
standardize this name and the format of the field between all of your
A boolean flag for indicating whether or not the the data from this field will
be searchable within the index. Default is
The companion of this option is
index_fieldname option allows you to force the name of the field in the
index. This does not change how Haystack refers to the field. This is useful
when using Solr’s dynamic attributes or when integrating with other external
Default is variable name of the field within the
model_attr option is a shortcut for preparing data. Rather than having
to manually fetch data out of a
model_attr allows you to specify
a string that will automatically pull data out for you. For example:
# Automatically looks within the model and populates the field with # the ``last_name`` attribute. author = CharField(model_attr='last_name')
It also handles callables:
# On a ``User`` object, pulls the full name as pieced together by the # ``get_full_name`` method. author = CharField(model_attr='get_full_name')
And can look through relations:
# Pulls the ``bio`` field from a ``UserProfile`` object that has a # ``OneToOneField`` relationship to a ``User`` object. biography = CharField(model_attr='user__profile__bio')
A boolean flag for indicating whether or not it’s permissible for the field
not to contain any data. Default is
Unlike Django’s database layer, which injects a
NULL into the database
when a field is marked nullable,
null=True will actually exclude that
field from being included with the document. This more efficient for the
search engine to deal with.
A boolean flag for indicating whether or not the data from this field will
be stored within the index. Default is
This is useful for pulling data out of the index along with the search result in order to save on hits to the database.
The companion of this option is
Allows you to override the name of the template to use when preparing data. By
default, the data templates for fields are located within your
under a path like
This option lets you override that path (though still within
bio = CharField(use_template=True, template_name='myapp/data/bio.txt')
You can also provide a list of templates, as
loader.select_template is used
under the hood.
bio = CharField(use_template=True, template_name=['myapp/data/bio.txt', 'myapp/bio.txt', 'bio.txt'])
A boolean flag for indicating whether or not a field should prepare its data via a data template or not. Default is False.
Data templates are extremely useful, as they let you easily tie together
different parts of the
Model (and potentially related models). This leads
to better search results with very little effort.
__init__(self, model_attr=None, use_template=False, template_name=None, document=False, indexed=True, stored=True, faceted=False, default=NOT_PROVIDED, null=False, index_fieldname=None, facet_class=None, boost=1.0, weight=None)¶
Instantiates a fresh
Returns a boolean of whether this field has a default value.
Takes data from the provided object and prepares it for storage in the index.
Flattens an object for indexing.
This loads a template
returns the result of rendering that template.
object will be in