django-mysqlsearch
Adding search functionality to a django application is an easy task. Just install and configure django-haystack and you are done. But using a file based search engine has some major disadvantages: For most projects they are oversized, too complicated or simply out of sync between your development and live system.
If you are using a MySQL Backend for your django application, this project may be an alternative. django-mysqlsearch uses the built-in MySQL fulltext indexes to provide a reliable search engine to your django app.
How does it work?
MySQL provides several Storage Engines for your tables. Each storage engine has pro and cons for different types of use cases. The most popular is MyISAM: its the default table type on most distributions, but lacks support for referential integrity, transactions and many more. The other popular storage engine is InnoDB: It supports foreign keys, transaction, etc. But the fulltext index can only be used with the MyISAM table type. So they are two ways to get a fulltext index into your database:
- Convert all your tables to MyISAM. This is not recommended, since you lose an extra layer of data integrity security on database level.
- Use seperate MyISAM tables for your search index.
django-mysqlsearch goes the second way: It creates "shadow" MyISAM tables for all of your searchable models and creates database triggers on your models database tables to update the search tables if a model changes.
If you are on Gentoo, you are fine - simply emerge django-mysqlsearch from our overlay and start using the app. Other distros/os have to download the tarball release and add it to the python site-packages directory.
Adding a search to your django application
Add the mysqlsearch app to your INSTALLED_APPS:
INSTALLED_APPS = (
...
'mysqlsearch',
...
)
After that, alter your models to use the SearchManager:
class MyModel(models.Model):
....
objects = SearchManager(('field1', 'field2')) # Add the search fields
To automaticly create the search tables for new models, add the following line to the end of your models.py:
signals.post_syncdb.connect(create_search_tables, sender=sys.modules[globals()['__name__']])
Finally, create all search tables for your exisiting model data:
python manage.py createsearchtables myapp.MyModel
Have a look at the sample app in the repository for a more detailed example.
The source code of this project is available via anonymous svn:
svn co https://repos.j-schmitz.net/svn/pub/django-mysqlsearch/trunk/ django-mysqlsearch
Releases
| 0.1Beta | 18.12.2010 |