I currently use Sphinx Search for alot of website on my web server, and have about 30-40 different indexes. As you can imagine this can make for a very long sphinx config file.
(40 lines per index) X (30 indexes) = 1200 lines - alot to have in one file.
I learnt this nifty trick from the sphinx search forum. You can use any command line scripting language (BASH,PHP,etc) and then include config files from another directory.
I use a global MySQL account (all databases, SELECT priveleges) this way if I change any database passwords, I can change it in one place.
#!/usr/bin/php
## data source definition
source root {
type = mysql
# some straightforward parameters for SQL source types
sql_host = 127.0.0.1
sql_user = MySQLUSER
sql_pass = MySQLPASS
sql_port = 3306 # optional, default is 3306
}
<?php
$files = scandir("/usr/local/etc/sphinx_confs/");
foreach($files as $key => $value){
if($value != "." && $value != ".." ) {
include("sphinx_confs/$value");
}
}
?>
## indexer settings
indexer
{
mem_limit = 256M
}
## searchd settings
searchd
{
port = 9816
log = /var/log/searchd.log
query_log = /var/log/search_query.log
read_timeout = 5
max_children = 30
pid_file = /var/log/searchd.pid
max_matches = 1000
seamless_rotate = 1
preopen_indexes = 1
unlink_old = 1
}
This will setup your root "source" item, and also your indexer and searchd settings
Inside my individual site files (in sphinx_confs/) is:
source site1_src : root
{
sql_db = idorset_co_uk_bigdaddy
sql_query = \
SELECT id, keywords, attr FROM table
sql_attr_uint = attr
}
index site1
{
source = site1_src
path = /var/sphinx_data/ifind_town
docinfo = extern
mlock = 0
morphology = none
min_word_len = 2
charset_type = sbcs
html_strip = 0
}
This will then be included into the main conf (on a search / index).
posted by Mark Willis


where do you assign a value to $value?!
Thanks Paolo. I’ve just updated the post – seems that the code got cut out when I posted it up.
< ?php
$files = scandir("/usr/local/etc/sphinx_confs/");
foreach($files as $key => $value){
if($value != "." && $value != ".." ) {
include("sphinx_confs/$value");
}
}
?>
after you scan the directory you want to use for configs $value holds the filename (and we ignore . and ..)
Thanks to you for the fast reply.
you do this in the sphinx.conf.dist directly?? this file suport php?? thx
adding:
#!/usr/bin/php
as the very top of the config will make it look for the php exec when the config is loaded. So you could use any type of parser (perl, ruby etc maybe?)
Mark
too much thx
)
Hi! I tried using the same method, but the php script is not working (i tried to just echo somthing, that didn’t work too).
and the .conf file cant be loaded. Can u help me please?
Thanks for the post. Do you have an can post a similar command/config file for a Windows machine? Thanks in advance