- Make sure to check CentOS, PHP-FPM, Nginx, memcached and MYSQL posts.
- Get fresh RPM from sphinxsearch.com
- Install Sphinx
rpm -Uhv http://sphinxsearch.com/files/sphinx-2.0.4-1.rhel6.x86_64.rpm |
- Create sample MySQL database
|
- Config sphinx
mkdir -p /server/sphinx/data chown -R sphinx /server/sphinx mv /etc/sphinx/sphinx.conf /etc/sphinx/sphinx.conf.old vi /etc/sphinx/sphinx.conf |
# # Sphinx configuration file sample # # WARNING! While this sample file mentions all available options, # it contains (very) short helper descriptions only. Please refer to # doc/sphinx.html for details. # OR http://www.molecularsciences.org/book/export/html/405 ############################################################################# ## data source definition for sites ############################################################################# source sites { # data source type. mandatory, no default value # known types are mysql, pgsql, mssql, xmlpipe, xmlpipe2, odbc type = mysql ##################################################################### ## SQL settings (for 'mysql' and 'pgsql' types) ##################################################################### # some straightforward parameters for SQL source types sql_host = localhost sql_user = root sql_pass = rootpass sql_db = Sample_DB #sql_port = 3306 # optional, default is 3306 sql_query_pre = SET SESSION query_cache_type=OFF #update counter sql_query_pre = REPLACE INTO sph_counter SELECT 1, MAX(id) FROM sites # main document fetch query # mandatory, integer document ID field MUST be the first selected column sql_query = SELECT id,url ,Title,Description FROM sites \ WHERE id<=( SELECT max_doc_id FROM sph_counter WHERE counter_id=1 ) sql_ranged_throttle = 0 } ############################################################################# ## sitesDelta source definition ############################################################################# source sitesDelta : sites { sql_query_pre = SET SESSION query_cache_type=OFF sql_query = SELECT id,url ,Title,Description FROM sites \ WHERE id > ( SELECT max_doc_id FROM sph_counter WHERE counter_id=1 ) } ############################################################################# ## sites index definition ############################################################################# # local index example # # this is an index which is stored locally in the filesystem # # all indexing-time options (such as morphology and charsets) # are configured per local index index sites { # document source(s) to index # multi-value, mandatory # document IDs must be globally unique across all sources source = sites # index files path and file name, without extension # mandatory, path must be writable, extensions will be auto-appended path = /server/sphinx/data/sites # minimum indexed word length # default is 1 (index everything) min_word_len = 3 # minimum word infix length to index # optional, default is 0 (do not index infixes) # min_infix_len = 3 # enable star-syntax (wildcards) when searching prefix/infix indexes # known values are 0 and 1 # optional, default is 0 (do not use wildcard syntax) # enable_star = 1 #enable chinese search ngram_len = 1 } ############################################################################# ## sitesDelta index definition ############################################################################# index sitesDelta : sites { # document source(s) to index # multi-value, mandatory # document IDs must be globally unique across all sources source = sitesDelta # index files path and file name, without extension # mandatory, path must be writable, extensions will be auto-appended path = /server/sphinx/data/sitesDelta # minimum indexed word length # default is 1 (index everything) min_word_len = 3 # minimum word infix length to index # optional, default is 0 (do not index infixes) # min_infix_len = 3 # enable star-syntax (wildcards) when searching prefix/infix indexes # known values are 0 and 1 # optional, default is 0 (do not use wildcard syntax) # enable_star = 1 #enable chinese search ngram_len = 1 } ####################################################################################### ############################################################################# ## indexer settings ############################################################################# indexer { # memory limit, in bytes, kiloytes (16384K) or megabytes (256M) # optional, default is 32M, max is 2047M, recommended is 256M to 1024M mem_limit = 32M } ############################################################################# ## searchd settings ############################################################################# searchd { # hostname, port, or hostname:port, or /unix/socket/path to listen on # multi-value, multiple listen points are allowed # optional, default is 0.0.0.0:9312 (listen on all interfaces, port 9312) # # listen = 127.0.0.1 # listen = 192.168.0.1:9312 # listen = 9312 compat_sphinxql_magics = 0 # log file, searchd run info is logged here # optional, default is 'searchd.log' log = /var/log/searchd.log # query log file, all search queries are logged here # optional, default is empty (do not log queries) query_log = /var/log/query.log # client read timeout, seconds # optional, default is 5 read_timeout = 5 # request timeout, seconds # optional, default is 5 minutes client_timeout = 300 # maximum amount of children to fork (concurrent searches to run) # optional, default is 0 (unlimited) max_children = 30 # PID file, searchd process ID file name # mandatory pid_file = /var/log/searchd.pid # max amount of matches the daemon ever keeps in RAM, per-index # WARNING, THERE'S ALSO PER-QUERY LIMIT, SEE SetLimits() API CALL # default is 1000 (just like Google) max_matches = 1000 # seamless rotate, prevents rotate stalls if precaching huge datasets # optional, default is 1 seamless_rotate = 1 # whether to forcibly preopen all indexes on startup # optional, default is 0 (do not preopen) preopen_indexes = 0 # whether to unlink .old index copies on succesful rotation. # optional, default is 1 (do unlink) unlink_old = 1 # crash log path # searchd will (try to) log crashed query to 'crash_log_path.PID' file # optional, default is empty (do not create crash logs) # #crash_log_path = /server/log/sphinx/crash } # --eof-- |
- Create indexes
/usr/bin/indexer --all chown -R sphinx /server/sphinx |
- Start Sphinx
/etc/init.d/searchd start |
- Run a test search for word ‘mysql’
|
- Search using PHP
- Get sphinxapi.php : Sphinx comes with a PHP implementation of searchd client API. You can find it at api/sphinxapi.php in the root of the sphinx source tarball (tar.gz).
|
- Run sphinx.php http://www.example.com/sphinx.php?q=*mysql
- Create script to rotate delta indexes
vi /server/sphinx-delta.sh |
#!/bin/sh if [ -z "$(/sbin/pidof indexer)" ] then /usr/bin/indexer sitesDelta --rotate; fi |
- add cron job
chmod +x /server/sphinx-delta.sh crontab -e |
##########################sphinx #rotate delta index every 30 min */30 * * * * /server/sphinx-deltas.sh > /dev/null 2>&1 |
- To merge indexes run
/usr/bin/indexer --merge sites sitesDelta --rotate; |