码迷,mamicode.com
首页 > 其他好文 > 详细

Sphinx 之 Coreseek、Sphinx-for-chinaese、Sphinx+Scws 评测

时间:2016-06-09 21:02:45      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:

Sphinx是一个基于SQL的全文检索引擎;普遍使用于很多网站;但由于中英文的差异,其本身,对中文的支持并不好。
主要体现在对一段话断词;英文只需按照空格对其分词即可;但对于博大精深的中文来说,却是件困难的事情。

分词在两个地方会用到;
1、索引时,根据分词索引原始数据
2、搜索时,对用户输入分词,到索引中查询

本文提供了三种目前最常用的方案Coreseek、Sphinx-for-chinaese、Sphinx+Scws,并对其简单对比;在合适的场景,选择合适的方案,得出最优的解。

评测:
一、Coreseek 为国人基于Sphinx开发的方案,目前最稳定版,是基于经典的Sphinx0.9.9版

优点:有成熟的文档、以及社区;其分词mmseg为目前国内最为好用分词,索引和搜索分词都可以用到;
缺点:深度开发、版本更新较慢;索引较慢
策略:一个词库管理后台,维护词库;定期生成字典;此套件会自动分词索引;
适用场景:普通青年、搭建差不多的搜索,适用于普遍网站

二、Sphinx-for-chinaese 为国人2基于经典的Sphinx0.9.9版开发的扩展版
优点:部署简单,易操作,内嵌分词和词库,索引和搜索分词都可以用到;
缺点:版本更新较慢;分词较弱;索引相对较慢
策略:同一
适用场景:普通青年、快速搭建搜索的小站

三、Sphinx+Scws 为两套独立系统,单独部署,所谓高内聚低耦合,强烈推荐
优点:两套系统,相对独立,各自单独Server;分词可以做其他用途;版本更新较快;
缺点:部署稍复杂,使用稍复杂;索引分词只能用一元分词,数据量较大
策略:词库管理外;使用时,先调用分词服务,后调用搜索
适用场景:文艺青年、搭建像样点的搜索;好吧文艺青年

由于本人强烈推荐方案三,对方案三有详细描述,其他方案仅提供简单部署方式。

三种方案具体实现如下:

一、coreseek
=======================================================================================================

官网 http://www.coreseek.cn

基础库 http://ftp.gnu.org/gnu/automake/

1、安装

1.0 基础库

#设置路径和中文环境:
export PATH=/usr/local/bin:$PATH
export LC_ALL=zh_CN.UTF-8
export LANG=zh_CN.UTF-8

#依赖包安装顺序
m4、autoconf、automake、libtool

./configure –prefix=/usr/local
make && make install

1.1 安装mmseg

./bootstrap
./configure –prefix=/opt/server/mmseg
make
make install

1.2 安装csft

sh buildconf.sh

./configure –prefix=/opt/server/coreseek –without-unixodbc –with-mmseg=/opt/server/mmseg –with-mmseg-includes=/opt/server/mmseg/include/mmseg/ –with-mmseg-libs=/opt/server/mmseg/lib/ –with-mysql=/opt/server/mysql
make
mkae install

1.3 配置

mv csft_mysql.conf /opt/server/coreseek/etc

配置 /opt/server/coreseek/etc/csft_mysql.conf 参数 ;注意编码问题;

#源定义
source mysql
{
    type                    = mysql

    sql_host                = 127.0.0.1
    sql_user                = root
    sql_pass                = 123456
    sql_db                  = test
    sql_port                = 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_query第一列id需为整数
                                                              #title、content作为字符串/文本字段,被全文索引
    sql_attr_uint            = group_id           #从SQL读取到的值必须为整数
    sql_attr_timestamp        = date_added #从SQL读取到的值必须为整数,作为时间属性

    sql_query_info_pre      = SET NAMES utf8                                        #命令行查询时,设置正确的字符集
    sql_query_info            = SELECT * FROM documents WHERE id=$id #命令行查询时,从数据库读取原始数据信息
}

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

    #中文分词配置,详情请查看:http://www.coreseek.cn/products-install/coreseek_mmseg/
    charset_dictpath = /opt/server/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 = /opt/server/coreseek/var/log/searchd_mysql.pid
    log = /opt/server/coreseek/var/log/searchd_mysql.log
    query_log = /opt/server/coreseek/var/log/query_mysql.log
}

  

1.4测试

/opt/server/coreseek/bin/indexer -c /opt/server/coreseek/etc/csft.conf –all
/opt/server/coreseek/bin/search -c /opt/server/coreseek/etc/csft.conf 网络搜索

1.5词库管理

词库文件
/opt/server/mmseg/bin/mmseg -u /opt/server/mmseg/etc/unigram.txt

unigram.txt.uni 更名为 uni.lib 放入对应字典目录

二、sphinx-for-chinese
=======================================================================================================

官网 http://www.sphinx-search.com

1、安装
./configure –prefix=/opt/server/sphinx-for-chinese –with-mysql=/opt/server/mysql

make
make install

2、中文支持

2.1 生成字典
/opt/server/sphinx-for-chinese/bin/mkdict xdict_1.1.txt xdict

2.2 配置使用字典
mv xdict /opt/server/sphinx-for-chinese/etc/xdict

配置 sphinx.conf 参数

加入
charset_type = utf-8
chinese_dictionary = /opt/server/sphinx-for-chinese/etc/xdict

相关配置详见 /opt/server/sphinx-for-chinese/etc/sphinx.conf;注意编码问题;

#
# Minimal Sphinx configuration sample (clean, simple, functional)
#

source src1
{
	type					= mysql

	sql_host				= 127.0.0.1
	sql_user				= root
	sql_pass				= 123456
	sql_db					= mocube
	sql_port				= 3306	# optional, default is 3306
	sql_query_pre 				= SET NAMES UTF8
	sql_query				= SELECT id, title, content FROM mb_activity_detail

	sql_attr_uint				= id

	sql_query_info				= SELECT * FROM mb_activity_detail WHERE id=$id
}


