Search code examples
djangodjango-haystack

Django-Haystack: Understanding the use of data template in Haystack


I am new to Haystack. I cannot understand why we have to use a template to render it with the text that we want to search. More simple , why we don't have to use something like this?

text = indexes.CharField(document=True, "and here the attributes to search")

UPDATE To be more specific Let's say that we have an app places an here a model countries. In the model i want to be searchable from haystack the fields capital and biggest_cities. So in search_indexes.py i put

text = indexes.CharField(document=True, use_template=True )

After make a template in the path search/indexes/places/countries_text.txt Here i put

{{ object.capital }}
{{ object.biggest_cites }}

Again the question is: why we have to use a template in order to accomplish our goal?

It wouldn't be easier to use something like

text = indexes.CharField(document=Truer, model_attr='capital',model_attr='biggest_cites')

Solution

  • Have you read this Haystack Documentation page http://django-haystack.readthedocs.org/en/latest/searchindex_api.html ?

    If you haven't, you must. If you have, read it again.

    The SearchIndex API contains valuable fundamentals of how Haystack works on your project. It can also grant you a useful insight of "why you use templates to make your data searchable'.

    why we have to use a template in order to accomplish our goal?

    From the Haystack Docs:

    "...we’re providing use_template=True on the text field. This allows us to use a data template (rather than error prone concatenation) to build the document the search engine will use in searching"

    As you can see, we can choose whether use a template or not.

    Ps: sorry for the late post; I hope it helps you.