免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2491 | 回复: 1
打印 上一主题 下一主题

全文索引sphinx+mysql的配置简单事例 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-12-27 16:14 |只看该作者 |倒序浏览
sphinx在win上的安装

下载MYSQL5.0.27以上版本 http://dev.mysql.com

从Sphinx官网上http://www.sphinxsearch.com/downloads.html下载sphinx-0.9.8-win32.zip 和mysql-5.0.45-sphinxse-0.9.8-win32.zip

先将mysql服务停掉  解压mysql-5.0.45-sphinxse-0.9.8-win32.zip  将bin和share覆盖掉 mysql目录中的bin和share

解压sphinx-0.9.8-win32.zip到独立的目录,如:d:/www/sphinx/中

在test数据库中新建测试表

CREATE TABLE `documents` (
  `id` int(11) NOT NULL auto_increment,
  `group_id` int(11) NOT NULL,
  `group_id2` int(11) NOT NULL,
  `date_added` datetime NOT NULL,
  `title` varchar(255) NOT NULL,
  `content` text NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5;

INSERT INTO `documents` VALUES ('1', '1', '5', '2008-09-13 21:37:47', 'test one', 'this is my test document number one. also checking search within phrases.');
INSERT INTO `documents` VALUES ('2', '1', '6', '2008-09-13 21:37:47', 'test two', 'this is my test document number two');
INSERT INTO `documents` VALUES ('3', '2', '7', '2008-09-13 21:37:47', 'another doc', 'this is another group');
INSERT INTO `documents` VALUES ('4', '2', '8', '2008-09-13 21:37:47', 'doc number four', 'this is to test groups');

配置sphinx-doc.conf文件
  将sphinx下的sphinx-min.conf复制一份改名为sphinx-doc.conf

#
# Minimal Sphinx configuration sample (clean, simple, functional)
#
source documents
{
        type                                        = mysql
        sql_host                                = localhost
        sql_user                                = test
        sql_pass                                = test
        sql_db                                        = test
        sql_port                                = 3306        # optional, default is 3306

        sql_query                                = \
                SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content \
                FROM documents

        sql_attr_uint                        = group_id
        sql_attr_timestamp                = date_added

        sql_query_info                        = SELECT * FROM documents WHERE id=$id
}
index documents
{
        source                                        = documents
        path                                        = D:/www/sphinx/data/doc

           morphology              = extern
            enable_star = 1

            min_word_len            = 3
            min_prefix_len          = 0
            min_infix_len           = 3


}
indexer
{
        mem_limit                                = 32M
}
searchd
{
        port                                        = 3312
        log                                                = D:/www/sphinx/data/log/searchd-doc.log
        query_log                                = D:/www/sphinx/data/log/query-doc.log
        read_timeout                        = 5
        max_children                        = 30
        pid_file                                = D:/www/sphinx/data/log/searchd-doc.pid
        max_matches                                = 1000
        seamless_rotate                        = 0
        preopen_indexes                        = 0
        unlink_old                                = 1
}

启动mysql数据库 net start mysql


生成数据索引或重建索引

d:/www/sphinx/bin/indexer.exe --config d:/www/sphinx/bin/sphinx-doc.conf documents

运行检索守护进程 searchd.exe

d:/www/sphinx/bin/searchd.exe --config d:/www/sphinx/bin/sphinx-doc.conf

此时sphinx已经正常运行了,可以通过netstat -an 查看是否3312端口是否处如监听状态


现在来用sphinx自带的工具search.exe来测试一样


索引关键字: this is m

D:\www\sphinx\bin>search.exe -c d:/www/sphinx/bin/sphinx-doc.conf this is m
Sphinx 0.9.8-release (r1371)
Copyright (c) 2001-2008, Andrew Aksyonoff

using config file 'd:/www/sphinx/bin/sphinx-doc.conf'...
WARNING: index 'documents': invalid morphology option 'extern' - IGNORED
index 'documents': query 'this is m ': returned 4 matches of 4 total in 0.000 s
c

displaying matches:
1. document=1, weight=1, group_id=1, date_added=Sat Sep 13 21:37:47 2008
        id=1
        group_id=1
        group_id2=5
        date_added=2008-09-13 21:37:47
        title=test one
        content=this is my test document number one. also checking search withi
phrases.
2. document=2, weight=1, group_id=1, date_added=Sat Sep 13 21:37:47 2008
        id=2
        group_id=1
        group_id2=6
        date_added=2008-09-13 21:37:47
        title=test two
        content=this is my test document number two
3. document=3, weight=1, group_id=2, date_added=Sat Sep 13 21:37:47 2008
        id=3
        group_id=2
        group_id2=7
        date_added=2008-09-13 21:37:47
        title=another doc
        content=this is another group
4. document=4, weight=1, group_id=2, date_added=Sat Sep 13 21:37:47 2008
        id=4
        group_id=2
        group_id2=8
        date_added=2008-09-13 21:37:47
        title=doc number four
        content=this is to test groups

words:
1. 'this': 4 documents, 4 hits

索引关键字: this is another group
D:\www\sphinx\bin>search.exe -c d:/www/sphinx/bin/sphinx-doc.conf this is another group
Sphinx 0.9.8-release (r1371)
Copyright (c) 2001-2008, Andrew Aksyonoff

using config file 'd:/www/sphinx/bin/sphinx-doc.conf'...
WARNING: index 'documents': invalid morphology option 'extern' - IGNORED
index 'documents': query 'this is another group ': returned 1 matches of 1 total
in 0.000 sec

displaying matches:
1. document=3, weight=4, group_id=2, date_added=Sat Sep 13 21:37:47 2008
        id=3
        group_id=2
        group_id2=7
        date_added=2008-09-13 21:37:47
        title=another doc
        content=this is another group

words:
1. 'this': 4 documents, 4 hits
2. 'another': 1 documents, 2 hits
3. 'group': 1 documents, 1 hits

到此sphinx在win上算正常运行了,sphinx-doc.conf文件配置比较灵活,根据你需要索引的数据库进行灵活配置来达到你需要的效果

如果配置过程中出现运行参数配置问题可以查看 doc/sphinx.html文件,里面对各种参数都要详细的说明

论坛徽章:
0
2 [报告]
发表于 2010-12-27 16:15 |只看该作者
在本地用php文件搜索的例子,很简单明了,进一步的功能完善待续.....

由于安装的是直接从官方下的包,所有目前还不支持中文。继续探索中....

<?PHP
include_once('../bbs/include/common.inc.php');
require ( "sphinxapi.php" );
$cl = new SphinxClient ();
$cl->SetServer ( 'localhost', 3312 );
$cl->SetWeights ( array ( 100, 1 ) );
$cl->SetMatchMode ( SPH_MATCH_EXTENDED );

$cl->SetRankingMode ( SPH_RANK_PROXIMITY_BM25 );
$cl->SetArrayResult ( true );

$res = $cl->Query ( 'test', 'documents' );  //test为关键字  documents为索引名

//var_dump($res);

//echo '<br />';

echo '总共查询到'.$res['total_found'].'条记录. 查询花费时间 '.$res['time'].'s<br>';

foreach($res['matches'] as $v){
$_query = $db->query( "select * from test.documents where id = $v[id]" ) ;
while($row = $db->fetch_array( $_query ) ){
  echo $row['id'].':'.$row['title'].':'.$row['content'].'<br>';
}
//echo $v['id'].':'.$v['title'].':'.$v['content'].'<br />';
}

/**/
?>
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP