You can download the django-sphinx application from its repository on GitHub by visiting the following URL and clicking on the Download button: http://github.com/dcramer/django-sphinx/.
It is also available through the easy_install
tool by issuing the following command:
$ sudo easy_install django-sphinx
Once installed, you can add the django-sphinx layer to any model and take advantage of a very simple interface to search and retrieve Django model objects from your database tables. The only requirement is to create the source and indexes exactly as we did earlier and specify which index django-sphinx
should search when you attach it to your models.
For example, to use django-sphinx on our Product
model from chapter 2, we would change the model definition to include the SphinxSearch
manager.
from djangosphinx.models import SphinxSearch class Product(models.Model): category = models.ForeignKey('CatalogCategory', related_name='products') name = models.CharField(max_length=300) slug = models.SlugField(max_length=150) description = models.TextField() photo = models.ImageField(upload_to='product_photo', blank=True) manufacturer = models.CharField(max_length=300, blank=True) price_in_dollars = models.DecimalField(max_digits=6, decimal_places=2) price_in_euros = models.DecimalField(max_digits=6, decimal_places=2) price_in_pounds = models.DecimalField(max_digits=6, decimal_places=2) search = SphinxSearch('products_product')
The index we want to use is specified as the first parameter when we attach the manager object to our model. Multiple indexes are supported with the index keyword argument:
SphinxSearch(index='products_product products_other')
Our Product
model now has a custom manager that we can use to query the Sphinx search engine and get back Django model objects for the corresponding results. This is done using the query method on the SphinxSearch
manager:
products = Product.search.query("cranberry juice")
We can filter the products QuerySet
just like we would anywhere else in Django:
expensive_matches = products.filter(price_in_dollars__gte=100)
The django-sphinx layer also injects the Sphinx result weights, allowing you to order the search results by their relevance weight:
products = Product.search.query('cranberry juice').order_by('@weight')
In the SphinxSearch
manager definition on our Product
model, django-sphinx will allow you to further define weights on your indexed fields. This way we can weigh the name field, for example, heavier than the slug or description fields.
search = SphinxSearch(index='products_product', weights={ 'name': 100, 'slug': 50, 'description: 80})
One last excellent feature of django-sphinx is the ability to generate the Sphinx configuration files automatically. The resulting sphinx.conf
will likely require hand tuning, but it can help get you up and running quickly. To use this feature, add the SphinxManager
to your models, as described above, and then use django-sphinx's generate_config_for_model
helper method:
generate_config_for_model(Product)