index test1
{
	source					= src1
	path					= /opt/server/sphinx-for-chinese/var/data/test1
	docinfo					= extern
	charset_type 				= utf-8
	chinese_dictionary 			= /opt/server/sphinx-for-chinese/etc/xdict
}


indexer
{
	mem_limit				= 256M
}


searchd
{
	port					= 9312
	log					= /opt/server/sphinx-for-chinese/var/log/searchd.log
	query_log				= /opt/server/sphinx-for-chinese/var/log/query.log
	read_timeout				= 5
	max_children				= 30
	pid_file				= /opt/server/sphinx-for-chinese/var/log/searchd.pid
	max_matches				= 1000
	seamless_rotate				= 1
	preopen_indexes				= 0
	unlink_old				= 1
}

  

3、测试
/opt/server/sphinx-for-chinese/bin/search -c /opt/server/sphinx-for-chinese/etc/sphinx.conf 分享身边的精彩

三、sphinx+Scws
=======================================================================================================

Scws官网:http://www.ftphp.com/scws/

1、安装

./configure –prefix=/opt/server/sphinx –with-mysql=/opt/server/mysql

make
make install

2、配置

见/opt/server/sphinx/etc/sphinx.conf

#
#  基础索引方案
#

source src1
{
	type					= mysql

	sql_host				= 127.0.0.1
	sql_user				= root
	sql_pass				= 123456
	sql_db					= mocube
	sql_port				= 3306
	sql_query_pre 			= SET NAMES UTF8
	sql_query				= SELECT id, title, content FROM mb_activity_detail

	sql_attr_uint			= id

	sql_query_info			= SELECT * FROM mb_activity_detail WHERE id=$id
}


index test1
{
	source			= src1
	path			= /opt/server/sphinx/var/data/test1
	docinfo			= extern
	mlock			= 0
	stopwords		=
	min_prefix_len	= 0
	min_infix_len	= 0
	min_word_len	= 1
	charset_type	= utf-8
	charset_table = U+FF10..U+FF19->0..9, 0..9, U+FF41..U+FF5A->a..z, U+FF21..U+FF3A->a..z,A..Z->a..z, a..z, U+0149, U+017F, U+0138, U+00DF, U+00FF, U+00C0..U+00D6->U+00E0..U+00F6,U+00E0..U+00F6, U+00D8..U+00DE->U+00F8..U+00FE, U+00F8..U+00FE, U+0100->U+0101, U+0101,U+0102->U+0103, U+0103, U+0104->U+0105, U+0105, U+0106->U+0107, U+0107, U+0108->U+0109,U+0109, U+010A->U+010B, U+010B, U+010C->U+010D, U+010D, U+010E->U+010F, U+010F,U+0110->U+0111, U+0111, U+0112->U+0113, U+0113, U+0114->U+0115, U+0115, U+0116->U+0117,U+0117, U+0118->U+0119, U+0119, U+011A->U+011B, U+011B, U+011C->U+011D, U+011D,U+011E->U+011F, U+011F, U+0130->U+0131, U+0131, U+0132->U+0133, U+0133, U+0134->U+0135,U+0135, U+0136->U+0137, U+0137, U+0139->U+013A, U+013A, U+013B->U+013C, U+013C,U+013D->U+013E, U+013E, U+013F->U+0140, U+0140, U+0141->U+0142, U+0142, U+0143->U+0144,U+0144, U+0145->U+0146, U+0146, U+0147->U+0148, U+0148, U+014A->U+014B, U+014B,U+014C->U+014D, U+014D, U+014E->U+014F, U+014F, U+0150->U+0151, U+0151, U+0152->U+0153,U+0153, U+0154->U+0155, U+0155, U+0156->U+0157, U+0157, U+0158->U+0159, U+0159,U+015A->U+015B, U+015B, U+015C->U+015D, U+015D, U+015E->U+015F, U+015F, U+0160->U+0161,U+0161, U+0162->U+0163, U+0163, U+0164->U+0165, U+0165, U+0166->U+0167, U+0167,U+0168->U+0169, U+0169, U+016A->U+016B, U+016B, U+016C->U+016D, U+016D, U+016E->U+016F,U+016F, U+0170->U+0171, U+0171, U+0172->U+0173, U+0173, U+0174->U+0175, U+0175,U+0176->U+0177, U+0177, U+0178->U+00FF, U+00FF, U+0179->U+017A, U+017A, U+017B->U+017C,U+017C, U+017D->U+017E, U+017E, U+0410..U+042F->U+0430..U+044F, U+0430..U+044F,U+05D0..U+05EA, U+0531..U+0556->U+0561..U+0586, U+0561..U+0587, U+0621..U+063A, U+01B9,U+01BF, U+0640..U+064A, U+0660..U+0669, U+066E, U+066F, U+0671..U+06D3, U+06F0..U+06FF,U+0904..U+0939, U+0958..U+095F, U+0960..U+0963, U+0966..U+096F, U+097B..U+097F,U+0985..U+09B9, U+09CE, U+09DC..U+09E3, U+09E6..U+09EF, U+0A05..U+0A39, U+0A59..U+0A5E,U+0A66..U+0A6F, U+0A85..U+0AB9, U+0AE0..U+0AE3, U+0AE6..U+0AEF, U+0B05..U+0B39,U+0B5C..U+0B61, U+0B66..U+0B6F, U+0B71, U+0B85..U+0BB9, U+0BE6..U+0BF2, U+0C05..U+0C39,U+0C66..U+0C6F, U+0C85..U+0CB9, U+0CDE..U+0CE3, U+0CE6..U+0CEF, U+0D05..U+0D39, U+0D60,U+0D61, U+0D66..U+0D6F, U+0D85..U+0DC6, U+1900..U+1938, U+1946..U+194F, U+A800..U+A805,U+A807..U+A822, U+0386->U+03B1, U+03AC->U+03B1, U+0388->U+03B5, U+03AD->U+03B5,U+0389->U+03B7, U+03AE->U+03B7, U+038A->U+03B9, U+0390->U+03B9, U+03AA->U+03B9,U+03AF->U+03B9, U+03CA->U+03B9, U+038C->U+03BF, U+03CC->U+03BF, U+038E->U+03C5,U+03AB->U+03C5, U+03B0->U+03C5, U+03CB->U+03C5, U+03CD->U+03C5, U+038F->U+03C9,U+03CE->U+03C9, U+03C2->U+03C3, U+0391..U+03A1->U+03B1..U+03C1,U+03A3..U+03A9->U+03C3..U+03C9, U+03B1..U+03C1, U+03C3..U+03C9, U+0E01..U+0E2E,U+0E30..U+0E3A, U+0E40..U+0E45, U+0E47, U+0E50..U+0E59, U+A000..U+A48F, U+4E00..U+9FBF,U+3400..U+4DBF, U+20000..U+2A6DF, U+F900..U+FAFF, U+2F800..U+2FA1F, U+2E80..U+2EFF,U+2F00..U+2FDF, U+3100..U+312F, U+31A0..U+31BF, U+3040..U+309F, U+30A0..U+30FF,U+31F0..U+31FF, U+AC00..U+D7AF, U+1100..U+11FF, U+3130..U+318F, U+A000..U+A48F,U+A490..U+A4CF
	ngram_len = 1 
	ngram_chars = U+4E00..U+9FBF, U+3400..U+4DBF, U+20000..U+2A6DF, U+F900..U+FAFF,U+2F800..U+2FA1F, U+2E80..U+2EFF, U+2F00..U+2FDF, U+3100..U+312F, U+31A0..U+31BF,U+3040..U+309F, U+30A0..U+30FF, U+31F0..U+31FF, U+AC00..U+D7AF, U+1100..U+11FF,U+3130..U+318F, U+A000..U+A48F, U+A490..U+A4CF
}


