最新文章
标签云
网站统计
- 观海听潮•博客
- 81篇
- 151条
- 299206次
- 120次
- 美国弗吉尼亚州
您现在的位置是:首页 > 技术杂谈 > linux > sphinx sphinx
前几天部署了sphinx,当时去搜索是正常的,但是在我新增了一篇文章后,再去搜索就搜不到新增的文章,所以在网上查了些资料,原来
sphinx查询的是创建索引之前的数据,无法实现实时更新数据。当然,如果定时去重新创建主索引,也是可以解决这个问题的,但是如果
数据量太大的话,会很占内存。所以如果定时把新增的数据重新去创建索引,因为新增的数据不会太多,占用的内存也不会很大,所以网
上都是这种解决方案。
1、新增article_count表
CREATE TABLE article_count(indexes INTEGER PRIMARY KEY NOT NULL,article_last_id INTEGER NOT NULL);
//indexes是主键,ariticle_last_id最后一个文章的id。默认indexes的值是1,代表是blog索引。
2、更改配置文件sphinx.conf文件
//主索引来源
source blog_src{
//blog_src 可以自定义。
//配置数据库
sql_host = localhost
sql_user = root//用户名
sql_pass = root//密码
sql_db = blog//数据库名称
sql_port = 3306 # optional, default is 3306
sql_query_pre = SET NAMES utf8//数据库字符集utf8
sql_query_pre = REPLACE INTO article_count SELECT 1,MAX(id) FROM article_info
//新增内容:更新主索引后将最后文章的id记录到article_count表中。
sql_query = \
SELECT a.id,a.title,UNIX_TIMESTAMP(a.create_time) as create_time,a.content,a.author,c.name FROM \
article_info a left join article_cate_relate b on a.id=b.article_id left join article_cate c on \
b.cate_id=c.id where a.show = 2 and a.id <= (select article_last_id from article_count where indexes = 1) group by a.id
//新增条件,a.show = 2是我之前少了条件,这个不是重点。后面那个条件是id要小于等于article_last_id
//配置数据关联关系,数据库表:
//aritcle_info 文章表 //article_cate_relate 文章分类关联表 //article_cate 文章分类表。
//这样配置可以通过文章标题,内容,作者,分类查询。
#sql_attr_uint = id //这个注释掉就好了,否则会报错:document ID field MUST be the first selected column
sql_attr_timestamp = create_time
}
//增量索引来源
source blog_incre_src{
//blog_incre_src可以自定义。
//配置数据库
sql_host = localhost
sql_user = root//用户名
sql_pass = root//密码
sql_db = blog//数据库名称
sql_port = 3306 # optional, default is 3306
sql_query_pre = SET NAMES utf8//数据库字符集utf8
sql_ranged_throttle = 100
sql_query = \
SELECT a.id,a.title,UNIX_TIMESTAMP(a.create_time) as create_time,a.content,a.author,c.name FROM \
article_info a left join article_cate_relate b on a.id=b.article_id left join article_cate c on \
b.cate_id=c.id where a.show = 2 and a.id > (select article_last_id from article_count where indexes = 1) group by a.id
//新增条件,a.show = 2是我之前少了条件,这个不是重点。后面那个条件是id要大于article_last_id
//配置数据关联关系,数据库表:
//aritcle_info 文章表 //article_cate_relate 文章分类关联表 //article_cate 文章分类表。
//这样配置可以通过文章标题,内容,作者,分类查询。
#sql_attr_uint = id //这个注释掉就好了,否则会报错:document ID field MUST be the first selected column
sql_attr_timestamp = create_time
}
//主索引不变
//新增增量索引
index blog_incre
{
source = blog_incre_src
path = /usr/local/sphinx/var/data/article_incre
ngram_len = 1
ngram_chars = U+3000..U+2FA1F
}
3、重新创建索引,启动
命令:
/usr/local/sphinx/bin/searchd -c /usr/local/sphinx/bin/sphinx.conf --stop #停止searchd
/usr/local/sphinx/bin/indexer -c /usr/local/sphinx/bin/sphinx.conf blog #重新创建主索引
/usr/local/sphinx/bin/indexer -c /usr/local/sphinx/bin/sphinx.conf blog_incre #重新创建增量索引
/usr/local/sphinx/bin/searchd -c /usr/local/sphinx/bin/sphinx.conf #启动searchd
4、定时任务执行
新增主索引脚本:sphinx_blog.sh
#!/bin/sh
/usr/local/sphinx/bin/searchd --stop #停止searchd
/usr/local/sphinx/bin/indexer -c /usr/local/sphinx/bin/sphinx.conf blog --rotate #重新创建主索引
/usr/local/sphinx/bin/searchd #启动searchd
新增增量索引脚本:sphinx_blog_incre.sh
#!/bin/sh
/usr/local/sphinx/bin/searchd --stop
/usr/local/sphinx/bin/indexer -c /usr/local/sphinx/bin/sphinx.conf blog_incre --rotate #重新创建增量索引
/usr/local/sphinx/bin/searchd #启动searchd
crontab脚本命令执行:
#每隔10分钟更新增量索引
*/10 * * * * /root/backup/sphinx_blog_incre.sh
#每天凌晨3点半更新主索引
30 3 * * * /root/backup/sphinx_blog.sh
5、php调用Query($query,"blog;blog_incre");同时调用主索引和增量索引,查到数据是完整的。
下一篇:公众号基本配置
文章评论

评论列表