美文网首页
Sphinx(斯芬克司)

Sphinx(斯芬克司)

作者: 日风和 | 来源:发表于2016-08-03 11:14 被阅读64次

    简要描述:

    • 基于sql的全文检索引擎,可以结合DB:MySQL、PostgreSQL,同时为MySQL设计了存储引擎插件。
    • 提供API搜索接口,支持多语言,如PHP、Python、Perl、Ruby等。
    • 特点:高速索引 (在新款CPU上,近10 MB/秒); 高速搜索 (2-4G的文本量中平均查询速度不到0.1秒); 高可用性 (单CPU上最大可支持100 GB的文本,100M文档); 提供良好的相关性排名 支持分布式搜索; 提供文档摘要生成; 提供从MySQL内部的插件式存储引擎上搜索 支持布尔,短语, 和近义词查询; 支持每个文档多个全文检索域(默认最大32个); 支持每个文档多属性; 支持断词; 支持单字节编码与UTF-8编码

    安装步骤

    • 参见备注资料

    主要配置:

    ** sphinx.conf **
    source mysql
    {
        type                    = mysql
        sql_host                = localhost
        sql_user                = root
        sql_pass                = 
        sql_db                  = test
        sql_port                = 3306
        sql_query_pre           = SET NAMES utf8
        sql_query               = SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content FROM documents
                                          #sql_query第一列id需为整数
                                          #title、content作为字符串/文本字段,被全文索引
    
        sql_attr_uint            = group_id   #从SQL读取到的值必须为整数
        sql_attr_timestamp       = date_added #从SQL读取到的值必须为整数,作为时间属性
    
        sql_query_info_pre      = SET NAMES utf8   #命令行查询时,设置正确的字符集
        sql_query_info          = SELECT * FROM documents WHERE id=$id #命令行查询时,从数据库读取原始数据信息
    }
    
    index mysql
    {
    
        source          = mysql             #对应的source名称
        path            = var/data/mysql #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
    
        docinfo           = extern
        mlock             = 0
        morphology        = none
        min_word_len      = 1
        html_strip        = 0
    
         #charset_dictpath = /usr/local/mmseg3/etc/ #BSD、Linux环境下设置,/符号结尾
        charset_dictpath = etc/                             #Windows环境下设置,/符号结尾,最好给出绝对路径,例如:C:/usr/local/coreseek/etc/...
        charset_type        = zh_cn.utf-8
    }
     
    indexer
    {
        mem_limit            = 128M
    }
    
    
    searchd
    {
        listen                  =   9312
        read_timeout        = 5
        max_children        = 30
        max_matches            = 1000
        seamless_rotate        = 0
        preopen_indexes        = 0
        unlink_old            = 1
        pid_file = var/log/searchd_mysql.pid  #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
        log = var/log/searchd_mysql.log        #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
        query_log = var/log/query_mysql.log #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
        binlog_path =                                #关闭binlog日志
    
    }
    

    配置项说明

    参数名 说明
    source 定义数据索引源(就是被搜索的数据啦),如果以mysql为索引源,那么source里的信息包含数据库账号、密码、端口、获取数据索引的sql语句等
    index 定义如何处理索引源,例如索引文件目录、分词单位、分词配置文件、去除数据的html标签等
    indexer 定义indexer服务设置,例如内存使用大小限制、文件索引大小限制
    searchd 定义searchd服务设置,用于搜索时的设置,例如服务端口、搜索最大数量限制、搜索超时时间等

    接口调用示例

    <?php
        $s = new SphinxClient();
        $s->setServer('127.0.0.1', 9312);
        $result = $s->Query('max', 'in_bbs_test');
        echo json_encode($result);
        exit;
    

    返回结果示例

    {
        error: "",
        warning: "",
        status: 0,
        fields: [
            "fid",
            "tid",
            "first",
            "author",
            "authorid",
            "subject",
            "dateline",
            "message",
            "useip",
            "port",
            "invisible",
            "anonymous",
            "usesig",
            "htmlon",
            "bbcodeoff",
            "smileyoff",
            "parseurloff",
            "attachment",
            "rate",
            "ratetimes",
            "status",
            "tags",
            "comment",
            "replycredit",
            "position"
        ],
        attrs: [
            
        ],
        matches: {
            
        },
        total: "1000",
        total_found: "4139",
        time: "0.001",
        words: {
            max: {
                docs: "4139",
                hits: "8928"
            }
        }
    }
    

    参考资料

    相关文章

      网友评论

          本文标题:Sphinx(斯芬克司)

          本文链接:https://www.haomeiwen.com/subject/rsyqsttx.html