VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > 简明python教程 >
  • 分词搜索 sphinx+php+mysql

  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文件夹
    • cd /usr/local/sphinx
    • mkdir data && mkdir log
  5. 在/usr/local/sphinx/etc 编写 sphinx.conf 配置文件
    • vim /usr/local/sphinx/etc/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/test1
          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/
      }
      复制代码
  6. 在test数据库中 运行/usr/local/sphinx/etc目录下的example.sql文件
    • 进入mysql
    • use test;
    • source /usr/local/sphinx/etc/example.sql
  7. 添加索引
    • /usr/local/sphinx/bin/indexer -c  /usr/local/sphinx/etc/sphinx.conf test1
    • /usr/local/sphinx/bin/indexer -c /usr/local/sphinx/etc/sphinx.conf test1 --rotate  // 重新生成索引命令
  8. 运行sphinx
    • /usr/local/sphinx/bin/searchd -c  /usr/local/sphinx/etc/sphinx.conf
    • /usr/local/sphinx/bin/searchd -c /usr/local/sphinx/etc/sphinx.conf --stop  //停止sphinx命令

  9. php操作sphinx
    • 复制/usr/local/sphinx/api/ 目录下的 sphinxapi.php
    • 复制代码
      $sphinx = new SphinxClient();
      $q      = $_GET['key'] ?? 'test'; //搜索关键字
      // 3.1已经弃用了setMatchMode!直接写查询语法就可以
      // 如果要匹配字符串中任意一个词或字使用如下写法
      // '"string1" | "string2" | "string3"'
      
      // '@(title,content) string'  @括号中的是要搜索的字段 可以写多个或单个
      
      // '^$string$'  表示全部匹配 类似mysql中的 fieldName = 'string'
      // 更多用法可以参考正则表达式或者官方文档
      
      // 中文分词扩展建议使用scws,官网安装和使用教程说明很详细,(不过sphinx自带的一元分词已经够用了,一般不需要scws
      //http://www.xunsearch.com/scws/
      
      $sql    = "";
      $host   = "127.0.0.1";
      $port   = 9312;
      $index  = "test1";
      $sphinx->SetServer($host, $port);
      $sphinx->SetConnectTimeout(10);
      $sphinx->SetArrayResult(true);
      $res = $sphinx->Query($q, $index);
      print_r($res);
      复制代码

       运行结果如下:

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


相关教程