index testrt
{
	type			= rt
	rt_mem_limit		= 32M

	path			= /opt/server/sphinx/var/data/testrt
	charset_type		= utf-8

	rt_field		= title
	rt_field		= content
	rt_attr_uint		= id
}


indexer
{
	mem_limit		= 32M
}


searchd
{
	listen			= 9312
	log			= /opt/server/sphinx/var/log/searchd.log
	query_log		= /opt/server/sphinx/var/log/query.log
	read_timeout		= 5
	max_children		= 30
	pid_file		= /opt/server/sphinx/var/log/searchd.pid
	max_matches		= 1000
	seamless_rotate		= 1
	preopen_indexes		= 1
	unlink_old		= 1
	workers			= threads # for RT to work
	binlog_path		= /opt/server/sphinx/var/data
}

  

3、启动服务

/opt/server/sphinx/bin/searchd

4、php分词 scws

官网 http://www.ftphp.com/scws/

4.1 安装
./configure –prefix=/opt/server/scws
make
make install

4.2 词库
scws-dict-chs-utf8.tar.bz2 解压放入 /opt/server/scws/etc

词库 dict.utf-8.xdb
规则 rules.utf-8.ini

4.3 php 扩展

源码在 phpext下

/opt/server/php/bin/phpize
./configure –with-scws=/opt/server/scws –with-php-config=/opt/server/php/bin/php-config
make
make install

vi php.ini
[scws]
extension = scws.so
scws.default.charset = utf-8
scws.default.fpath = /opt/server/scws/etc

查看
/opt/server/php/bin/php -m |grep scws

4.4 分词测试

官方测试地址:http://www.ftphp.com/scws/docs.php

详见测试文件 test_all.php

<?php

	echo ‘<p>‘.microtime(true).‘</p>‘;

	//搜索词
	$key = trim($_GET[‘k‘]);

//========================================分词

	$so = scws_new();
	$so->set_charset(‘utf-8‘);
	//默认词库
	$so->add_dict(ini_get(‘scws.default.fpath‘) . ‘/dict.utf-8.xdb‘);
	//自定义词库
	$so->add_dict(‘./dd.txt‘,SCWS_XDICT_TXT);
	//默认规则
	$so->set_rule(ini_get(‘scws.default.fpath‘) . ‘/rules.utf-8.ini‘);

	//设定分词返回结果时是否去除一些特殊的标点符号
	$so->set_ignore(true);

	//设定分词返回结果时是否复式分割,如“中国人”返回“中国+人+中国人”三个词。
	// 按位异或的 1 | 2 | 4 | 8 分别表示: 短词 | 二元 | 主要单字 | 所有单字
	//1,2,4,8 分别对应常量 SCWS_MULTI_SHORT  SCWS_MULTI_DUALITY SCWS_MULTI_ZMAIN SCWS_MULTI_ZALL
	$so->set_multi(false);

	//设定是否将闲散文字自动以二字分词法聚合
	$so->set_duality(false);

	//设定搜索词
	$so->send_text($key);

	$words_array = $so->get_result();

	foreach($words_array as $v)
	{
		$words = $words.‘ ‘.$v[‘word‘];
	}

	$so->close();


	echo ‘<p>输入:‘.$key.‘</p>‘;
	echo ‘<p>分词:‘.$words.‘</p>‘;

	//var_dump($words);
	//exit;

//========================================搜索

	require_once ‘sphinxapi.php‘;

	$sc = new SphinxClient();

	$sc->SetServer(‘127.0.0.1‘,9312);

	$sc->SetMatchMode(SPH_MATCH_ALL);

	$sc->SetArrayResult(TRUE);

	$res = $sc->Query($words);

	var_dump($res);


	echo ‘<p>‘.microtime(true).‘</p>‘;


	exit;

?>

  

其他高阶操作
=======================================================================================================
5 索引

//索引某个索引
/opt/server/sphinx/bin/indexer test1

//searchd 索引某个索引
/opt/server/sphinx/bin/indexer test1 –rotate

//指定索引搜索
/opt/server/sphinx/bin/indexer -i test1 ‘逗她男’

5.1 增量索引方案

//创建表记录偏移
CREATE TABLE search_counter
(
counterid INTEGER PRIMARY KEY NOT NULL,
max_doc_id INTEGER NOT NULL
);

//增量索引
/opt/server/sphinx/bin/indexer test1stemmed –rotate

//合并索引
/opt/server/sphinx/bin/indexer –merge test1 test1stemmed –rotate

索引策略

