Sphinx+PHP搜索服务
随着自己电影网站资源逐渐增多,增加电影资源搜索服务成为必然。直接操作数据库的搜索,IO口请求增多减低了搜索性能。之前项目中有sphinx的使用基础,加之支持中文检索服务,最后决定采用基于sphinx的Coreseek搜索服务。
下载安装步骤【本人采用 linux环境下 4.1版本,系统支持mysql和xml数据源】:
coreseek下载地址,下载 coreseek-4.1-beta.tar.gz 包
解压gz包,tar zxvf coreseek-4.1-beta.tar.gz
编译安装 mmseg【中文分词包】
./bootstrap
./configure --prefix=/usr/local/mmseg3
make && make install
编译安装 coreseek
sh buildconf.sh #输出的warning信息可以忽略,如果出现error则需要解决
./configure --prefix=/usr/local/coreseek --without-unixodbc --with-mmseg --with-mmseg-includes=/usr/local/mmseg3/include/mmseg/ --with-mmseg-libs=/usr/local/mmseg3/lib/ --with-mysql #with-mmseg-libs就是mmseg中文分词路径
make && make install
电影网站更新周期比较长,sphinx采用主索引+增量索引方式进行索引,最后合并两个索引文件。下面开始部署自己的搜索配置文件:
进入coreseek安装目录下的etc文件,新建或修改 .conf配置文件
配置source源
-
source movie
{
type = mysql
sql_host = localhost #mysql数据库host
sql_user = root #mysql用户
sql_pass = #mysql用户密码
sql_db = movie #movie
sql_port = 3306 # optional, default is 3306
sql_query_pre = SET NAMES utf8
#建立增量索引
sql_query_pre = REPLACE INTO movie_sph_counter SELECT 1, MAX(id) FROM movie
sql_query = SELECT id, UNIX_TIMESTAMP(cdate) AS date ,id AS movie_id ,name, year, type,status,sync_status FROM movie WHERE id#搜索返回字段
sql_attr_uint = movie_id
sql_attr_uint = year
sql_attr_uint = type
sql_attr_uint = date
sql_attr_uint = status
sql_attr_uint = sync_status
sql_field_string = name
sql_query_info_pre = SET NAMES utf8 #命令行查询时,设置正确的字符集
sql_query_info = SELECT * FROM movie WHERE id=$id #命令行查询时,从数据库读取原始数据信息
}#增量索引源
source delta : movie
{
sql_query_pre = SET NAMES utf8
sql_query = SELECT id, UNIX_TIMESTAMP(cdate) AS date ,id AS movie_id ,name , year, type ,status,sync_status FROM movie WHERE id>( SELECT max_movie_id FROM movie_sph_counter WHERE counter_id=1 )
sql_query_post_index = REPLACE INTO movie_sph_counter SELECT 1, MAX(id) FROM movie
} 配置索引
#index定义
index movie
{
source = movie #对应的source名称
path = /usr/local/coreseek/var/data/movie #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
docinfo = extern
mlock = 0
morphology = none
min_word_len = 1
html_strip = 0
#中文分词配置,详情请查看:http://www.coreseek.cn/products-install/coreseek_mmseg/
charset_dictpath = /usr/local/mmseg/etc/ #BSD、Linux环境下设置,/符号结尾 mmseg路径
charset_type = zh_cn.utf-8 #中文编码
}
index delta : movie
{
source = delta
path = /usr/local/coreseek/var/data/movie_delta #注意!!不要和主索引路径名称一样
docinfo = extern
mlock = 0
morphology = none
min_word_len = 1
html_strip = 0
charset_dictpath = /usr/local/mmseg/etc/
charset_type = zh_cn.utf-8
}配置搜索服务
#searchd服务定义
searchd
{
listen = 9312 #端口号,可以自己定义
read_timeout = 5
max_children = 30
max_matches = 1000
seamless_rotate = 0
preopen_indexes = 0
unlink_old = 1
compat_sphinxql_magics=0
pid_file = /usr/local/coreseek/var/log/searchd_mysql.pid #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
log = /usr/local/coreseek/var/log/searchd_mysql.log #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
query_log = /usr/local/coreseek/var/log/query_mysql.log #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
binlog_path = #关闭binlog日志
}执行命令建立索引: /usr/local/coreseek/bin/indexer -c movie.conf --all
后台开启搜索服务运行:/usr/local/coreseek/bin/searchd -c movie.conf
建立定时任务,执行增量索引:/usr/local/coreseek/bin/indexer -c csft_movie.conf delta --rotate
建立定时任务,合并索引:/usr/local/coreseek/bin/indexer -c csft_movie.conf --merge movie delta --merge-dst-range deleted 0 0 --rotate
至此基于sphinx+mysql的搜索服务已经搭建完毕,接下来就是根据sphinxapi.php开发搜索接口代码……
第一次自己搭建sphinx搜索服务,最后测试网站搜索,速度杠杠的。
特此分享,希望对大家有所帮助

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
