Sphinx3安装与简单配置


环境介绍

  1. 操作系统CentOS 7
  2. Sphinx-3.1.1
  3. 虚拟主机域名http://tp5.net

基于Sphinx,支持中文,叫做Coreseek。

下载安装

官网:http://sphinxsearch.com/downloads/current/
下载第一个Linux x64 binaries
在这里插入图片描述

下载后,解压,重命名

$ mv xxxxx /home/sy/local/sphinx-3.1.1

软连接

$ ln -s /home/sy/local/sphinx-3.1.1 /application/sphinx

配置文件

复制一个简单版配置文件

$ cp /application/sphinx/etc/sphinx-mini.conf.dist /application/sphinx/etc/test1.conf

编辑

$ vim /application/sphinx/etc/test1.conf

配置数据目录

# mkdir -p /sphinx/var/data  /sphinx/var/log
# chmod 777 -R /sphinx

数据库示例

/application/sphinx/etc/example.sql
放到数据库中生成测试用例。

mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

documents表数据

mysql> SELECT * FROM documents;
+----+----------+-----------+---------------------+-----------------+---------------------------------------------------------------------------+
| id | group_id | group_id2 | date_added          | title           | content                                                                   |
+----+----------+-----------+---------------------+-----------------+---------------------------------------------------------------------------+
|  1 |        1 |         5 | 2019-07-08 10:56:34 | test one        | this is my test document number one. also checking search within phrases. |
|  2 |        1 |         6 | 2019-07-08 10:56:34 | test two        | this is my test document number two                                       |
|  3 |        2 |         7 | 2019-07-08 10:56:34 | another doc     | this is another group                                                     |
|  4 |        2 |         8 | 2019-07-08 10:56:34 | doc number four | this is to test groups                                                    |
+----+----------+-----------+---------------------+-----------------+---------------------------------------------------------------------------+
4 rows in set (0.01 sec)

test1.conf文件中的SQL查询语句结果如下

mysql> SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content FROM documents;
+----+----------+------------+-----------------+---------------------------------------------------------------------------+
| id | group_id | date_added | title           | content                                                                   |
+----+----------+------------+-----------------+---------------------------------------------------------------------------+
|  1 |        1 | 1562554594 | test one        | this is my test document number one. also checking search within phrases. |
|  2 |        1 | 1562554594 | test two        | this is my test document number two                                       |
|  3 |        2 | 1562554594 | another doc     | this is another group                                                     |
|  4 |        2 | 1562554594 | doc number four | this is to test groups                                                    |
+----+----------+------------+-----------------+---------------------------------------------------------------------------+
4 rows in set (0.00 sec)

查看 /application/sphinx/etc/test1.conf 文件内容

#
# Minimal Sphinx configuration sample (clean, simple, functional)
#

source src1
{
	type			= mysql

	sql_host		= localhost
	sql_user		= root
	sql_pass		= root
	sql_db			= test
	sql_port		= 3306	# optional, default is 3306
	sql_query_pre	= SET NAMES utf8
	sql_query		= \
		SELECT id, group_id, group_id AS gid, UNIX_TIMESTAMP(date_added) AS date_added, title, content \
		FROM documents

	sql_attr_uint		= group_id
	sql_attr_timestamp	= date_added
}


index test1
{
	source			= src1
	path			= /sphinx/var/data/test1
}


index testrt
{
	type			= rt
	rt_mem_limit	= 128M

	path			= /sphinx/var/data/testrt

	rt_field		= title
	rt_field		= content
	rt_attr_uint	= gid
}


indexer
{
	mem_limit		= 128M
}


searchd
{
	listen			= 9312
	listen			= 9306:mysql41
	log				= /sphinx/var/log/searchd.log
	query_log		= /sphinx/var/log/query.log
	read_timeout	= 5
	max_children	= 30
	pid_file		= /sphinx/var/log/searchd.pid
	seamless_rotate	= 1
	preopen_indexes	= 1
	unlink_old		= 1
	workers			= threads # for RT to work
	binlog_path		= /sphinx/var/data
}

创建目录,并设置访问权限。

# mkdir -p /sphinx/var/data
# mkdir -p /sphinx/var/log
# chmod 777 -R /sphinx

生成索引文件

生成索引失败

