This article describes the implementation method of enabling Sphinx full-text search in PHP. Share it for your reference. The specific analysis is as follows:
When compiling and installing sphinx, a lot of Chinese garbled code appeared, and finally the error was thrown and stuck. I went to the official website to download a rpm package. The installation was very good. I don’t want to study the specific errors anymore, so I’m busy with development.
Install two packages, one is mmseg, which is the program for generating Chinese dictionary, and the other is csft, which is the Chinese version of sphinx.
After rpm -ivh was installed, it went smoothly~~It was installed in less than half a minute.
I went to the official Chinese dictionary library to download it directly. It was very good and thoughtful.
Dictionary text, you can add your own keywords to it.
Then use:mmseg -u to generate the dictionary file: and then rename it. This is the dictionary known to sphinx.
Where to put it? Put it in the dictionary path you configured in, and then it will be almost done. Let’s take a look at some practical programs in sphinx:
csft-indexer csft-search csft-searchd
csft-indexer is a program that generates full-text search index
csft-search is a test to see if the search is effective and is also very useful. It is better to use this to see if the full text search is successful before I developed it with client scripts.
csft-searchd This is the daemon for sphinx search. After starting, you can use the script php python, etc. to open the query.
It's that simple, let's take a look at the two key things.
Configuration file:
{
type = mysql
sql_host = localhost
sql_user = root
sql_pass = 1
sql_db = phpwind75sp3
sql_port = 3306 # optional, default is 3306
#sql_sock = /tmp/
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/
query_log = /var/log/
read_timeout = 5
max_children = 30
pid_file = /var/log/
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/
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/
query_log = /var/log/
read_timeout = 5
max_children = 30
pid_file = /var/log/
max_matches = 1000
#seamless_rotate = 1
#preopen_indexes = 0
#unlink_old = 1
}
Take a look at it and test the client code:
header("Content-type:text/html;charset=utf-8");
include '';
$cl = new SphinxClient();
$cl->SetServer('localhost',3312);
$cl->SetMatchMode(SPH_MATCH_ALL);
$cl->SetArrayResult(true);
$res = $cl->Query("name card","*");
print_r($res);
?>
<?php
header("Content-type:text/html;charset=utf-8");
include '';
$cl = new SphinxClient();
$cl->SetServer('localhost',3312);
$cl->SetMatchMode(SPH_MATCH_ALL);
$cl->SetArrayResult(true);
$res = $cl->Query("name card","*");
print_r($res);
?>
The keyword "name card" was added manually in the dictionary by myself to see if it can be found. The example code is as follows:
(
[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
(
[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
(
[Card] => Array
(
[docs] => 1
[hits] => 1
)
)
)
There is no problem at all, searched out, a few key operations:
[root@beihai365 /]# csft-searchd --stop Stop Search Guard
[root@beihai365 /]# csft-indexer --all Generate indexes for all nodes, you can also generate indexes for a certain node, such as: csft-indexer xx
[root@beihai365 /]# csft-search App Search keywords App, but looking at the following information, no documents were found or hit.
Coreseek Full Text Server 3.1
Copyright (c) 2006-2008
using config file './'...
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, you need to manually pin the path of the configuration file by yourself, which is very inconvenient, so I simply ln -s one in ./, so I don't need to type in --config every time.
I hope this article will be helpful to everyone's PHP programming.