1、搜索时,同时从主索引和增量索引取数据
2、每5分钟,运行一次增量索引;满足新数据搜索需求
3、每晚,运行一次主索引,同时会更新索引标示;再运行增量索引,实质为清空增量索引,避免与主索引重复
4、好处:避免开合并索引,合并索引效率较差
5、如数据量特别大,可考虑合并索引的方案

索引策略shell

//add.sh
#!/bin/sh
/opt/server/sphinx/bin/indexer test1stemmed –rotate >> /opt/server/sphinx/var/log/add.sh.log

//all.sh
#!/bin/sh
/opt/server/sphinx/bin/indexer test1 –rotate >> /opt/server/sphinx/var/log/all.sh.log
/opt/server/sphinx/bin/indexer test1stemmed –rotate >> /opt/server/sphinx/var/log/add.sh.log

详情配置

/opt/server/sphinx/etc/sphinx.conf

#
# 增量索引方案
#

source src1
{
	type					= mysql

	sql_host				= 127.0.0.1
	sql_user				= root
	sql_pass				= 123456
	sql_db					= mocube
	sql_port				= 3306
	sql_query_pre				= SET NAMES UTF8
	sql_query_pre				= REPLACE INTO search_counter SELECT 1,MAX(uid) FROM mb_renren  #创建主索引前更改标识位置
	sql_query				= SELECT uid,email,url,nickname,is_access FROM mb_renren
	sql_attr_uint				= is_access	

	sql_query_info				= SELECT * FROM mb_renren WHERE uid=$id
}


source src1throttled
{
	type					= mysql

	sql_host				= 127.0.0.1
	sql_user				= root
	sql_pass				= 123456
	sql_db					= mocube
	sql_port				= 3306
	sql_query_pre 				= SET NAMES UTF8
	sql_query				= SELECT uid,email,url,nickname,is_access FROM mb_renren WHERE uid>(SELECT max_doc_id FROM search_counter WHERE counterid=1) #增量索引是id大于标识位置的部分
	sql_attr_uint                           = is_access
	sql_query_info				= SELECT * FROM mb_renren WHERE uid=$id
}


index test1
{
	source			= src1
	path			= /opt/server/sphinx/var/data/test1
	docinfo			= extern
	mlock			= 0
	stopwords		=
	min_prefix_len	= 0
	min_infix_len	= 0
	min_word_len	= 1
	charset_type	= utf-8
	charset_table = U+FF10..U+FF19->0..9, 0..9, U+FF41..U+FF5A->a..z, U+FF21..U+FF3A->a..z,A..Z->a..z, a..z, U+0149, U+017F, U+0138, U+00DF, U+00FF, U+00C0..U+00D6->U+00E0..U+00F6,U+00E0..U+00F6, U+00D8..U+00DE->U+00F8..U+00FE, U+00F8..U+00FE, U+0100->U+0101, U+0101,U+0102->U+0103, U+0103, U+0104->U+0105, U+0105, U+0106->U+0107, U+0107, U+0108->U+0109,U+0109, U+010A->U+010B, U+010B, U+010C->U+010D, U+010D, U+010E->U+010F, U+010F,U+0110->U+0111, U+0111, U+0112->U+0113, U+0113, U+0114->U+0115, U+0115, U+0116->U+0117,U+0117, U+0118->U+0119, U+0119, U+011A->U+011B, U+011B, U+011C->U+011D, U+011D,U+011E->U+011F, U+011F, U+0130->U+0131, U+0131, U+0132->U+0133, U+0133, U+0134->U+0135,U+0135, U+0136->U+0137, U+0137, U+0139->U+013A, U+013A, U+013B->U+013C, U+013C,U+013D->U+013E, U+013E, U+013F->U+0140, U+0140, U+0141->U+0142, U+0142, U+0143->U+0144,U+0144, U+0145->U+0146, U+0146, U+0147->U+0148, U+0148, U+014A->U+014B, U+014B,U+014C->U+014D, U+014D, U+014E->U+014F, U+014F, U+0150->U+0151, U+0151, U+0152->U+0153,U+0153, U+0154->U+0155, U+0155, U+0156->U+0157, U+0157, U+0158->U+0159, U+0159,U+015A->U+015B, U+015B, U+015C->U+015D, U+015D, U+015E->U+015F, U+015F, U+0160->U+0161,U+0161, U+0162->U+0163, U+0163, U+0164->U+0165, U+0165, U+0166->U+0167, U+0167,U+0168->U+0169, U+0169, U+016A->U+016B, U+016B, U+016C->U+016D, U+016D, U+016E->U+016F,U+016F, U+0170->U+0171, U+0171, U+0172->U+0173, U+0173, U+0174->U+0175, U+0175,U+0176->U+0177, U+0177, U+0178->U+00FF, U+00FF, U+0179->U+017A, U+017A, U+017B->U+017C,U+017C, U+017D->U+017E, U+017E, U+0410..U+042F->U+0430..U+044F, U+0430..U+044F,U+05D0..U+05EA, U+0531..U+0556->U+0561..U+0586, U+0561..U+0587, U+0621..U+063A, U+01B9,U+01BF, U+0640..U+064A, U+0660..U+0669, U+066E, U+066F, U+0671..U+06D3, U+06F0..U+06FF,U+0904..U+0939, U+0958..U+095F, U+0960..U+0963, U+0966..U+096F, U+097B..U+097F,U+0985..U+09B9, U+09CE, U+09DC..U+09E3, U+09E6..U+09EF, U+0A05..U+0A39, U+0A59..U+0A5E,U+0A66..U+0A6F, U+0A85..U+0AB9, U+0AE0..U+0AE3, U+0AE6..U+0AEF, U+0B05..U+0B39,U+0B5C..U+0B61, U+0B66..U+0B6F, U+0B71, U+0B85..U+0BB9, U+0BE6..U+0BF2, U+0C05..U+0C39,U+0C66..U+0C6F, U+0C85..U+0CB9, U+0CDE..U+0CE3, U+0CE6..U+0CEF, U+0D05..U+0D39, U+0D60,U+0D61, U+0D66..U+0D6F, U+0D85..U+0DC6, U+1900..U+1938, U+1946..U+194F, U+A800..U+A805,U+A807..U+A822, U+0386->U+03B1, U+03AC->U+03B1, U+0388->U+03B5, U+03AD->U+03B5,U+0389->U+03B7, U+03AE->U+03B7, U+038A->U+03B9, U+0390->U+03B9, U+03AA->U+03B9,U+03AF->U+03B9, U+03CA->U+03B9, U+038C->U+03BF, U+03CC->U+03BF, U+038E->U+03C5,U+03AB->U+03C5, U+03B0->U+03C5, U+03CB->U+03C5, U+03CD->U+03C5, U+038F->U+03C9,U+03CE->U+03C9, U+03C2->U+03C3, U+0391..U+03A1->U+03B1..U+03C1,U+03A3..U+03A9->U+03C3..U+03C9, U+03B1..U+03C1, U+03C3..U+03C9, U+0E01..U+0E2E,U+0E30..U+0E3A, U+0E40..U+0E45, U+0E47, U+0E50..U+0E59, U+A000..U+A48F, U+4E00..U+9FBF,U+3400..U+4DBF, U+20000..U+2A6DF, U+F900..U+FAFF, U+2F800..U+2FA1F, U+2E80..U+2EFF,U+2F00..U+2FDF, U+3100..U+312F, U+31A0..U+31BF, U+3040..U+309F, U+30A0..U+30FF,U+31F0..U+31FF, U+AC00..U+D7AF, U+1100..U+11FF, U+3130..U+318F, U+A000..U+A48F,U+A490..U+A4CF
	ngram_len = 1 
	ngram_chars = U+4E00..U+9FBF, U+3400..U+4DBF, U+20000..U+2A6DF, U+F900..U+FAFF,U+2F800..U+2FA1F, U+2E80..U+2EFF, U+2F00..U+2FDF, U+3100..U+312F, U+31A0..U+31BF,U+3040..U+309F, U+30A0..U+30FF, U+31F0..U+31FF, U+AC00..U+D7AF, U+1100..U+11FF,U+3130..U+318F, U+A000..U+A48F, U+A490..U+A4CF
}

