Centos 6下利用sphinx + php + Mysql 构建高效的搜索引擎教程, 并实现增量索引

Home Forums Share your experiences / 经验分享 Centos 6下利用sphinx + php + Mysql 构建高效的搜索引擎教程, 并实现增量索引

Tagged: , , , ,

Viewing 0 reply threads
  • Author
    Posts
    • Participant
      #1 lightwriter #540436

      相关URL

      http://sphinxsearch.com/downloads/current/

      老版本的sphinx,如果想要最新版本的sphinx(已经不再开源)请到官方下载预编译版本

      https://github.com/sphinxsearch/sphinx

      http://pecl.php.net/package/sphinx

      http://git.php.net/?p=pecl/search_engine/sphinx.git

       

      1. 先用Oneinstack安装好PHP, Nignx, Mysql环境,本教程

      Mysql 5.7

      PHP 7.2

      Nginx 1.16.1

       

      2. 安装相关依赖

      最新的代码需要gcc 4.7以上版本才可以编译,所以安装gcc 4.7

      devtools-1.1包含了gcc 4.7, 安装devtools-1.1

       

      先删除之前老版本的的gcc

      yum remove gcc

       

      cd /etc/yum.repos.d

      wget http://people.centos.org/tru/devtools-1.1/devtools-1.1.repo

      yum –enablerepo=testing-1.1-devtools-6 install devtoolset-1.1-gcc devtoolset-1.1-gcc-c++

       

      更新path

      export PATH=/opt/centos/devtoolset-1.1/root/usr/bin/:$PATH

       

      查看gcc版本

      gcc –version

       

      2. 安装sphinx, 下载源码

      注意这个github源码是2.0的,如果想使用最新的需要到官方下载

      git clone https://github.com/sphinxsearch/sphinx.git

      cd sphinx

      一定要加:LIBS=-liconv,否则编译会出错, 或者,导入系统变量,执行命令: export LIBS=”-liconv”

      ./configure –prefix=/usr/local/sphinx LIBS=-liconv

      libiconv用来干什么呢,是在xmlunknowencode时才用到。大多数情况下我们都使用utf-8,几乎用不到这个功能的,所以可以不安装,configure参数如下

      ./configure -without-iconv

      编译,安装sphinx

      make

      make install

       

      如无错误,应该sphinx安装到了/usr/local/sphinx

      cd /usr/local/sphinx

      ll

       

      drwxr-xr-x 2 root root 4096 Sep 11 21:13 bin

      drwxr-xr-x 2 root root 4096 Sep 11 21:13 etc

      drwxr-xr-x 3 root root 4096 Sep 11 21:13 share

      drwxr-xr-x 4 root root 4096 Sep 11 21:13 var

       

      配置

      cd etc

      mv sphinx.conf.dist sphinx.conf

      vi sphinx.conf

       

      配置source src1

      source src1

      {

      #修改数据库连接信息

      sql_host                = localhost

      sql_user                = test

      sql_pass                = test

      sql_db                  = test

      sql_port                = 3306  # optional, default is 3306

       

      #设置UTF8编码

      sql_query_pre           = SET NAMES utf8

      #创建一个表,用于保存本次主索引完成后,已经处理到的最大记录ID, 增量索引只处理大于此ID值的

      sql_query_pre          = CREATE TABLE IF NOT EXISTS sph_counter ( counter_id int(11) NOT NULL, max_doc_id int(11) NOT NULL ,PRIMARY KEY (counter_id) ) ENGINE=MyISAM DEFAULT CHARSET=utf8

      #执行sql_query前将数据表中的最大ID值更新到sph_counter中

      sql_query_pre          = REPLACE INTO sph_counter SELECT 1, MAX(ID) FROM your_table

       

      #修改SQL查询语句,查询语句必须有一个ID主键,text为需要进行全文索引的字段

      #使用了增量索引,只处理ID<=max_doc_id的记录,新增加的记录,放到增量索引中处理

      #$start, $end 在sql_query_range中指定2个值,分别是最小和最大值

      sql_query               = SELECT ID, text FROM your_table WHERE ID<=(SELECT max_doc_id FROM sph_counter WHERE counter_id=1) and ID>=$start AND id<=$end

      # 指定查询范围

      sql_query_range         = SELECT MIN(ID),MAX(ID) FROM your_table

      # 每次取1000条记录

      sql_range_step          = 1000

      # 处理完一批数据后等待时间

      sql_ranged_throttle     = 0

      }

      #创建一个增量索引的数据源, 从src1继承相关的属性

      source src_delta : src1

      {

      sql_query_pre = set names utf8

      # sql_query 执行完成后将最大ID记录更新到sph_counter保存,以便下次的增量索引仍然只处理新增的数据

      sql_query_post         = REPLACE INTO sph_counter SELECT 1, MAX(ID) FROM your_table

      sql_query = SELECT ID,text FROM your_table where ID > (SELECT max_doc_id FROM sph_counter WHERE counter_id=1) and ID>=$start AND ID<=$end

      sql_query_range = SELECT MIN(ID),MAX(ID) FROM your_table where ID>(SELECT max_doc_id FROM sph_counter WHERE counter_id=1)

      }

      #主索引

      index test1

      {

      #数据源

      source                  = src1

      path                    = /var/lib/manticore/test1

      docinfo                 = extern

      dict                    = keywords

      mlock                   = 0

      morphology              = none

      min_word_len            = 1

      html_strip              = 0

      }

      #增量索引

      index index_src_delta: test1

      {

      source                  = src_delta

      path                    = /var/lib/manticore/test1_delta

      }

       

      3, 建立索引, 注意以下命令只适用于首次建立索引,以后增量合并时不能这样

      -c 指定配置文件

      –all 对所有索引从新编制索引。

      –rotate 用于轮换索引,主要是在不停止服务的情况下,增加索引。

      –merge 合并索引

       

      首次建立所有索引:

      /usr/local/sphinx/bin/indexer -c /usr/local/sphinx/etc/sphinx.conf –all –rotate

       

      以后有新增数据时,生成增量索引, 生成增量索引后,新增的数据在主索引中查询不到,但是可以在增量索引表中查到

      只有将增量索引数据合并到主索引数据中去后才能查询到

      /usr/local/sphinx/bin/indexer -c /usr/local/sphinx/etc/sphinx.conf –rotate index_src_delta

       

      将增量索引合并到主索引中去

      /usr/local/sphinx/bin/indexer -c /usr/local/sphinx/etc/sphinx.conf –rotate –merge test1 index_src_delta

       

      4, 启动搜索服务进程

      /usr/local/sphinx/bin/searchd -c /usr/local/sphinx/etc/sphinx.conf

      用MYSQL测试一下

      mysql -h0 -P9306

      SELECT * FROM test1 WHERE MATCH(‘test’); SHOW META;

      MySQL [(none)]> SELECT * FROM test1 WHERE MATCH(‘test’); SHOW META;

      以上返回的是搜索到的内容对应的ID值,再用这些ID值用PHP去MYSQL取回真正的内容

       

      5. 安装libsphinxclient

      php sphinc扩展需要libsphinxclient支持,先安装libsphinxclient

      cd /root/sphinx/api/libsphinxclient/

      ./configure -prefix=/usr/local/sphinx

       

      如果出现如下错误:

      checking how to run the C++ preprocessor… /lib/cpp

      configure: error: C++ preprocessor “/lib/cpp” fails sanity check

       

      安装gcc-c++

      yum install gcc-c++

      再运行

      ./configure -prefix=/usr/local/sphinx

      无错误出现后,编译,安装libsphinxclient

      ./configure -prefix=/usr/local/sphinx

      make &&  make install

      ———————————————————————-

      Libraries have been installed in:

      /root/sphinx-3.1.1/lib

       

      6. 让PHP支持sphinx,编译安装php扩展.

      下载: http://pecl.php.net/package/sphinx

       

      注:写此教程时,官网上只有1.3.3版本下载, 1.3.3只支持php 5.2.2 -> 6.0.0的版本

      但是,我们使用php 7,当前没有发布稳定版本,只能下载dev版本

      进入:http://git.php.net/?p=pecl/search_engine/sphinx.git

      下载最新的版本,例如:

      http://git.php.net/?p=pecl/search_engine/sphinx.git;a=snapshot;h=d958afb6c587f08eee37602dbd8518afdcd72916;sf=tgz

      下载后上传到服务器。因为直接在服务器WGET的提示文件无效

       

      tar -xzf sphinx-d958afb.tar.gz

      cd sphinx-d958afb

      /usr/local/php/bin/phpize

      ./configure –with-php-config=/usr/local/php/bin/php-config –with-sphinx=/usr/local/sphinx

      make

      make install

      如果需要测试扩展,执行 make test, 这需要打开proc_open函数

      vi /usr/local/php/etc/php.ini

      找到proc_open,从disable_functions中移除

      重启nginx

      service nginx restart

      make test

       

      提示安装成功

      Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20170718/

       

      修改php.ini, 加载: sphinx.so

      vi /usr/local/php/etc/php.ini

       

      加入如下extension

      extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20170718/sphinx.so

       

      重启nginx, php-fpm

      service nginx restart

      service php-fpm restart

       

      7. 写一个测试php测试一下sphinx

      <?php

      #phpinfo();

      $sphinx = new SphinxClient;

      $sphinx->setServer(“localhost”, 9312);

      $sphinx->setMatchMode(SPH_MATCH_ANY);

      $sphinx->SetArrayResult ( true );

      $result = $sphinx->query(“test”,”*”);

      print_r( $result );

      ?>

       

      ==========================================================

      3.0版本不需要安装上边的扩展,在sphinx安装目录下的/api/sphinxapi.php文件就是PHP的接口文件

      只需要在php引入这个文件就可以使用了, 如下

      ==========================================================

      <?php

      include “sphinxapi.php”;

      $sphinx = new SphinxClient();

      $sphinx->setServer(‘localhost’,9312);

      #$sphinx->setMatchMode(SPH_MATCH_ANY);

      $sphinx->SetArrayResult ( true );

      $result = $sphinx->query(‘”hacke”/1’,”*”);

      print_r( $result );

      ?>

       

      php函数说明请查看:

      http://sphinxsearch.com/wiki/doku.php?id=sphinx_manual_chinese

Viewing 0 reply threads

You must be logged in to reply to this topic.