欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

分词搜索 sphinx3.1.1+php7+mysql

程序员文章站 2022-05-13 18:29:37
wget http://sphinxsearch.com/files/sphinx-3.1.1-612d99f-linux-amd64.tar.gz tar zxf sphinx-3.1.1-612d99f-linux-amd64.tar.gz mv sphinx-3.1.1 sphinx mv s ......
  1. 下载sphinx3.1.1
    • wget http://sphinxsearch.com/files/sphinx-3.1.1-612d99f-linux-amd64.tar.gz
  2. 解压 
    • tar zxf sphinx-3.1.1-612d99f-linux-amd64.tar.gz
  3. 改名 sphinx 并移动到 /usr/local/
    • mv sphinx-3.1.1 sphinx
    • mv sphinx /usr/local/
  4. 到sphinx目录下创建 data,log文件夹,并赋777权限
    • cd /usr/local/sphinx
    • mkdir data
    • chmod -r 777 data
    • mkdir log
    • chmod -r 777 log
  5. 在/usr/local/sphinx/etc 编写 sphinx.conf 配置文件
  • cd /usr/local/sphinx/etc
  • vim sphinx.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, 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                    = /usr/local/sphinx/data
        min_word_len            = 1
        ngram_len               = 1
        ngram_chars             = u+3000..u+2fa1f
    }
    
    indexer
    {
        mem_limit        = 128m
    }
    
    searchd
    {
            listen                  = 9312
            listen                  = 9306:mysql41
            log                     = /usr/local/sphinx/log/searchd.log
            query_log               = /usr/local/sphinx/log/query.log
            read_timeout            = 5
            max_children            = 30
            pid_file                = /usr/local/sphinx/log/searchd.pid
            seamless_rotate         = 1
            preopen_indexes         = 1
            unlink_old              = 1
            binlog_path             = /usr/local/sphinx/data
    }
  • 在test数据库中 运行/usr/local/sphinx/etc目录下的example.sql文件
    • 进入mysql
    • use test;
    • source /usr/local/sphinx/etc/example.sql
  • 添加索引
    • /usr/local/sphinx/bin/indexer -c  /usr/local/sphinx/etc/sphinx.conf test1
    • 分词搜索 sphinx3.1.1+php7+mysql
  • 运行sphinx
    • /usr/local/sphinx/bin/searchd -c  /usr/local/sphinx/etc/sphinx.conf
    • 分词搜索 sphinx3.1.1+php7+mysql
  • php操作sphinx
    • 下载sphinxapi.php 自带的类文件缺少function
  • https://files.cnblogs.com/files/wxzxc/sphinxclient.zip

      • $sphinx = new sphinxclient();
        $q      = $_get['key'] ?? 'test'; //搜索关键字
        $sql    = "";
        $host   = "127.0.0.1";
        $port   = 9312;
        $index  = "*";
        $sphinx->setserver($host, $port);
        $sphinx->setconnecttimeout(10);
        $sphinx->setarrayresult(true);
        $sphinx->setmatchmode(sph_match_all);
        $res = $sphinx->query($q, $index);
        print_r($res);

         运行结果如下:

      • 分词搜索 sphinx3.1.1+php7+mysql

    结束,记得数据库数据改变后需重新生成索引