index test1stemmed
{
	source			= src1throttled
	path			= /opt/server/sphinx/var/data/test1stemmed
	docinfo			= extern
	mlock			= 0
	stopwords		=
	min_prefix_len	= 0
	min_infix_len	= 0
	min_word_len	= 1
	charset_type	= utf-8
	charset_table = U+FF10..U+FF19->0..9, 0..9, U+FF41..U+FF5A->a..z, U+FF21..U+FF3A->a..z,A..Z->a..z, a..z, U+0149, U+017F, U+0138, U+00DF, U+00FF, U+00C0..U+00D6->U+00E0..U+00F6,U+00E0..U+00F6, U+00D8..U+00DE->U+00F8..U+00FE, U+00F8..U+00FE, U+0100->U+0101, U+0101,U+0102->U+0103, U+0103, U+0104->U+0105, U+0105, U+0106->U+0107, U+0107, U+0108->U+0109,U+0109, U+010A->U+010B, U+010B, U+010C->U+010D, U+010D, U+010E->U+010F, U+010F,U+0110->U+0111, U+0111, U+0112->U+0113, U+0113, U+0114->U+0115, U+0115, U+0116->U+0117,U+0117, U+0118->U+0119, U+0119, U+011A->U+011B, U+011B, U+011C->U+011D, U+011D,U+011E->U+011F, U+011F, U+0130->U+0131, U+0131, U+0132->U+0133, U+0133, U+0134->U+0135,U+0135, U+0136->U+0137, U+0137, U+0139->U+013A, U+013A, U+013B->U+013C, U+013C,U+013D->U+013E, U+013E, U+013F->U+0140, U+0140, U+0141->U+0142, U+0142, U+0143->U+0144,U+0144, U+0145->U+0146, U+0146, U+0147->U+0148, U+0148, U+014A->U+014B, U+014B,U+014C->U+014D, U+014D, U+014E->U+014F, U+014F, U+0150->U+0151, U+0151, U+0152->U+0153,U+0153, U+0154->U+0155, U+0155, U+0156->U+0157, U+0157, U+0158->U+0159, U+0159,U+015A->U+015B, U+015B, U+015C->U+015D, U+015D, U+015E->U+015F, U+015F, U+0160->U+0161,U+0161, U+0162->U+0163, U+0163, U+0164->U+0165, U+0165, U+0166->U+0167, U+0167,U+0168->U+0169, U+0169, U+016A->U+016B, U+016B, U+016C->U+016D, U+016D, U+016E->U+016F,U+016F, U+0170->U+0171, U+0171, U+0172->U+0173, U+0173, U+0174->U+0175, U+0175,U+0176->U+0177, U+0177, U+0178->U+00FF, U+00FF, U+0179->U+017A, U+017A, U+017B->U+017C,U+017C, U+017D->U+017E, U+017E, U+0410..U+042F->U+0430..U+044F, U+0430..U+044F,U+05D0..U+05EA, U+0531..U+0556->U+0561..U+0586, U+0561..U+0587, U+0621..U+063A, U+01B9,U+01BF, U+0640..U+064A, U+0660..U+0669, U+066E, U+066F, U+0671..U+06D3, U+06F0..U+06FF,U+0904..U+0939, U+0958..U+095F, U+0960..U+0963, U+0966..U+096F, U+097B..U+097F,U+0985..U+09B9, U+09CE, U+09DC..U+09E3, U+09E6..U+09EF, U+0A05..U+0A39, U+0A59..U+0A5E,U+0A66..U+0A6F, U+0A85..U+0AB9, U+0AE0..U+0AE3, U+0AE6..U+0AEF, U+0B05..U+0B39,U+0B5C..U+0B61, U+0B66..U+0B6F, U+0B71, U+0B85..U+0BB9, U+0BE6..U+0BF2, U+0C05..U+0C39,U+0C66..U+0C6F, U+0C85..U+0CB9, U+0CDE..U+0CE3, U+0CE6..U+0CEF, U+0D05..U+0D39, U+0D60,U+0D61, U+0D66..U+0D6F, U+0D85..U+0DC6, U+1900..U+1938, U+1946..U+194F, U+A800..U+A805,U+A807..U+A822, U+0386->U+03B1, U+03AC->U+03B1, U+0388->U+03B5, U+03AD->U+03B5,U+0389->U+03B7, U+03AE->U+03B7, U+038A->U+03B9, U+0390->U+03B9, U+03AA->U+03B9,U+03AF->U+03B9, U+03CA->U+03B9, U+038C->U+03BF, U+03CC->U+03BF, U+038E->U+03C5,U+03AB->U+03C5, U+03B0->U+03C5, U+03CB->U+03C5, U+03CD->U+03C5, U+038F->U+03C9,U+03CE->U+03C9, U+03C2->U+03C3, U+0391..U+03A1->U+03B1..U+03C1,U+03A3..U+03A9->U+03C3..U+03C9, U+03B1..U+03C1, U+03C3..U+03C9, U+0E01..U+0E2E,U+0E30..U+0E3A, U+0E40..U+0E45, U+0E47, U+0E50..U+0E59, U+A000..U+A48F, U+4E00..U+9FBF,U+3400..U+4DBF, U+20000..U+2A6DF, U+F900..U+FAFF, U+2F800..U+2FA1F, U+2E80..U+2EFF,U+2F00..U+2FDF, U+3100..U+312F, U+31A0..U+31BF, U+3040..U+309F, U+30A0..U+30FF,U+31F0..U+31FF, U+AC00..U+D7AF, U+1100..U+11FF, U+3130..U+318F, U+A000..U+A48F,U+A490..U+A4CF
	ngram_len = 1 
	ngram_chars = U+4E00..U+9FBF, U+3400..U+4DBF, U+20000..U+2A6DF, U+F900..U+FAFF,U+2F800..U+2FA1F, U+2E80..U+2EFF, U+2F00..U+2FDF, U+3100..U+312F, U+31A0..U+31BF,U+3040..U+309F, U+30A0..U+30FF, U+31F0..U+31FF, U+AC00..U+D7AF, U+1100..U+11FF,U+3130..U+318F, U+A000..U+A48F, U+A490..U+A4CF
}


