0

Currently I have a database of Courses, and it has 6 columns:

class Course(models.Model):
    title = models.CharField(max_length=50)
    number = models.CharField(max_length=12)
    Cat1 = models.BooleanField()
    Cat2 = models.BooleanField()
    Cat3 = models.BooleanField()
    department = models.ForeignKey(Department)

The unicode method for this course is made to output something like:

def __unicode__(self):
        return u'%s %s %s %s %s %s' % (self.department, self.number, self.title, 'CAT1' if self.Cat1 else '','Cat2' if self.Cat2 else '','Cat3' if self.Cat3 else '')

Which returns something like: ENG 1104 Academic Writing Cat1 Cat3

I'm trying to implement a search to find courses by text queries, so I tried Haystack (with Whoosh as the engine), but to make it easier to index I just added a new column to the model named text, in which I just added the Unicode text of each course. This method works pretty well, but obviously its not robust and because this is only a small project of me for learning purposes, I would like to know more efficient ways of accomplishing this search. Any Ideas?

2
  • U can try MYSQL full tet search mercurytide.co.uk/news/article/django-full-text-search Commented Jun 13, 2012 at 15:17
  • 1
    Also, FWIW. When you're passing in that many variables to a string interpolation, it's better to use named parameters, i.e. u'%(department)s' % {'department': self.department}. You can spread the dictionary over multiple lines, making it much more readable, and you can see at a glance what info is going where. Commented Jun 13, 2012 at 15:17

1 Answer 1

1

Haystack doesn't require any changes to your models. The recommended method is to use a template:

class CourseIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    ...

Then, you create a file named in the form of templates/search/indexes/[app]/[model]_[indexfield].txt. So, in this case it would be templates/search/indexes/yourapp/course_text.txt. Then, you use standard Django template tags and such to create a plain text representation of your model:

{{ object.title }}
{{ object.number }}
{{ object.department.name }}

Add as much info as you like. If you have any rich text fields that include HTML, remember to use striptags, so you don't end up with garbage in your index.

Sign up to request clarification or add additional context in comments.

3 Comments

This would work with the exception of the Cat1,2,3 fields, which are Boolean but when searching I want to search for the actual name. Can I write something like {{'CAT1' if object.Cat1 else ''}} in the template?
Sure, something like, at least. It works just like a standard Django template, so what you have won't work, just because it's not valid syntax for a Django template, but {% if object.Cat1 %}CAT1{% endif %} will work.
Also, the idea here is simply to add any keyword to the template you might want people to find it by. The spacing, line endings etc. don't matter. This isn't being viewed like a standard template, it's just getting fed into the index as the content for a full-text search.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.