Technical Recipes

Monday 5, September 2011

As you know full-text searching is very interesting for some applications using relational databases such as MySQL or PostgreSQL. Despite you have different options for configuring your application and database, this recipe is focus on MySql and Sphinx. Our operating system will be Fedora Linux, but you can easily use your own. Obviously, we need a Django application ready on our machine.

First, we're going to install django-sphinx:

$ pip-python django-sphinx

Then, we'll add some lines to our settings.py file:

# Sphinx 0.9.9
SPHINX_API_VERSION = 0x116
# Default port for Sphinx server on Fedora
SPHINX_PORT = 9312
# Require for Django 1.3 compatibility
DATABASE_ENGINE = 'mysql'

We need to execute a command as root user for installing the Sphinx server:

$ sudo yum install sphinx

It's time to get the required configuration for our own application. In order to do that, we can execute a simple and effective command provided by django-sphinx application:

$ ./manage.py generate_sphinx_config myapp_name

Keep in mind that myapp_name is the name of your application which contains the model you're using for full-text searching. Actually, we need to modify our model adding a line like the following one:

search = SphinxSearch()

Next step will be to copy the generated configuration for Sphinx to our general configuration file. Open your /etc/sphinx/sphinx.conf and add the generated lines by the generate_sphinx_config command. Don't forget to change the lines regarding to your database.

Before continuing, you'll need to do some additional changes to the generated configuration file. Look for the property path and change it to /var/lib/sphinx/myapp, then create this new directory:

$ sudo mkdir -p /var/lib/sphinx/myapp

When your Sphinx configuration is saved into your sphinx.conf, you're ready to generate the required indexes:

$ sudo indexer --all

Finally, you should launch your Sphinx server:

$ sudo searchd

If you want to test your application with Sphinx, open a new Django shell and execute these commands:

$ qs = MyModel.search.query('text for searching')
$ qs.count()

The following lines are an example of a sphinx.conf* file:

indexer
{
mem_limit   = 32M
}

source recipes_recipe
{
   type  = mysql
   sql_host = localhost
   sql_user = myuser 
   sql_pass = mypass
   sql_db = mydb 
   sql_port = 
   sql_query_pre =
   sql_query_post =
   sql_query = \
    SELECT id, title, body, permalink, \
       created_at, updated_at, slug\
    FROM myapp_table
   sql_query_info  = SELECT * FROM `myapp_table` \
                             WHERE `id` = $id

   sql_attr_timestamp   = created_at
   sql_attr_timestamp   = updated_at
}

index myapp_table
{
   source = myapp_table
   path = /var/lib/sphinx/myapp_table
   docinfo = extern
   morphology = none
   stopwords  =
   min_word_len = 2
   charset_type = utf-8
   min_prefix_len  = 0
   min_infix_len = 0
}
searchd
{
 listen = 127.0.0.1:9312
 log = /var/log/sphinx/searchd.log
 query_log = /var/log/sphinx/query.log
 read_timeout = 5
 max_children = 30
 pid_file = /var/run/sphinx/searchd.pid
 max_matches = 1000
 seamless_rotate = 1
 preopen_indexes = 0
 unlink_old = 1
 }
tags: django

Monday 11, July 2011

Some times it's very useful to find out which query is executed by the QuerySet object used by Django. You can find it out following the example below:

>>> qs = User.objects.filter(id__contains=1)
>>> print qs.query()
tags: django

Monday 11, July 2011

Just execute the following lines inside your console:

>>> from django.db import connection
>>> connection.queries
tags: django

Wednesday 8, June 2011

South is a very good tool for handling migration with Python and Django.

For applying a new migration after changing some things in our models we need to execute the following commands:

 $ ./manage.py schemamigration myapp --auto
 $ ./manage.py migrate

Tags

My latest tweets

tweet Only one week for Fedora 17! #fedora
tweet At Least 100,000 March in Spain Over Austerity http://t.co/LIvRCkTO
tweet Hoy es el día: Protesta como un ciudadano o calla como un súbdito. Tú eliges #12m15m
tweet Somos campeones!! #atleti
tweet Dell is working on a new laptop designed for developers with Ubuntu http://t.co/CemtM2fw
tweet Good tutorial about Flask, how to get up and running a web application with Python http://t.co/RtcW4f9d
Buy me a coffee!! Coffee cup

Advertisment