18đź‘Ť
You’re right, the tutorial seems a little vague, but here’s how I understand it. For each instance of the Note
model, Haystack renders the data template using that instance and indexes the rendered templates. The rendered template is the “document” for the instance. The tutorial says, “This allows us to use a data template (rather than error prone concatenation) to build the document the search engine will use in searching.” So if you only wanted the title
field to be searchable, you would only include {{ object.title }}
in the data template.
So the other fields in the NoteIndex
model are used for filtering search query results. If your index model looked just like this:
class NoteIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
you would not be able to issue a search query that says, “Give me all the Notes published in the last year where foo
appears in the document text.” If you include pub_date
as a field in your NoteIndex
(as they do in the tutorial) then you can make a query such as the following:
recent_results = SearchQuerySet().filter(content='foo').order_by('-pub_date')[:5]
which asks for the 5 most recently published documents that contain the word foo
. I suppose that, without including pub_date
in the NoteIndex
model, you could query for content='foo'
and then filter the results yourself, but I’d imagine it’s a much more efficient query if you tell Haystack at indexing time about the fields you might want to filter on.
As for how the search results will be displayed, you use a different template to specify that. In the most basic Haystack usage, which they show in the tutorial, the template for displaying search results goes in search/search.html: http://django-haystack.readthedocs.org/en/latest/tutorial.html#search-template
You can iterate through the search results and print out whatever fields of the model instance (result.object
) that you’d like.
2đź‘Ť
In your class definition,
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')
Haystack stores the index of user
attribute of database as author
, and the index of the database field pub_date
as pub_date
in index
The template includes only the “searchable” fields. For example, you might want to save some sensitive data in the search indices you can hide it from the search by not specifying it in the template.
text
can be thought of as free text search
- 'str' object is not callable Django Rest Framework
- Docker + Celery tells me not to run as root, but once I don't, I lack permissions to run
- How to set form field value – django
- How can I test whether Django is running in debug mode?
- When trying set corsheaders in settings.py file