David Cramer's Blog

Setting Up Django With Sphinx Full-text Search

It seems I suck at documentation (yes, It’s very true), so here’s a quick How-to on using the Sphinx ORM for Django we created:

  1. Download and install Sphinx (you’re on your own here): http://www.sphinxsearch.com/
  2. Download and install the Django Sphinx package: http://django-sphinx.googlecode.com/ (again, on your own)
  3. Setup a source for your table:
    source files_file_en : base
    {
        sql_query	= \
                         SELECT files_file.id, files_file.name, files_data.description, files_file.tags as tag \
                         FROM files_file JOIN files_data \
                         ON files_file.id = files_data.file_id \
                         AND files_data.lang = 'en' \
                         AND files_file.visible = 1 \
                         GROUP BY files_file.id
        sql_query_info	= SELECT * FROM files_file WHERE id=$id
    }
    
  4. Setup an index for your table:
    index files_file_en
    {
        source			= files_file_en
        path			= /var/data/files_file_en
        docinfo			= extern
        morphology			= none
        stopwords			=
        min_word_len		= 2
        charset_type		= sbcs
        min_prefix_len		= 0
        min_infix_len		= 0
    }
    
  5. Go into your models and add the import:
    1
    
    from cursesite.core.search.models import SphinxSearch
    
  6. Go to the model you want to attach the search to:
    1
    
    search	= SphinxSearch(index="files_file_en")
    
  7. Now query:
    1
    
    File.search.query('hello')