Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django-haystack : Better ways of creating search indexes for models having foreign key and many-to-many fields

Suggestions needed for creating better and efficient search indexes for models having foreign key and many-to-many fields while using haystack with django.

Sample Model:

class Resource(models.Model):
   title = models.CharField(max_length=255)
   description = models.TextField(blank=True, null=True)
   content = models.ForeignKey(ResourceContent, unique=True)
   metadata = models.ManyToManyField(MetaData)
like image 392
Neo Avatar asked Jul 12 '11 09:07

Neo


1 Answers

you don't need to declare

metadata = models.ManyToManyField(MetaData)

instead use looping inside index template easy where best practise says in doc

Related Data

Related data is somewhat problematic to deal with, as most search engines are better with documents than they are with relationships. One way to approach this is to de-normalize a related child object or objects into the parent’s document template. The inclusion of a foreign key’s relevant data or a simple Django {% for %} templatetag to iterate over the related objects can increase the salient data in your document. Be careful what you include and how you structure it, as this can have consequences on how well a result might rank in your search

http://docs.haystacksearch.org/dev/best_practices.html?highlight=loop

like image 151
soField Avatar answered Oct 20 '22 05:10

soField