IT技术博客大学习 共学习 共进步
全部 移动开发 后端 数据库 AI 算法 安全 DevOps 前端 设计 开发者

利用Sphinx实现实时全文检索

超群.com的博客 2010-06-23 12:56:14 累计浏览 3,280 次
本机暂存

Sphinx 0.9.9及以前的版本,原生不支持实时索引,一般的做法是通过主索引+增量索引的方式来实现“准实时”索引,最新的1.10.1(trunk中,尚未发布)终于支持real-time index,查看SVN中文档,我们很容易利用Sphinx搭建一个按需索引(on demand index)的全文检索系统。

参考文章:http://filiptepper.com/2010/05/27/real-time-indexing-and-searching-with-sphinx-1-10-1-dev.html

首先,从sphinxsearch的SVN下载最新的代码,编译安装:

svn checkout http://sphinxsearch.googlecode.com/svn/trunk sphinx
cd sphinx/
./configure --prefix=/path/to/sphinx
make
make install

编译没问题的话,在sphinx安装目录下的etc,建立sphinx.conf的配置文件,记得一定指定中文编码方面的配置搜索,否则中文会有问题:

index rt {
    # 指定索引类型为real-time index
    type = rt
    # 指定utf-8编码
    charset_type  = utf-8
    # 指定utf-8的编码表
    charset_table  = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F
    # 一元分词
    ngram_len = 1
    # 需要分词的字符
    ngram_chars   = U+3000..U+2FA1F
    # 索引文件保存地址
    path = /path/to/sphinx/data/rt
    # 索引列
    rt_field = message
    # 索引属性
    rt_attr_uint = message_id
}
 
searchd {
    log = /path/to/sphinx/log/searchd.log
    query_log = /path/to/sphinx/log/query.log
    pid_file = /path/to/sphinx/log/searchd.pid
    workers = threads
    # sphinx模拟mysql接口,不需要真正的mysql,mysql41表示支持mysql4.1~mysql5.1协议
    listen = 127.0.0.1:9527:mysql41
}

启动sphinx服务:

/path/to/sphinx/bin/searchd --config /path/to/sphinx/etc/sphinx.conf

插入几条数据看看:

ubuntu:chaoqun ~:mysql -h127.0.0.1 -P9527
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 1.10.1-dev (r2351)
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql> INSERT INTO rt VALUES (1, 'this message has a body', 1);
Query OK, 1 row affected (0.01 sec)
 
mysql> INSERT INTO rt VALUES (2, '测试中文OK', 2);
Query OK, 1 row affected (0.00 sec)
 
mysql>

测试全文检索:

mysql> SELECT * FROM rt WHERE MATCH('message');
+------+--------+------------+
| id   | weight | message_id |
+------+--------+------------+
|    1 |   1643 |          1 |
+------+--------+------------+
1 row in set (0.00 sec)
 
mysql> SELECT * FROM rt WHERE MATCH('OK');
+------+--------+------------+
| id   | weight | message_id |
+------+--------+------------+
|    2 |   1643 |          2 |
+------+--------+------------+
1 row in set (0.01 sec)
 
mysql> SELECT * FROM rt WHERE MATCH('中');
+------+--------+------------+
| id   | weight | message_id |
+------+--------+------------+
|    2 |   1643 |          2 |
+------+--------+------------+
1 row in set (0.00 sec)
 
mysql> SELECT * FROM rt WHERE MATCH('我');
Empty set (0.00 sec)
 
mysql>

简单方便,码完收工。

同分类推荐文章

  1. 使用deepseek进行Oracle恢复,引起重大故障 (2026-06-22 10:56:00)
  2. 接手一个只差临门一脚的数据库恢复 (2026-06-18 00:13:09)
  3. 我做了一个 AI 版的 StarRocks 升级风险扫描工具,直接帮我定位到一个风险 (2026-06-15 01:00:00)

查看更多 数据库 文章 →

建议继续学习

  1. 几种常见的基于Lucene的开源搜索解决方案对比 (累计阅读 6,175)
  2. 用Sphinx快速搭建站内搜索功能 (累计阅读 5,706)
  3. 大型网站的Lucene应用 (累计阅读 5,216)
  4. 用PHP和xapian构建全文检索 (累计阅读 4,475)
  5. 用sphinx轻松搞定方便管理的多节点过亿级数据搜索 (累计阅读 3,863)
  6. 用搜索的倒排轻松搞定“好友的文章”类相关推荐功能 (累计阅读 3,509)
  7. 程序员如何写出一份好的文档? (累计阅读 3,359)
  8. MySQL全文检索中不进行全文索引默认过滤词表(ft_stopword_file =>ft_precompiled_stopwords) (累计阅读 3,024)
  9. Mysql+sphinx+中文分词简介(ubuntu) (累计阅读 2,912)
  10. Mysql+sphinx+中文分词简介(ubuntu) (累计阅读 2,815)