sphinx-for-chinese快速配置

建立累计表

CREATE TABLE sph_counter
(
    counter_id INTEGER PRIMARY KEY NOT NULL,
    max_doc_id bigint NOT NULL
);

下载并安装sphinx-for-chinese

下载地址:http://www.sphinx-search.com/

wget http://www.sphinx-search.com/downloads/sphinx-for-chinese-2.2.1-dev-r4311.tar.gz
tar -xvf sphinx-for-chinese-2.2.1-dev-r4311.tar.gz
cd sphinx-for-chinese-2.2.1-dev-r4311
./configure --prefix=/usr/local/sphinx-for-chinese --with-mysql --enable-id64
make -j4 install

下载词典:

wget https://sphinx-for-chinese.googlecode.com/files/xdict_1.1.tar.gz
tar -xzf xdict_1.1.tar.gz
/usr/local/sphinx-for-chinese/bin/mkdict xdict_1.1.txt /usr/local/sphinx-for-chinese/etc/xdict

在索引配置项中添加以下两项:

    charset_type = utf-8
    chinese_dictionary = /usr/local/sphinx-for-chinese/etc/xdict

全局配置

indexer
{
    mem_limit            = 512M
}
searchd
{
    listen          = 9312
    listen          = 9306:mysql41
    log         = /usr/local/sphinx-for-chinese/var/log/searchd.log
    query_log       = /usr/local/sphinx-for-chinese/var/log/query.log
    read_timeout        = 5
    max_children        = 30
    pid_file        = /usr/local/sphinx-for-chinese/var/log/searchd.pid
    max_matches     = 1000
    seamless_rotate     = 1
    preopen_indexes     = 1
    unlink_old      = 1
    workers         = threads # for RT to work
#   binlog_path     = /usr/local/sphinx-for-chinese/var/data 关闭binlog日志
}

测试代码

<?php

$s = new SphinxClient;
$s->setServer("192.168.2.101", 9312);
$s->setMatchMode(SPH_MATCH_ALL);
$s->setMaxQueryTime(3);

$keywords = array(
    'the',
    '一分钱补差价',
    '一分钱 补差价',
    '补差价 一分钱',
    '一分钱乱补差价',
    '一分钱 乱 补差价',
    '一分钱',
    '补差价',
);
foreach ($keywords as $k) {
 $result = $s->query($k);
 echo $k,"\n";
 if(isset($result['matches']) ) {
    print_r($result['matches']);
 }
}