coreseek 安装与简单实用
安装环境
系统环境
centos7.2
1 核 2G
软件环境
- coreseek-3.2.14
- lnmp1.5 https://lnmp.org/
安装 mmseg
更新依赖包和安装编译环境
- yum -y install m4 autoconf automake libtool
- yum -y install gcc gcc-c++ wget
- yum -y install MySQL-devel
安装 coreseek
- tar -xzvf coreseek-3.2.14.tar.gz
- cd coreseek-3.2.14
- cd mmseg-3.2.14/
- ./Bootstrap
- ./configure --prefix=/usr/local/mmseg3
- make
- make install
- cd ../csft-3.2.14/
- sh buildconf.sh
- ./configure --prefix=/usr/local/coreseek --without-python --without-unixodbc --with-mmseg --with-mmseg-includes=/usr/local/mmseg3/include/mmseg/ --with-mmseg-libs=/usr/local/mmseg3/lib/ --with-MySQL --host=ARM
- make
- make install
在安装 csft 的过程中出现了三种错误情况, 错误情况如下错误记录, 可以参考修改即可
配置 coreseek 配置文件
配置文件主要修改的几个地方如下, 具体的修改信息请参见 coreseek 配置文件 目录的进行配置
- source src1
- {
- type = MySQL #数据库类型
- sql_host = localhost # MySQL 主机 IP
- sql_user = root # MySQL 用户名
- sql_pass = 123 # MySQL 密码
- sql_db = test # MySQL 数据库
- sql_port = 3306 # MySQL 端口
- sql_sock = /tmp/MySQL.sock #如果是 Linux 下需要开启, 指定 sock 文件
- sql_query_pre = SET NAMES UTF8 # MySQL 检索编码
- sql_query_pre = SET SESSION query_cache_type=OFF #关闭缓存
- sql_query = \ #获取数据的 SQL 语句
- SELECT id, title, content FROM post
- #sql_attr_uint = group_id #对排序字段进行注释
- #sql_attr_timestamp = date_added #对排序字段进行注释
- sql_query_info = SELECT * FROM post WHERE id=$id
- } #这行不需要修改
- source srclthrottled:srcl #继承主数据源
主数据源索引:
- index text1
- {
- source = src1 #索引源声明
- charset_type = utf-8 #数据编码 (设置成 utf8)
- charset_table = #上面指定了 utf-8, 这里需要开启
- }
增量索引
- index testlstemmed:test1 #先进行注释
- index distl #分布式也注释掉
索引器设置
- indexer
- {
- mem_limit = 256M # 内存大小限制 默认是 32M, 推荐为 256M
- } #其他用默认即可
Sphinx 服务进程 searchd 的相关配置
- searchd {
- } #全部用默认的就可以了
除了上述的配置项外还需要单独配置几项 (如果你是安装的 Sphinx 则进行上述配置即可, 如果是 coreseek 则还需要配置下述内容)
- index test1
- {
- #stopwords = G:\data\stopwords.txt
- #wordforms = G:\data\wordforms.txt
- #exceptions = /data/exceptions.txt
- #charset_type = sbcs
添加下面这两行, 意思是把中文分词加入到配置文件中
- charset_type = zh_cn.utf-8
- charset_dictpath = /usr/local/mmseg/etc/ #你安装 mmseg 的目录
- }
数据库操作
- // 创建数据库
- create database test;
- // 创建数据表
- userinfo | CREATE TABLE `userinfo` (
- `id` int(4) NOT NULL AUTO_INCREMENT,
- `name` varchar(10) NOT NULL,
- `age` int(3) NOT NULL,
- `sex` enum('女','男') NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4
- // 插入数据
- INSERT INTO `userinfo` VALUES (1,'张三',12,'女'),(2,'李四',13,'男'),
- (3,'小明',12,'女'),(4,'小红',13,'女'),(5,'小四',12,'女'),(6,'章泽天',13,'女');
创建索引数据 (indexer)
- // 第一次创建索引
- /usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft.conf --all
- // 第二次创建所以 (当我们第一次创建索引之后, 对数据表做了新的操作再按照上面的方式生成索引是会多增加一个参数)
- /usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft.conf -rotate option
如果上述的操作执行正确, 则会提示如下信息
- Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)]
- Copyright (c) 2007-2011,
- Beijing Choice Software Technologies Inc (http://www.coreseek.com)
- using config file '/usr/local/coreseek/etc/csft.conf'...
- WARNING: no such index '-rotate', skipping.
- WARNING: no such index 'option', skipping.
- total 0 reads, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg
- total 0 writes, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg
测试索引数据 (search)
/usr/local/coreseek/bin/search '小'
正常情况下会出现下述信息
- Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)]
- Copyright (c) 2007-2011,
- Beijing Choice Software Technologies Inc (http://www.coreseek.com)
- using config file '/usr/local/coreseek/etc/csft.conf'...
- index 'test1': query '小': returned 3 matches of 3 total in 0.004 sec
- displaying matches:
- 1. document=3, weight=1
- 2. document=4, weight=1
- 3. document=5, weight=1
- words:
- 1. '小': 3 documents, 3 hits
启动 searchd 服务
该服务是可以在 / usr/local/coreseek/etc/csft.conf 文件中的 searchd 配置项中配置, 默认服务的端口已经配置, 可以不单独做配置
- // 启动服务
- /usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/csft.conf
- // 检测服务启动情况
- [root@test ~]# netstat -anp | grep 9312
- tcp 0 0 0.0.0.0:9312 0.0.0.0:* LISTEN 31672/searchd
创建测试代码
测试代码可以参见分享文件中的 测试代码 目录
- require './api/sphinxapi.php';
- $Sphinx = new SphinxClient();
- $Sphinx->SetServer('127.0.0.1', 9312);
- $Sphinx->SetArrayResult(true);
- $Sphinx->SetMatchMode(SPH_MATCH_ALL);
- $Sphinx->SetSortMode(SPH_SORT_RELEVANCE);
- $result = $Sphinx->query('小', '*');
- var_dump($result);
查询出的结果如下
- array(10) {
- ["error"]=>
- string(0) "" ["warning"]=>
- string(0) "" ["status"]=>
- int(0)
- ["fields"]=>
- array(3) {
- [0]=>
- string(4) "name"
- [1]=>
- string(3) "age"
- [2]=>
- string(3) "sex"
- }
- ["attrs"]=>
- array(0) {
- }
- ["matches"]=>
- array(3) {
- [0]=>
- array(3) {
- ["id"]=>
- int(2)
- ["weight"]=>
- string(1) "1"
- ["attrs"]=>
- array(0) {
- }
- }
- [1]=>
- array(3) {
- ["id"]=>
- int(3)
- ["weight"]=>
- string(1) "1"
- ["attrs"]=>
- array(0) {
- }
- }
- [2]=>
- array(3) {
- ["id"]=>
- int(4)
- ["weight"]=>
- string(1) "1"
- ["attrs"]=>
- array(0) {
- }
- }
- }
- ["total"]=>
- string(1) "3"
- ["total_found"]=>
- string(1) "3"
- ["time"]=>
- string(5) "0.003"
- ["words"]=>
- array(1) {
- ["小"]=>
- array(2) {
- ["docs"]=>
- string(1) "3"
- ["hits"]=>
- string(1) "3"
- }
- }
- }
coreseek 错误记录
安装 coerseek 问题一
- cd ../csft-3.2.14/
- sh buildconf.sh
- ./configure --prefix=/usr/local/coreseek --without-python --without-unixodbc --with-mmseg --with-mmseg-includes=/usr/local/mmseg3/include/mmseg/ --with-mmseg-libs=/usr/local/mmseg3/lib/ --with-MySQL --host=ARM
- vi src/sphinxexpr.cpp #然后将所有的 T val = ExprEval ( this->m_pArg, tMatch )..... 修改为 T val =this->ExprEval ( this->m_pArg, tMatch ), 共有三处.
安装 coreseek 错误二
- make[2]: *** [tokenizer_zhcn.o] Error 1
- make[2]: Leaving directory `/home/zyf/zyfwork/csft3.1b3/src'
- make[1]: *** [all] Error 2
- make[1]: Leaving directory `/home/zyf/zyfwork/csft3.1b3/src'
- make: *** [all-recursive] Error 1
- /usr/local/Sphinx/src/Sphinx.cpp:15557: undefined reference to `libiconv_open'
- libsphinx.a(Sphinx.o)(.text+0x53a01):/usr/local/Sphinx/src/Sphinx.cpp:15575: undefined
- reference to `libiconv'
- libsphinx.a(Sphinx.o)(.text+0x53a28):/usr/local/Sphinx/src/Sphinx.cpp:15581: undefined
- reference to `libiconv_close'
- collect2: ld returned 1 exit status
- make[2]: * [indexer] Error 1
- make[2]: Leaving directory `/usr/local/Sphinx/src'
- make[1]: * [all] Error 2
- make[1]: Leaving directory `/usr/local/Sphinx/src'
- make: * [all-recursive] Error 1
修改 configure 文件把 #define USE_LIBICONV 0 最后的数值由 1 改为 0
安装 coreseek 问题三
db_interface_mysql.h:32:25: 致命错误: MySQL/MySQL.h: 没有那个文件或目录
- // Ubuntu 系统解决方案
- sudo apt-get install libmysqlclient-dev
- // centos7 解决方案
- sudo yum install MySQL-devel
参考链接
参考链接一
参考链接二
参考链接三
coreseek 额外说明
安装路径
/usr/local/coreseek
配置目录
/usr/local/coreseek/etc/
执行程序目录
/usr/local/coreseek/bin/
配置文件说明
进入配置目录, 你会找到如下几个文件
-rw-r--r--. 1 root root 905 10 月 13 21:27 example.sql
-rw-r--r--. 1 root root 18954 10 月 13 21:27 Sphinx.conf.dist
-rw-r--r--. 1 root root 956 10 月 13 21:27 Sphinx-min.conf.dist
将 Sphinx.conf.dist 文件复制一份名为 csft.conf 即可. 其余文件可以不管.
执行程序说明
进入执行程序目录可以看到如下几个文件, 具体参考请见 Sphinx 文档 http://sphinxsearch.com/docs/current.html
-rwxr-xr-x. 1 root root 7032560 10 月 13 21:27 indexer // 生成索引程序
-rwxr-xr-x. 1 root root 6650552 10 月 13 21:27 indextool // 调试程序
-rwxr-xr-x. 1 root root 6740544 10 月 13 21:27 search // 搜索程序
-rwxr-xr-x. 1 root root 7970448 10 月 13 21:27 searchd // 提供客户端查询索引服务
-rwxr-xr-x. 1 root root 6370256 10 月 13 21:27 spelldump // 提取字典内容
分享文件目录说明
├── API.zip // coreseek 类文件 (该文件和测试代码目录下的 API.zip 是一样的)
├── coreseek-3.2.14.tar.gz // coreseek 安装程序文件
├── coreseek 配置文件
│ └── csft.conf // 上述 配置 coreseek 配置文件 步骤中提交到的配置文件
├── 测试代码
│ ├── API.zip
│ └── index.PHP // 测试代码
└── 测试数据库
└── test.sql // 测试数据库
来源: https://www.cnblogs.com/qqblog/p/9813904.html