index testrt
{
        type                    = rt
        rt_mem_limit            = 32M

        path                    = /opt/server/sphinx/var/data/testrt
        charset_type            = utf-8

        rt_field                = email
        rt_field                = url
	rt_field                = nickname
        rt_attr_uint            = is_access
}


indexer
{
	mem_limit		= 32M
}


searchd
{
	listen			= 9312
	log			= /opt/server/sphinx/var/log/searchd.log
	query_log		= /opt/server/sphinx/var/log/query.log
	read_timeout		= 5
	max_children		= 30
	pid_file		= /opt/server/sphinx/var/log/searchd.pid
	max_matches		= 1000
	seamless_rotate		= 1
	preopen_indexes		= 1
	unlink_old		= 1
	workers			= threads # for RT to work
	binlog_path		= /opt/server/sphinx/var/data
}

  

5.2 实时索引

策略:

在插入Myslq数据的同时,将需要索引的数据,同时往sphinx 虚拟Mysql数据库写入一份,实时索引

应用场景:

对实施搜索要求非常要的应用,一般不推荐此方式,尚且不稳定

方案:

编写相关PHP类,在Myslq插入数据的类中,同时插入索引数据,对前端使用者无需关心逻辑

详情配置

/opt/server/sphinx/etc/sphinx.conf

#
# 实时索引方案
#

source src1
{
	type					= mysql

	sql_host				= 127.0.0.1
	sql_user				= root
	sql_pass				= 123456
	sql_db					= test
	sql_port				= 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_query第一列id需为整数
                                                              #title、content作为字符串/文本字段,被全文索引
    sql_attr_uint            = group_id           #从SQL读取到的值必须为整数
    sql_attr_timestamp        = date_added #从SQL读取到的值必须为整数,作为时间属性
    sql_query_info            = SELECT * FROM documents WHERE id=$id #命令行查询时,从数据库读取原始数据信息
}


