I’ve split the setting up of Sphinx Search in two parts. Please read Setting up Sphinx Search (Part 1) first.
Lets look at a “real world” example setup:
#The settings and SQL source for your index
source blogposts
{
type = mysql
sql_host = localhost
sql_user = dbuser
sql_pass = secret
sql_db = dbname
sql_port = 3306 #optional, default is 3306
sql_query_pre = SET NAMES utf8
sql_query = \
SELECT id, blog_id, title, body, \
UNIX_TIMESTAMP(publish_date) AS publish_date\
FROM blogposts \
sql_attr_uint = blog_id
sql_attr_timestamp = publish_date
}
The first section is the source of your index. Basically, it is the SQL query that you’d like to get indexed to search against. Note the sql_attr settings after the query. What that does is it allows you to filter or order your search results. For example: Lets say you want to order your blog post results by date, then you can in the API tell it to order by the field publish_date or lets suppose you only want to search blog posts from a blog with the blog_id = 2 you can tell the API in your code to filter results only for blog_id 2.
The next part is setting up the index for your source.
#The index for the source
index blogpostsindex
{
source = blogposts
path = /var/sphinx/data/blogposts
}
The source setting reference your source you’ve specified earlier. I.e., which SQL query it should index. When you use the API to query your index, you would have to specify blogpostindex as the index you’re querying. The path is where your index files would be stored on your web server. NOTE: The directory specified in the path setting need to be writable!
Next, we specify settings for your indexer command.
indexer
{
mem_limit = 256M
}
The mem_limit setting is optional, but it would be good to set it so that your indexer don’t eat too much of your server resources when it build your index.
Finally, you need to set up your search daemon.
searchd
{
log = /var/log/searchd.log
query_log = /var/log/query.log
pid_file = /var/log/searchd.pid
}
By the very least you need to specify the path to your log files and your process id or pid.
With all this set up, you’re good to go. First you would build your index:
indexer --all
The –all would build all your indexes if you have more than one set up. Once the index is built successfully, you can fire up the daemon:
searchd
You would probably want to setup a cron job in order to periodically update your index. All you need to do is to add a crontab entry running the following command:
/usr/local/bin/indexer --all --rotate
The –rotate will build a new index and once it is completed it would replace the old with the new without interrupting your search daemon. NOTE: If your indexes are big you can create delta indexes and merge it with the current ones. Delta indexes are indexes with only the latest data. I’ll explain at a later stage.
Thats it! You’re good to go. Next we’ll look at the API and how to run queries.