首先建立一个计数表,保存数据表的最新记录ID

CREATE TABLE `sph_counter` (
  `id` int(11) unsigned NOT NULL,
  `max_id` int(11) unsigned NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='sphinx增量表最大记录数';

#定义主索引源

source test
{
    type                    = mysql
    sql_host                = localhost
    sql_user                = root
    sql_pass                = 8888
    sql_db                    = test
    sql_port                = 3306
    sql_query_pre            = SET NAMES utf8
    sql_query_pre           = REPLACE INTO sph_counter SELECT 1, MAX(id) FROM test where status=1 #取最大记录数

    sql_query = select id from test where id<(select max_id from sph_counter where id=1) and  status = 1

 ##如果这里不加id<的条件,合并索引时会报字段数不匹配的错误

 #FATAL: failed to merge index 'test_delta' into index 'test': fulltext fields count mismatch (me=/usr/local/sphinx/var/data/test, in=/usr/local/sphinx/var/data/test_delta, myfields=4, infields=5)
    sql_query_info = select * from test where id = $id
}

#增量索引数据源定义
source test_delta : test
{
        sql_query_pre = SET NAMES utf8
        sql_query = select * from test  where id>=(select max_id from sph_counter where id=1) and status = 1
        sql_query_info = select * from test where id = $id

}

#定义主索引

index test
{
    source            = test            #对应的source名称
    path            = /usr/local/sphinx/var/data/test #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
    docinfo            = extern
    mlock            = 0
    morphology        = none
    min_word_len        = 2
    html_strip                = 1

    #中文分词配置,详情请查看:http://www.coreseek.cn/products-install/coreseek_mmseg/
    charset_dictpath = /usr/local/mmseg/etc/ #BSD、Linux环境下设置,/符号结尾
    #charset_dictpath = etc/                             #Windows环境下设置,/符号结尾,最好给出绝对路径,例如:C:/usr/local/coreseek/etc/...
    charset_type        = zh_cn.utf-8
}
#定义增量索引
index test_delta:test
{
    source            = test_delta            #对应的source名称
    path            = /usr/local/sphinx/var/data/test_delta #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
    docinfo            = extern
    mlock            = 0
    morphology        = none
    min_word_len        = 2
    html_strip                = 1

    #中文分词配置,详情请查看:http://www.coreseek.cn/products-install/coreseek_mmseg/
    charset_dictpath = /usr/local/mmseg/etc/ #BSD、Linux环境下设置,/符号结尾
    #charset_dictpath = etc/                             #Windows环境下设置,/符号结尾,最好给出绝对路径,例如:C:/usr/local/coreseek/etc/...
    charset_type        = zh_cn.utf-8
}

#全局index定义
indexer
{
    mem_limit            = 128M
}

#searchd服务定义
searchd
{
    listen                  =   9312
    read_timeout        = 5
    max_children        = 30
    max_matches            = 1000
    seamless_rotate        = 0
    preopen_indexes        = 0
    unlink_old            = 1
    pid_file = /usr/local/sphinx/var/log/searchd_mysql.pid  #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
    log = /usr/local/sphinx/var/log/searchd_mysql.log        #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
    query_log = /usr/local/sphinx/var/log/query_mysql.log #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
    binlog_path =                                #关闭binlog日志
}

保存配置文件后退出,先停止searchd进程再启动,然后重新生成索引。

停止进程
/usr/local/sphinx/bin/searchd -c /usr/local/sphinx/etc/csft.conf --stop

启动进程
/usr/local/sphinx/bin/searchd -c /usr/local/sphinx/etc/csft.conf

重新生成所有索引
/usr/local/sphinx/bin/indexer -c /usr/local/sphinx/etc/csft.conf --all --rotate
增量索引
/usr/local/sphinx/bin/indexer -c /usr/local/sphinx/etc/csft.conf test_delta --rotate
合并索引
/usr/local/sphinx/bin/indexer -c /usr/local/sphinx/etc/csft.conf --merge test test_delta --rotate

如果合并索引时出现下面问题:

FATAL: failed to merge index 'test_delta' into index 'test': source index preload failed: failed to open /usr/local/sphinx/var/data/test_delta.sph: No such file or directory

停止searchd进程,然后重新启动searchd进程。

增量索引可以放在crontab里根据需要设置几分钟运行一次,然后执行索引合并,至于主索引重建可以选择在访问量不大或者半夜运行。

##每5分钟运行增量索引

*/5 * * * /usr/local/sphinx/bin/indexer -c /usr/local/sphinx/etc/csft.conf test_delta --rotate > /dev/null 2>&1

##每10分钟执行一次增量索引合并

*/10 * * * /usr/local/sphinx/bin/indexer -c /usr/local/sphinx/etc/csft.conf --merge test test_delta --rotate

##凌晨0点5分重新建立主索引

5 0 * * * /usr/local/sphinx/bin/indexer -c /usr/local/sphinx/etc/csft.conf --all --rotate > /dev/null 2>&1

sphinx增量索引的更多相关文章

  1. sphinx 增量索引 实现近实时更新

    一.sphinx增量索引的设置   数据库中的已有数据很大,又不断有新数据加入到数据库中,也希望能够检索到.全部重新建立索引很消耗资源,因为我们需要更新的数据相比较而言很少.例如.原来的数据有几百万条 ...

  2. Sphinx 增量索引更新

    是基于PHP API调用,而不是基于sphinxSE.现在看来sphinxSE比API调用更简单的多,因为之前没有想过sphinxSE,现在先把API的弄明白.涉及到的:sphinx 数据源的设置,简 ...

  3. sphinx 增量索引 及时更新、sphinx indexer索引合成时去旧和过滤办法(转)

    一.sphinx增量索引的设置    数据库中的已有数据很大,又不断有新数据加入到数据库中,也希望能够检索到.全部重新建立索引很消耗资源,因为我们需要更新的数据相比较而言很少.例如.原来的数据有几百万 ...

  4. sphinx增量索引使用

    sphinx在使用过程中如果表的数据量很大,新增加的内容在sphinx索引没有重建之前都是搜索不到的. 这时可以通过建立sphinx增量索引,通过定时更新增量索引,合并主索引的方式,来实现伪实时更新. ...

  5. sphinx增量索引和主索引来实现索引的实时更新

    项目中文章的信息内容因为持续有新增,而文章总量的基数又比较大,所以做搜索的时候,用了主索引+增量索引这种方式来实现索引的实时更新. 实现原理: 1. 新建一张表,记录一下上一次已经创建好索引的最后一条 ...

  6. sphinx 增量索引与主索引使用测试

    2013年10月28日 15:01:16 首先对新增的商品建立增量索引,搜索时只使用增量索引: array (size=1) 0 => array (size=6) 'gid' => st ...

  7. sphinx通过增量索引实现近实时更新

    一.sphinx增量索引实现近实时更新设置 数据库中的已有数据很大,又不断有新数据加入到数据库中,也希望能够检索到.全部重新建立索引很消耗资源,因为我们需要更新的数据相比较而言很少. 例如.原来的数据 ...

  8. sphinx (coreseek)——3、区段查询 与 增量索引实例

    首先本文测试数据100多万的域名的wwwtitle 信息  检索数据: 首先建立临时表格: CREATE TABLE `sph_counter` ( `index_id` ) NOT NULL, `m ...

  9. sphinx(coreseek)——1、增量索引

    首先介绍一下     CoreSeek/Sphinx的发布包 indexer: 用于创建全文索引;    search: 一个简单的命令行(CLI) 的测试程序,用于测试全文索引;    search ...

随机推荐

  1. Qt Style Sheet实践(二):组合框QComboBox的定制

    导读 组合框是一个重要且应用广泛的组件,一般由两个子组件组成:文本下拉单部分和按钮部分.在许多既需要用户选择.又需要用户手动输入的应用场景下,组合框能够很好的满足我们的需求.如我们经常使用的聊天软件Q ...

  2. 关于URL、Web的一些概念

    关于URL ★ 书写路径时,网络文件用斜杠“/”划分不同层级,本地文件管理系统用反斜杠“\”,分隔不同层级:                               如下图示   ★  绝对/相对 ...

  3. 小巧方便的MVC后端验证码,供大家学习借鉴

    调用: public ActionResult Vcode()//验证码 { string code = ValidateCode.CreateRandomCode(4); ValidateCode. ...

  4. MVC5+EF6 入门完整教程12--灵活控制Action权限

    大家久等了. 本篇专题主要讲述MVC中的权限方案. 权限控制是每个系统都必须解决的问题,也是园子里讨论最多的专题之一. 前面的系列文章中我们用到了 SysUser, SysRole, SysUserR ...

  5. DataList分页访问FooterTemplate模板里的控件

    今天做DataList分页的时候,突然想把分页控件写在FooterTemplate模板里面,弄了很久都访问不到控件,终于发现问题所在,以下是访问FooterTemplate里控件的方法: <Fo ...

  6. Win7配置Go环境

    最近想学习下Go语言,先从最基本的Hello Go开始,搭建Go开发环境 一.下载Go包 由于Go官网国内访问经常有问题,可以从国内的镜像下载: http://www.golangtc.com/ 二. ...

  7. PhpWind 8.7中禁止后台管理员随意修改会员用户名功能

    有人反映PHPWind 8.7中后台可以直接修改用户名的功能太过随意,干脆禁掉. OK,研究一下他的代码,admin/usermange.php  tempalate/admin/usermange. ...

  8. 最小化安装centos7下配置网络

    虚拟机操作系统:centos7.0 命令行模式 1.首先明确centos7在最小化安装完是不支持上网的,相应的查看网络以及修改网络参数是不能使用的,最常见的就是我们常用的ifconfig. 2.找到网 ...

  9. 最全面的jdbcUtils,总有一种适合你

    附加jar包,TxQueryRunner.java文件,dbconfig.properties配置文件(点击链接下载): http://files.cnblogs.com/files/xiaoming ...

  10. PHP 操作socket 实现简易聊天室

    <?php $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP ); socket_bind($socket ,'127.0.0.1', ...