zoukankan      html  css  js  c++  java
  • Sphinx 安装与使用

    Sphinx 优点

    • 高速索引(接近10M/S)
    • 高速搜索(2-4G文本搜索耗时不到0.1秒)
    • 高可用性(单CPU支持100GB文本,100M文档)
    • 提供相关性排名、分布式搜索、文档摘要(高亮显示)

    Sphinx 缺点

    • 必须有主键
    • 主键必须是整形
    • 不负责数据存储
    • 配置复杂

    Sphinx是介于PHP和mysql的中间层,它提供比mysql更专业的搜索功能,性能更高,因为存在于mysql之前,可以更好的缓解数据库压力

    安装

    sudo apt-get install sphinxsearch -y
    

    安装完成后在 / etc / sphinxsearch / 目录下有以下4个文件

    example.sql 数据源,用于测试

    sphinx.conf.dist 完整的配置,包含全部配置

    sphinx.conf.sample 配置参考

    sphinx-min.conf.dist  精简配置,包含主要配置


    我们来看看 sphinx-min.conf.dist 中的配置项

    拷贝sphinx-min.conf.dist 到当前目录下 sphinx.conf

    源配置:

    source src1
    {
        type            = mysql
    
        sql_host        = localhost
        sql_user        = test
        sql_pass        =
        sql_db          = test
        sql_port        = 3306  # optional, default is 3306
    
        sql_query       = 
            SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content 
            FROM documents
    
        sql_attr_uint       = group_id
        sql_attr_timestamp  = date_added
    }

    块包含源代码,用户名和密码到MySQL服务器的类型。 所述的第一列sql_query应该是唯一的ID。 SQL查询将在每个索引上运行,并将数据转储到Sphinx索引文件。 下面是每个字段和源块本身的描述。

    • type :数据源索引的类型。 在我们的例子,这是MySQL。 其他支持的类型包括pgsql,mssql,xmlpipe2,odbc等。
    • sql_host :主机名MySQL的主机。 在我们的例子,这是localhost 。 这可以是域或IP地址。
    • sql_user :数据库用户名
    • sql_pass :密码
    • sql_db :存储数据的数据库的名称
    • sql_query :查询从数据库到索引那转储数据。

    索引配置:

    index test1
    {
        source          = src1
        path            = /var/lib/sphinxsearch/data/test1
    }
    • source :源块的名称。 在我们的例子,这是src1的 。
    • path :路径保存索引。

    端口和常量配置:

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

    searchd的组件包含端口和其他变量来运行Sphinx守护进程。

    • listen :这Sphinx守护进程运行的端口,后面的协议。 在我们的例子,这是9306:mysql41。 已知的协议是:Sphinx (SphinxAPI)和:mysql41(SphinxQL)
    • query_log :路径保存查询日志
    • pid_file :到Sphinx守护进程的PID文件的路径。
    • seamless_rotate :同时旋转海量数据预缓存的指标,防止searchd的摊位。
    • preopen_indexes :是否强行盘前在启动时的所有索引。
    • unlink_old :是否删除成功旋转旧的索引拷贝。

     

    管理索引

    将数据添加到Sphinx索引

    sudo indexer --all

     

    Sphinx 2.2.9-id64-release (rel22-r5006)
    Copyright (c) 2001-2015, Andrew Aksyonoff
    Copyright (c) 2008-2015, Sphinx Technologies Inc (http://sphinxsearch.com)
    
    using config file '/etc/sphinxsearch/sphinx.conf'...
    indexing index 'test1'...
    collected 4 docs, 0.0 MB
    sorted 0.0 Mhits, 100.0% done
    total 4 docs, 193 bytes
    total 0.032 sec, 5922 bytes/sec, 122.75 docs/sec
    skipping non-plain index 'testrt'...
    total 4 reads, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg
    total 12 writes, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg
    

    表示创建索引成功

    在生产环境中,有必要保持索引为最新。 为了做到这一点,让我们创建一个cronjob。 首先,打开crontab

    crontab -e

    可能会询问您要使用哪个文本编辑器。 选择你喜欢的; 在本教程中,我们使用nano 。

    随后的cronjob将每小时运行一次,并使用我们之前创建的配置文件向索引添加新数据。 将其复制并粘贴到文件末尾,然后保存并关闭文件

    @hourly /usr/bin/indexer --rotate --config /etc/sphinxsearch/sphinx.conf --all

    现在Sphinx已经完全设置和配置,我们可以启动服务并尝试

     


    启动 sphinx

    默认情况下,Sphinx守护程序关闭。 首先,我们将改变这一行启用START=noSTART=yes/etc/default/sphinxsearch

    sudo sed -i 's/START=no/START=yes/g' /etc/default/sphinxsearch

    然后,使用systemctl重启Sphinx守护进程

    sudo systemctl restart sphinxsearch.service

    要检查Sphinx守护程序是否正确运行,请运行

    sudo systemctl status sphinxsearch.service


    测试

    现在,一切都设置好了,让我们测试搜索功能。 使用MySQL接口连接到SphinxQL(在端口9306上)。 您提示将改变为mysql>

    mysql -h0 -P9306

    搜索:test1 代表sphinx配置文件中索引名称  match(搜索条件)

    select * from test1 where match('another');

    测试关键字

    CALL KEYWORDS ('test one three', 'test1', 1);

    输出:

    +------+-----------+------------+------+------+
    | qpos | tokenized | normalized | docs | hits |
    +------+-----------+------------+------+------+
    | 1    | test      | test       | 3    | 5    |
    | 2    | one       | one        | 1    | 2    |
    | 3    | three     | three      | 0    | 0    |
    +------+-----------+------------+------+------+
    3 rows in set (0.00 sec)

    在结果上面可以看到,在test1的指数,Sphinx发现:

    • 5个匹配在3个文档中的关键字'test'
    • 2在1个文档中匹配关键字“1”
    • 0匹配0个文档中的关键字'three'
  • 相关阅读:
    Codeforces Round #498 (Div. 3) E. Military Problem
    codeforces ~ 1009 B Minimum Ternary String(超级恶心的思维题
    二叉排序树
    codeforces ~ 1004 C Sonya and Robots (dp)
    fragment shader的优化
    计算带宽
    trilinear filter
    GPU bubbles
    Dx12 occlusion query
    非意外的PDB错误 OK(0)
  • 原文地址:https://www.cnblogs.com/xiaoliwang/p/9484897.html
Copyright © 2011-2022 走看看