index test1
{
	source			= src1
	path			= /opt/server/sphinx/var/data/test1
	docinfo			= extern
	mlock			= 0
	stopwords		=
	min_prefix_len	= 0
	min_infix_len	= 0
	min_word_len	= 1
	charset_type	= utf-8
	charset_table = U+FF10..U+FF19->0..9, 0..9, U+FF41..U+FF5A->a..z, U+FF21..U+FF3A->a..z,A..Z->a..z, a..z, U+0149, U+017F, U+0138, U+00DF, U+00FF, U+00C0..U+00D6->U+00E0..U+00F6,U+00E0..U+00F6, U+00D8..U+00DE->U+00F8..U+00FE, U+00F8..U+00FE, U+0100->U+0101, U+0101,U+0102->U+0103, U+0103, U+0104->U+0105, U+0105, U+0106->U+0107, U+0107, U+0108->U+0109,U+0109, U+010A->U+010B, U+010B, U+010C->U+010D, U+010D, U+010E->U+010F, U+010F,U+0110->U+0111, U+0111, U+0112->U+0113, U+0113, U+0114->U+0115, U+0115, U+0116->U+0117,U+0117, U+0118->U+0119, U+0119, U+011A->U+011B, U+011B, U+011C->U+011D, U+011D,U+011E->U+011F, U+011F, U+0130->U+0131, U+0131, U+0132->U+0133, U+0133, U+0134->U+0135,U+0135, U+0136->U+0137, U+0137, U+0139->U+013A, U+013A, U+013B->U+013C, U+013C,U+013D->U+013E, U+013E, U+013F->U+0140, U+0140, U+0141->U+0142, U+0142, U+0143->U+0144,U+0144, U+0145->U+0146, U+0146, U+0147->U+0148, U+0148, U+014A->U+014B, U+014B,U+014C->U+014D, U+014D, U+014E->U+014F, U+014F, U+0150->U+0151, U+0151, U+0152->U+0153,U+0153, U+0154->U+0155, U+0155, U+0156->U+0157, U+0157, U+0158->U+0159, U+0159,U+015A->U+015B, U+015B, U+015C->U+015D, U+015D, U+015E->U+015F, U+015F, U+0160->U+0161,U+0161, U+0162->U+0163, U+0163, U+0164->U+0165, U+0165, U+0166->U+0167, U+0167,U+0168->U+0169, U+0169, U+016A->U+016B, U+016B, U+016C->U+016D, U+016D, U+016E->U+016F,U+016F, U+0170->U+0171, U+0171, U+0172->U+0173, U+0173, U+0174->U+0175, U+0175,U+0176->U+0177, U+0177, U+0178->U+00FF, U+00FF, U+0179->U+017A, U+017A, U+017B->U+017C,U+017C, U+017D->U+017E, U+017E, U+0410..U+042F->U+0430..U+044F, U+0430..U+044F,U+05D0..U+05EA, U+0531..U+0556->U+0561..U+0586, U+0561..U+0587, U+0621..U+063A, U+01B9,U+01BF, U+0640..U+064A, U+0660..U+0669, U+066E, U+066F, U+0671..U+06D3, U+06F0..U+06FF,U+0904..U+0939, U+0958..U+095F, U+0960..U+0963, U+0966..U+096F, U+097B..U+097F,U+0985..U+09B9, U+09CE, U+09DC..U+09E3, U+09E6..U+09EF, U+0A05..U+0A39, U+0A59..U+0A5E,U+0A66..U+0A6F, U+0A85..U+0AB9, U+0AE0..U+0AE3, U+0AE6..U+0AEF, U+0B05..U+0B39,U+0B5C..U+0B61, U+0B66..U+0B6F, U+0B71, U+0B85..U+0BB9, U+0BE6..U+0BF2, U+0C05..U+0C39,U+0C66..U+0C6F, U+0C85..U+0CB9, U+0CDE..U+0CE3, U+0CE6..U+0CEF, U+0D05..U+0D39, U+0D60,U+0D61, U+0D66..U+0D6F, U+0D85..U+0DC6, U+1900..U+1938, U+1946..U+194F, U+A800..U+A805,U+A807..U+A822, U+0386->U+03B1, U+03AC->U+03B1, U+0388->U+03B5, U+03AD->U+03B5,U+0389->U+03B7, U+03AE->U+03B7, U+038A->U+03B9, U+0390->U+03B9, U+03AA->U+03B9,U+03AF->U+03B9, U+03CA->U+03B9, U+038C->U+03BF, U+03CC->U+03BF, U+038E->U+03C5,U+03AB->U+03C5, U+03B0->U+03C5, U+03CB->U+03C5, U+03CD->U+03C5, U+038F->U+03C9,U+03CE->U+03C9, U+03C2->U+03C3, U+0391..U+03A1->U+03B1..U+03C1,U+03A3..U+03A9->U+03C3..U+03C9, U+03B1..U+03C1, U+03C3..U+03C9, U+0E01..U+0E2E,U+0E30..U+0E3A, U+0E40..U+0E45, U+0E47, U+0E50..U+0E59, U+A000..U+A48F, U+4E00..U+9FBF,U+3400..U+4DBF, U+20000..U+2A6DF, U+F900..U+FAFF, U+2F800..U+2FA1F, U+2E80..U+2EFF,U+2F00..U+2FDF, U+3100..U+312F, U+31A0..U+31BF, U+3040..U+309F, U+30A0..U+30FF,U+31F0..U+31FF, U+AC00..U+D7AF, U+1100..U+11FF, U+3130..U+318F, U+A000..U+A48F,U+A490..U+A4CF
	ngram_len = 1 
	ngram_chars = U+4E00..U+9FBF, U+3400..U+4DBF, U+20000..U+2A6DF, U+F900..U+FAFF,U+2F800..U+2FA1F, U+2E80..U+2EFF, U+2F00..U+2FDF, U+3100..U+312F, U+31A0..U+31BF,U+3040..U+309F, U+30A0..U+30FF, U+31F0..U+31FF, U+AC00..U+D7AF, U+1100..U+11FF,U+3130..U+318F, U+A000..U+A48F, U+A490..U+A4CF
}


