zoukankan      html  css  js  c++  java
  • sphinx增量索引

    DROP TABLE IF EXISTS `lb`.`adrt_counter`;
    CREATE TABLE  `lb`.`adrt_counter` (
      `counter_id` int(5) NOT NULL,
      `max_doc_id` int(20) NOT NULL,
      `table_name`	char(30) NOT NULL,
      PRIMARY KEY (`counter_id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
    DROP TABLE IF EXISTS `lb`.`asph_counter`;
    CREATE TABLE  `lb`.`asph_counter` (
      `counter_id` int(11) NOT NULL,
      `max_doc_id` int(11) NOT NULL,
      PRIMARY KEY (`counter_id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
    3.重新建立索引:
    如果sphinx正在运行,那么首先停止运行,然后,根据sphinx.conf配置文件来建立所有索引,最后,启动服务
    /usr/local/sphinx/bin/searchd --stop/usr/local/sphinx/bin/indexer -c  /usr/local/sphinx/etc/sphinx.conf -- all/usr/local/sphinx/bin/searchd -c  /usr/local/sphinx/etc/sphinx.conf
    如果想测试增量索引是否成功,往数据库表中插入数据,查找是否能够检索到,这个时候检索应该为空,然后,单独重建 delta索引
    /usr/local/sphinx/bin/indexer -c /usr/lcoal/sphinx/etc/sphinx.conf delta 
    查看是否将新的记录进行了索引。如果成功,此时,再用 /usr/local/sphing/bin/search 工具来检索,能够看到,在main索引中检索到的结果为0,而在delta中检索到结果。当然,前提条件是,检索的词,只在后来插入的数据中存在。
    
    接下来的问题是如何让增量索引与主索引合并
    
    4.索引合并
    合并两个已有索引 有时比 重新索引所有数据有效,虽然,索引合并时,待合并的两个索引都会被读入内存一次,合并后的内容需写入磁盘一次,即,合并100GB和1GB的两个所以,将导致202GB的IO操作
    命令原型:  indexer --merge DSTINDEX  SRCINDEX [--rotate]   将SRCINDEX合并到 DSTINDEX ,所以只有DSTINDEX会改变,如果两个索引都正在提供服务,那么 -- rotate 参数是必须的。例如:将delta合并到main中。
    indexer --merge main delta 
    5.索引自动更新
    需要使用到脚本。
    建立两个脚本:build_main_index.sh 和 build_delta_index.sh.
    
    build_main_index.sh:
    #!/bin/sh
    # 停止正在运行的searchd
    /usr/local/sphinx/bin/searchd -c /usr/local/sphinx/etc/mersphinx.conf  --stop >> /usr/local/sphinx/var/log/sphinx/searchd.log
    #建立主索引
    /usr/local/sphinx/bin/indexer -c  /usr/local/sphinx/etc/mersphinx.conf main >> /usr/local/sphinx/var/log/sphinx/mainindex.log
    #启动searchd守护程序
    /usr/local/sphinx/bin/searchd >> /usr/local/sphinx/var/log/sphinx/searchd.log
    
    build_delta_index.sh
    #!/bin/sh
    #停止sphinx服务,将输出重定向
    /usr/local/sphinx/bin/searchd –stop >> /usr/local/sphinx/var/log/sphinx/searchd.log
    #重新建立索引delta ,将输出重定向
    /usr/local/sphinx/bin/indexer delta –c /usr/local/sphinx/etc/sphinx.conf >>/usr/lcoal/sphinx/var/log/sphinx/deltaindex.log
    #将delta合并到main中
    /usr/local/sphinx/bin/indexer –merge main delta –c /usr/local/sphinx/etc/sphinx.conf >> /usr/lcoal/sphinx/var/log/sphinx/deltaindex.log
    #启动服务
    /usr/local/sphinx/bin/searchd >> /usr/local/sphinx/var/log/sphinx/searchd.log
        
    脚本写好后,需要编译 chmod +x filename 这样才能运行。即
    chmod +x build_main_index.sh
    chmod +x build_delta_index.sh
    
    最后,我们需要脚本能够自动运行,以实现,delta索引每5分钟重新建立,和main索引只在午夜2:30时重新建立。
    
    使用crontab 命令 这有俩个地方可作参考 crontab  crontab文件
    crontab -e 来编辑 crontab文件,如果之前没有使用,会是一个空的文件。写下下面两条语句
    */30 * * * *  /bin/sh /usr/local/sphinx/etc/build_delta_index.sh > /dev/null 2>&1
    30 2 * * * /bin/sh /usr/local/sphinx/etc/build_main_index.sh > /dev/null 2>&1
    
    第一条是表示每30分钟运行 /usr/local/sphinx/etc/下的build_delta_index.sh 脚本,输出重定向。
    第二条是表示 每天的 凌晨2:30分运行 /usr/local/sphinx/etc下的build_main_inde.sh 脚本,输出重定向。
    关于前面的 5个值的设置,在上面的crontab文件中有详细的描述。关于重定向的解释,请看最上面的Crontab笔记 ,也有crontab的介绍。
    
    保存好后:重新启动服务
    [root@test1 init.d]# service crond stop
    [root@test1 init.d]# service crond start
    或者
    /etc/init.d/crontab   start
    
    到现在为止,如果脚本写的没有问题,那么build_delta_index.sh将每30分钟运行一次,而build_main_index.sh将在凌晨2:30分才运行。
    
    要验证的话,在脚本中,有将输出重定向到相关的文件,可以查看下文件中的记录是否增多,也可以看下 /usr/local/sphinx/var/log下的 searchd.log 中,每次重建索引都会有记录。
    
    总结
    1.在修改sphinx.conf文件中犯了一个很不应该的错误,在数据源delta_src 中,把sql_query 错写成 sql_query_pre 于是,即使有新的数据插入数据库,重新建立delta索引也找不到这个记录,而重新建立main索引却没有问题,这个问题就这样弄了整上午,最后,把main_src源中的所有文件都拷贝放入delta_src中,并且把继承main_src去掉,把delta_src当成完整的数据源来看待,这时才提示没有sql_query语句。而之前因为是继承自main_src源,所以即使没有也没有问题。 
    2.数据源和索引名字的问题,之前根据sphinx中文文档,把源和索引都命名为main 和delta,会出现中文检索不到的问题。开始以为是数据源的编码问题。另外写了一个简单点的配置文件,检索没有问题,也就是配置文件写错了。试着改了下名字,以及把set_sql_query=utf8注释掉。之后,开始好用。
    3.索引合并问题,前面已经解释过,两个索引合并时,都要读入,然后还要写一次硬盘,IO操作量很大。而在php API调用时,Query($query,$index)中$index可以设置多个索引名,如Query($query,"main;delta"),也就没有必要一定将两个索引合并,或者,合并的次数不用那么多。
    4.还有一个是没有尝试过的,把增量索引存放到共享内存中(/dev/shm)以提高索引性能,减少系统负荷。
     关于PHP API
    如何能够顺利通过PHP页面来进行检索。
    首先,在服务器上searchd 必须是运行的。
    然后,根据test.php来修改下。
    运行,连接时会出现一个很大的问题 errno =13 permission deny. 最后,查到一个英文的网页,是因为SElinux的原因,关于SELinux在网上能搜到。没有很好的解决办法,只能把SELinux设置为不用。使用的命令有下面两个: setenforce 在 /usr/bin 下
    setenforce 1 设置SELinux 成为enforcing模式
    
    setenforce 0 设置SELinux 成为permissive模式
    

      

  • 相关阅读:
    MySQL和B树的那些事
    记一次临时抱佛脚的性能压测经历
    zookeeper api
    zookeeper笔记
    Mysql优化系列(1)--Innodb重要参数优化
    搞懂MySQL InnoDB B+树索引
    我以为我对Mysql索引很了解,直到我遇到了阿里的面试官
    HDFS原理概念扫盲
    设计原则
    设计模式 6大则
  • 原文地址:https://www.cnblogs.com/yuchunju/p/2531008.html
Copyright © 2011-2022 走看看