Django Haystack: Responsibility for Template and Model Indexes

I looked through the docs, and even created some reverse search loops, but I'm still very confused about what these things do in the haystack. Is the search a complete search for the fields you put in your class that inherits indexes.SearchIndex, indexes.Indexable, or reverse text search inside your template? Can someone explain this to me?

In django haystack, you will create a class that determines which fields should be requested (well, as I understand it):

class ProductIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) name = indexes.CharField(model_attr='title', boost=1.75) description = indexes.CharField(model_attr='description') short_description = indexes.CharField(model_attr='short_description') def get_model(self): return Product def index_queryset(self, using=None): """Used when the entire index for model is updated.""" return self.get_model().objects.filter(active=True, published_at__lte=datetime.now()) 

You will also create a txt template that will do something - I'm not sure what. I know that the search backend will go through this template during the search algorithm.

 {{ object.name }} {{ object.description }} {{ object.short_description }} {% for related in object.related %} {{ related.name }} {{ related.description }} {% endfor %} {% for category in object.categories.all %} {% if category.active %} {{ category.name }} {% endif %} {% endfor %} 

As you can see, the template has several fields that I do not have in my index class, they will search in the search database. So why are there fields in the index? What are the roles of an index class and an index template? Can someone please explain this to me.

+7
django django-templates django-haystack
source share
1 answer

Here, the ProductIndex class is the main thing. Haystack will use this configuration to index your Product model according to the fields you have selected for indexing and how. You can read about it here .

The template you created will be used by this field text = indexes.CharField(document=True, use_template=True) . In this template, we include all the important data from the model or related models, why? because it is used to perform a search query on all the data if you do not want to search in only one field.

 # filtering on single field qs = SearchQuerySet().models(Product).filter(name=query) # filtering on multiple fields qs = SearchQuerySet().models(Product).filter(name=query).filter(description=query) # filtering on all data where ever there is a match qs = SearchQuerySet().models(Product).filter(text=query) 
+7
source share

All Articles