[sy@sy-pc bin]$ ./indexer --config ../etc/test1.conf --all
Sphinx 3.1.1 (commit 612d99f)
Copyright (c) 2001-2018, Andrew Aksyonoff
Copyright (c) 2008-2016, Sphinx Technologies Inc (http://sphinxsearch.com)

using config file '../etc/test1.conf'...
indexing index 'test1'...
ERROR: index 'test1': sql_connect: failed to load libmysqlclient (or libmariadb) (DSN=mysql://root:***@localhost:3306/test).
total 0 docs, 0.0 Kb
total 0.0 sec, 0.0 Kb/sec, 0 docs/sec
skipping non-plain index 'testrt'...

解决上述问题

# yum install mariadb-devel postgresql-devel unixODBC-devel

生成索引文件
进入/application/sphinx/bin/目录下
生成全部索引文件

[sy@sy-pc bin]$ ./indexer --config ../etc/test1.conf --all
Sphinx 3.1.1 (commit 612d99f)
Copyright (c) 2001-2018, Andrew Aksyonoff
Copyright (c) 2008-2016, Sphinx Technologies Inc (http://sphinxsearch.com)

using config file '../etc/test1.conf'...
indexing index 'test1'...
collected 4 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 4 docs, 0.2 Kb
total 0.1 sec, 1.4 Kb/sec, 28 docs/sec
skipping non-plain index 'testrt'...

创建索引简介:

  • 创建索引,--rotate 参数进行 无缝更新,无需重启服务
  • 全部:
    /application/sphinx/bin/indexer -c /application/sphinx/etc/test1.conf --all --rotate
  • 单个(test1为索引名称):
    /application/sphinx/bin/indexer -c /application/sphinx/etc/test1.conf test1

开启搜索服务,保持后台运行。但是下面这句无效。

[sy@sy-pc bin]$ ./searchd --pidfile
Sphinx 3.1.1 (commit 612d99f)
Copyright (c) 2001-2018, Andrew Aksyonoff
Copyright (c) 2008-2016, Sphinx Technologies Inc (http://sphinxsearch.com)

no config file, using 'sphinxdata' folder...
listening on all interfaces, port=9312
listening on all interfaces, port=9306

启动Sphinx服务

开启搜索服务。用配置文件启动。,为啥有WARNNING?

$ /application/sphinx/bin/searchd -c /application/sphinx/etc/test1.conf
Sphinx 3.1.1 (commit 612d99f)
Copyright (c) 2001-2018, Andrew Aksyonoff
Copyright (c) 2008-2016, Sphinx Technologies Inc (http://sphinxsearch.com)

using config file '/application/sphinx/etc/test1.conf'...
listening on all interfaces, port=9312
listening on all interfaces, port=9306
precaching index 'test1'
precaching index 'testrt'
precached 2 indexes in 0.003 sec

注意,如果上述有WARNING,要解决上述的WARNING,否则NOT SERVING

关闭Sphinx searchd服务
--stop

$ /application/sphinx/bin/searchd -c /application/sphinx/etc/test1.conf  --stop
Sphinx 3.1.1 (commit 612d99f)
Copyright (c) 2001-2018, Andrew Aksyonoff
Copyright (c) 2008-2016, Sphinx Technologies Inc (http://sphinxsearch.com)

using config file '/application/sphinx/etc/test1.conf'...
stop: successfully sent SIGTERM to pid 8377

PHP示例

进入Sphinx安装目录,将$ sphinx/api/sphinxapi.php拷贝到ThinkPHP5框架的extend目录中;
在ThinkPHP 5 框架的application目录下,新建sphinx文件夹;
在sphinx目录下,新建controller文件夹;
在controller目录下,新建Index.php文件;

PHP代码

<?php


namespace app\sphinx\controller;


class Index extends \think\Controller
{
    public function index()
    {
        $sphinx = new \SphinxClient();
        $sphinx->SetServer('localhost',9312);
        //$sphinx->SetMatchMode(SPH_MATCH_ANY);
        $sphinx->SetArrayResult ( true );
        //$res = $sphinx->Query($_GET['key'],'*');
        $res = $sphinx->Query($_GET['key'],'test1');
        echo '<pre>';
        print_r($res);
    }
}

浏览器访问

http://tp5.net/sphinx/index?key=test

执行结果

Array
(
    [error] => 
    [warning] => 
    [status] => 0
    [fields] => Array
        (
            [0] => title
            [1] => content
        )

    [attrs] => Array
        (
            [group_id] => 1
            [date_added] => 2
        )

    [matches] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [weight] => 2421
                    [attrs] => Array
                        (
                            [group_id] => 1
                            [date_added] => 1562554594
                        )

                )

            [1] => Array
                (
                    [id] => 2
                    [weight] => 2421
                    [attrs] => Array
                        (
                            [group_id] => 1
                            [date_added] => 1562554594
                        )

                )

            [2] => Array
                (
                    [id] => 4
                    [weight] => 1442
                    [attrs] => Array
                        (
                            [group_id] => 2
                            [date_added] => 1562554594
                        )

                )

        )

    [total] => 3
    [total_found] => 3
    [time] => 0
    [words] => Array
        (
            [test] => Array
                (
                    [docs] => 3
                    [hits] => 5
                )

        )

)

浏览器访问

http://tp5.net/sphinx/index?key=not

执行结果,无匹配,无[matches],[total]=0

Array
(
    [error] => 
    [warning] => 
    [status] => 0
    [fields] => Array
        (
            [0] => title
            [1] => content
        )

    [attrs] => Array
        (
            [group_id] => 1
            [date_added] => 2
        )

    [total] => 0
    [total_found] => 0
    [time] => 0
    [words] => Array
        (
            [not] => Array
                (
                    [docs] => 0
                    [hits] => 0
                )

        )

)
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值