index testrt
{
	type			= rt
	rt_mem_limit		= 32M
	path			= /opt/server/sphinx/var/data/testrt

	docinfo            = extern
	mlock            = 0
	morphology        = none
	min_word_len        = 1
	html_strip                = 0

	charset_type	= utf-8
	charset_table = U+FF10..U+FF19->0..9, 0..9, U+FF41..U+FF5A->a..z, U+FF21..U+FF3A->a..z,A..Z->a..z, a..z, U+0149, U+017F, U+0138, U+00DF, U+00FF, U+00C0..U+00D6->U+00E0..U+00F6,U+00E0..U+00F6, U+00D8..U+00DE->U+00F8..U+00FE, U+00F8..U+00FE, U+0100->U+0101, U+0101,U+0102->U+0103, U+0103, U+0104->U+0105, U+0105, U+0106->U+0107, U+0107, U+0108->U+0109,U+0109, U+010A->U+010B, U+010B, U+010C->U+010D, U+010D, U+010E->U+010F, U+010F,U+0110->U+0111, U+0111, U+0112->U+0113, U+0113, U+0114->U+0115, U+0115, U+0116->U+0117,U+0117, U+0118->U+0119, U+0119, U+011A->U+011B, U+011B, U+011C->U+011D, U+011D,U+011E->U+011F, U+011F, U+0130->U+0131, U+0131, U+0132->U+0133, U+0133, U+0134->U+0135,U+0135, U+0136->U+0137, U+0137, U+0139->U+013A, U+013A, U+013B->U+013C, U+013C,U+013D->U+013E, U+013E, U+013F->U+0140, U+0140, U+0141->U+0142, U+0142, U+0143->U+0144,U+0144, U+0145->U+0146, U+0146, U+0147->U+0148, U+0148, U+014A->U+014B, U+014B,U+014C->U+014D, U+014D, U+014E->U+014F, U+014F, U+0150->U+0151, U+0151, U+0152->U+0153,U+0153, U+0154->U+0155, U+0155, U+0156->U+0157, U+0157, U+0158->U+0159, U+0159,U+015A->U+015B, U+015B, U+015C->U+015D, U+015D, U+015E->U+015F, U+015F, U+0160->U+0161,U+0161, U+0162->U+0163, U+0163, U+0164->U+0165, U+0165, U+0166->U+0167, U+0167,U+0168->U+0169, U+0169, U+016A->U+016B, U+016B, U+016C->U+016D, U+016D, U+016E->U+016F,U+016F, U+0170->U+0171, U+0171, U+0172->U+0173, U+0173, U+0174->U+0175, U+0175,U+0176->U+0177, U+0177, U+0178->U+00FF, U+00FF, U+0179->U+017A, U+017A, U+017B->U+017C,U+017C, U+017D->U+017E, U+017E, U+0410..U+042F->U+0430..U+044F, U+0430..U+044F,U+05D0..U+05EA, U+0531..U+0556->U+0561..U+0586, U+0561..U+0587, U+0621..U+063A, U+01B9,U+01BF, U+0640..U+064A, U+0660..U+0669, U+066E, U+066F, U+0671..U+06D3, U+06F0..U+06FF,U+0904..U+0939, U+0958..U+095F, U+0960..U+0963, U+0966..U+096F, U+097B..U+097F,U+0985..U+09B9, U+09CE, U+09DC..U+09E3, U+09E6..U+09EF, U+0A05..U+0A39, U+0A59..U+0A5E,U+0A66..U+0A6F, U+0A85..U+0AB9, U+0AE0..U+0AE3, U+0AE6..U+0AEF, U+0B05..U+0B39,U+0B5C..U+0B61, U+0B66..U+0B6F, U+0B71, U+0B85..U+0BB9, U+0BE6..U+0BF2, U+0C05..U+0C39,U+0C66..U+0C6F, U+0C85..U+0CB9, U+0CDE..U+0CE3, U+0CE6..U+0CEF, U+0D05..U+0D39, U+0D60,U+0D61, U+0D66..U+0D6F, U+0D85..U+0DC6, U+1900..U+1938, U+1946..U+194F, U+A800..U+A805,U+A807..U+A822, U+0386->U+03B1, U+03AC->U+03B1, U+0388->U+03B5, U+03AD->U+03B5,U+0389->U+03B7, U+03AE->U+03B7, U+038A->U+03B9, U+0390->U+03B9, U+03AA->U+03B9,U+03AF->U+03B9, U+03CA->U+03B9, U+038C->U+03BF, U+03CC->U+03BF, U+038E->U+03C5,U+03AB->U+03C5, U+03B0->U+03C5, U+03CB->U+03C5, U+03CD->U+03C5, U+038F->U+03C9,U+03CE->U+03C9, U+03C2->U+03C3, U+0391..U+03A1->U+03B1..U+03C1,U+03A3..U+03A9->U+03C3..U+03C9, U+03B1..U+03C1, U+03C3..U+03C9, U+0E01..U+0E2E,U+0E30..U+0E3A, U+0E40..U+0E45, U+0E47, U+0E50..U+0E59, U+A000..U+A48F, U+4E00..U+9FBF,U+3400..U+4DBF, U+20000..U+2A6DF, U+F900..U+FAFF, U+2F800..U+2FA1F, U+2E80..U+2EFF,U+2F00..U+2FDF, U+3100..U+312F, U+31A0..U+31BF, U+3040..U+309F, U+30A0..U+30FF,U+31F0..U+31FF, U+AC00..U+D7AF, U+1100..U+11FF, U+3130..U+318F, U+A000..U+A48F,U+A490..U+A4CF
	ngram_len = 1 
	ngram_chars = U+4E00..U+9FBF, U+3400..U+4DBF, U+20000..U+2A6DF, U+F900..U+FAFF,U+2F800..U+2FA1F, U+2E80..U+2EFF, U+2F00..U+2FDF, U+3100..U+312F, U+31A0..U+31BF,U+3040..U+309F, U+30A0..U+30FF, U+31F0..U+31FF, U+AC00..U+D7AF, U+1100..U+11FF,U+3130..U+318F, U+A000..U+A48F, U+A490..U+A4CF

    #全文索引字段
    rt_field                  = title               #全文索引字段
    rt_field                  = content         #全文索引字段

    #属性字段
    rt_attr_uint            = groupid
    rt_attr_bigint         = biguid
    rt_attr_float           = score
    rt_attr_timestamp  = date_added

    #存储内容字段
    rt_attr_string          = author          #存储author的内容

    #已设置全文索引,并需要同时存储内容的字段
    rt_attr_string          = title              #同时存储title的内容
    rt_attr_string          = content        #同时存储content的内容
}


indexer
{
	mem_limit		= 32M
}


searchd
{
	listen			= 9312
	listen			= 9306:mysql41
	log			= /opt/server/sphinx/var/log/searchd.log
	query_log		= /opt/server/sphinx/var/log/query.log
	read_timeout		= 5
	max_children		= 30
	pid_file		= /opt/server/sphinx/var/log/searchd.pid
	max_matches		= 1000
	seamless_rotate		= 1
	preopen_indexes		= 1
	unlink_old		= 1
	workers			= threads # for RT to work
	binlog_path		= /opt/server/sphinx/var/data
}

  

/opt/server/mysql/bin/mysql -h127.0.0.1 -P9306
show tables;
desc testrt;

插入数据
INSERT INTO testrt VALUES ( 1, ‘first record’, ‘test one’, 123 );
INSERT INTO testrt VALUES ( 2, ‘second record’, ‘test two’, 234 );
INSERT INTO testrt VALUES ( 4, ‘没有什么不得了’, ‘真的么,就不告诉test’, 6789 );

检索
SELECT * FROM testrt WHERE MATCH(‘test’);
SELECT * FROM testrt WHERE MATCH(‘真的么’);

5.3 多个表独立索引方案

场景:如有用户搜索、商品搜索等多个索引需求

策略:

配置一个多索引方案,每个表单独建立索引

前端根据不同类型选择不同的查询索引;全部,即选择所有索引

Sphinx 之 Coreseek、Sphinx-for-chinaese、Sphinx+Scws 评测

标签:

原文地址:http://www.cnblogs.com/sandea/p/5572702.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!