Sphinx is an efficient search engine, Word segmentation search speed is relatively fast, index building stored in hard disk files, will not interfere with the database, has its own built-in set of databases.
I. Installing and configuring the 1.ubuntu installation Sphinx
If you do not have aptitude installed, you will need to install aptitude because there is a problem installing the following command with apt get install.
sudo apt-get install aptitude
sudo aptitude install sphinx3 sphinx3-doc sphinxsearch sphinx-common -y
2. Configurationcd/etc/sphinxsearch/CP sphinx.conf.sample sphinx.conf
Modify the configuration file as follows
#Configure source Sphinx_t0#Database name _ data table name, each configuration of a data table, you need to write a configuration source{type =MySQL#Database TypeSql_host =localhost Sql_user =root sql_pass = 123123sql_db = Sphinx#Specify Database Sql_port = 3306#Optional, default is 3306 Sql_sock =/tmp/Mysql.sock#MySQL Interface#SQL statement settings for reading data from a database#where possible not to use where or GroupBy,#GroupBy the where and the content to Sphinx, the Sphinx is more efficient for conditional filtering and GroupBy .#Note: The field for select must include a unique primary key and fields to be retrieved in full-text (can have more than one), and output. #Select the field you want to use in the where#Example:#in the configuration SQL statement, you can write an expected SQL statement to execute, and then set the Sphinx according to the#select * from t0 where description like '% Guangzhou ' or name like '%s% '#= = Select Id,description,name,age from t0sql_query =SELECT ID, name, age, description,group_id,date_added from t0 sql_attr_uint = Age#use Sql_attr to set the field (search condition), only as a property, using Sphinxclient::setfilter () to filter;#fields that are not set, automatically as fields for full-text retrieval, full-text search using Sphinxclient::query ("search string")#sql_query the first column ID needs to be an integer and is used by the system without setting the Sql_attr_uintSql_attr_uint =group_id sql_attr_timestamp = date_added#define different types of fields to use different property names, such as the sql_attr_timestamp above is the timestamp type#Sql_query_info = SELECT * from documents WHERE id= $id #命令行查询时 to read raw data information from the database#The SQL command executed before execution of Sql_query can have multiple sql_query_pre = SET NAMES UTF8#Execute SQL character encoding}#index, one index per source is required index sphinx_t0#The index name is generally consistent with the configuration source{Source = Sphinx_t0#Source association origin path =/usr/local/coreseek/var/data/sphinx_t0#index file storage path, one docinfo per index file =extern Charset_dictpath =/usr/local/mmseg/etc/#specifies that the word breaker reads the location of the dictionary file, which is required when word breaker is enabled. When using libmmseg as the word breaker, you need to make sure that the dictionary file uni.lib in the specified directory Charset_type = zh_cn.utf-8#character encoding}#index, controlling all indexesindexer{mem_limit = 512M#Memory}#Sphinx Daemon Configurationsearchd{port = 9312#Portlog =/usr/local/coreseek/var/Log/searchd.LogQuery_log =/usr/local/coreseek/var/log/query.Logread_timeout = 5#Timeout Max_children =#Maximum number of connections Pid_file =/usr/local/csft/ var/log/searchd.pid #pid file path max_matches = # max_matches maximum number of matches, that is, to find more data is only returned here set 1000 seamless_rotate = 1 preopen_indexes = 0 unlink_old = 1}
3. Execute command participle, generate a heap of index files in the/var/lib/sphinxsearch/data/test1 directory
sudo indexer -c /etc/sphinxsearch/sphinx.conf test1
Test1 is the index name for the above configuration file
4. Command line test Search
sudo search -c /etc/sphinxsearch/sphinx.conf google
Two. Use in PHP, install PHP, Sphinx Dependent Library 1. Install aptitude
apt-get install aptitude
sudo aptitude install libsphinxclient-dev libsphinxclient-0.0.1 -y
2. Installing the PHP Sphinx extension
Installing PECL
sudo apt-get install php-pear php5-dev
When installing Sphinx
sudo pecl install sphinx
3. Add the Sphinx extension to the profile php.ini,
My php.ini file is
sudo Vim/etc/php5/fpm/php.ini
to get its own php.ini file location using
php5-fpm-i|grep INI
Add to:
extension=sphinx.so
4. Restart PHP5-FPM to see if PHP is loading the Sphinx module
sudo /etc/init.d/php5-fpm restart
5. Run the search program in the background
sudo searchd -c /etc/sphinxsearch/sphinx.conf
Port in Default listener configuration file: 9312
PHP Code:
<?PHP Header(' Content-type:text/html;charset=utf-8 ');//encoded as Utf-8 $list=Array(); if(!Empty($_post)){ $SC=NewSphinxclient ();//instantiating the API $SC->setserver (' 192.168.169.128 ', 9312);//set service side, first parameter Sphinx server address, second Sphinx listening Port $res=$SC->query ($_post[' Key '], ' test1 ');//Execute query, first parameter query keyword, index name of second query, MySQL index name(This is also defined in the configuration file), multiple index names are separated, or can be used *represents all indexes. //Print_r ($SC); Echo' <pre> '; Print_r($res); Echo' </pre> '; Exit; } ?>
<form action= "" method= "post" > <input type= "text" name= "key"/> <input type= "Submit" value= "Submit"/> &L T;/form>
Using Sphinx search engine in PHP