This example describes the implementation of PHP enabled Sphinx Full-text Search. Share to everyone for your reference. The specific analysis is as follows:
In the compilation of installation Sphinx a lot of Chinese garbled, the last throw error stuck, I went to the official directly download an RPM package, installation is very cool, specific errors do not want to study, busy development.
Install two packages, one is mmseg this is to generate the Chinese dictionary of the program, one is csft is the Chinese version of the Sphinx.
RPM-IVH after installation, very smooth ~ ~ In less than half a minute to install the finished.
Chinese dictionary library, I went directly to the csft official download, very good thought very thoughtful.
Unigram.txt Uni.lib
Unigram.txt dictionary text, you can add your own keywords inside.
Then use: Mmseg-u unigram.txt to generate the dictionary file: Unigram.txt.uni and then rename Uni.lib This is the Dictionary Sphinx know.
Where to put it? Put you in the sphinx.conf inside the dictionary path configuration, and so will say, and then basically almost, under the Sphinx several practical procedures:
Copy Code code as follows:
[Root@beihai365/]# csft-
Csft-indexer Csft-search Csft-searchd
Csft-indexer is the program that generates the Full-text search index
Csft-search is to test whether the search is effective or not, and it works well, as I have no use for client script development, you can use this to see if Full-text search is successful
Csft-searchd This is the Sphinx search daemon. After startup, you can use scripting Php python and so on, open the query.
It's as simple as looking at the two key parts of the thing.
sphinx.conf configuration file:
Copy Code code as follows:
Source Tmsgs
{
type = MySQL
sql_host = localhost
Sql_user = root
Sql_pass = 1
sql_db = phpwind75sp3
Sql_port = 3306 # optional, default is 3306
#sql_sock =/tmp/mysql3307.sock
sql_query_pre = SET NAMES GBK
Sql_query = SELECT Id,name,type,stock from Pw_tools
#sql_attr_uint = ID
sql_attr_uint = Stock
}
Index Tmsgsindex
{
Source = Tmsgs
Path =/var/mmseg/searchdata/beihai365
docinfo = extern
Charset_type = ZH_CN.GBK
#min_prefix_len = 0
#min_infix_len = 2
#ngram_len = 2
Charset_dictpath =/var/mmseg/data
#min_prefix_len = 0
#min_infix_len = 0
#min_word_len = 2
}
Indexer
{
Mem_limit = 128M
}
Searchd
{
#listen = 3312
log =/var/log/searchd.log
Query_log =/var/log/query.log
read_timeout = 5
Max_children = 30
pid_file =/var/log/searchd.pid
max_matches = 1000
#seamless_rotate = 1
#preopen_indexes = 0
#unlink_old = 1
}
Source Tmsgs
{
Type = MySQL
sql_host = localhost
Sql_user = root
Sql_pass = 1
sql_db = phpwind75sp3
sql_port = 3306 # optional, default is 3306
#sql_sock =/tmp/mysql3307.sock
sql_query_pre = SET NAMES GBK
sql_query = SELECT id,name,type,stock from Pw_tools
#sql_attr_uint = ID
sql_attr_uint = Stock
}
Index Tmsgsindex
{
Source = Tmsgs
Path =/var/mmseg/searchdata/beihai365
docinfo = extern
Charset_type = ZH_CN.GBK
#min_prefix_len = 0
#min_infix_len = 2
#ngram_len = 2
Charset_dictpath =/var/mmseg/data
#min_prefix_len = 0
#min_infix_len = 0
#min_word_len = 2
}
Indexer
{
mem_limit = 128M
}
Searchd
{
#listen = 3312
log =/var/log/searchd.log
Query_log =/var/log/query.log
read_timeout = 5
Max_children = 30
pid_file =/var/log/searchd.pid
max_matches = 1000
#seamless_rotate = 1
#preopen_indexes = 0
#unlink_old = 1
}
look again, test client code:
Copy Code code as follows:
<?php
header ("Content-type:text /html;charset=utf-8 ");
include ' sphinxapi.php ';
& nbsp; $cl = new Sphinxclient ();
$cl->setserver (' localhost ', 3312);
$cl->setmatchmode (sph_match_all);
$cl->setarrayresult (True);
$res = $cl->query ("name card", "*");
print_r ($res);
?>
<?php br> header ("Content-type:text/html;charset=utf-8");
include ' sphinxapi.php ';
$cl = new Sphinxclient ();
$cl->setserver (' localhost ', 3312);
$cl->setmatchmode (Sph_match_all);
$cl->setarrayresult (true);
$res = $cl->query ("name card", "*");
Print_r ($res);
?>
"name card" This keyword is I manually added to the dictionary, to see if it can be really found, the example code is as follows:
Copy Code code as follows:
Array
(
[ERROR] =>
[WARNING] =>
[status] => 0
[Fields] => Array
(
[0] => name
[1] => type
)
[Attrs] => Array
(
[stock] => 1
)
[matches] => Array
(
[0] => Array
(
[id] => 8
[Weight] => 1
[Attrs] => Array
(
[stock] => 100
)
)
)
[Total] => 1
[Total_found] => 1
[TIME] => 0.018
[words] => Array
(
[name card] => Array
(
[docs] => 1
[hits] => 1
)
)
)
Array
(
[ERROR] =>
[WARNING] =>
[status] => 0
[Fields] => Array
(
[0] => name
[1] => type
)
[Attrs] => Array
(
[stock] => 1
)
[matches] => Array
(
[0] => Array
(
[id] => 8
[weight] => 1
[Attrs] => Array
(
[stock] => 100
)
)
)
[Total] => 1
[Total_found] => 1
[TIME] => 0.018
[Words] => Array
(
[name card] => Array
(
[docs] => 1
[hits] => 1
)
)
)
completely no problem, search out, a few key operations:
[root@beihai365/]# csft-searchd--stop stop search Guardian
[root@beihai365/]# csft-indexer--all generates indexes for all nodes, you can also index a node for example: Csft-indexer xx
[root@beihai365/]# csft-search app search keyword app, but look at the following information is not found and did not hit any documents.
Coreseek Full Text Server 3.1
Copyright (c) 2006-2008 coreseek.com
Using config file './csft.conf ' ...
1,
Pt:1, 1; Index ' tmsgsindex ': Query ' App ': returned 0 matches of 0 Total in 0.017 sec
Words
1. ' App ': 0 documents, 0 hits
When you run these commands to find that you need your own manual top--config sphinx.conf configuration file path, very inconvenient, so I simply ln-s a in./, so do not have to typing--config every time.
I hope this article will help you with your PHP program design.