sphinx简单配置

来源:互联网 发布:反杀电影 知乎 编辑:程序博客网 时间:2024/05/21 12:15
中文手册可以在这里获得,感谢译者的辛勤工作。二、Sphinx在windows上的安装1.直接在http://www.sphinxsearch.com/downloads.html找到最新的windows版本,我这里下的是Win32 release binaries with MySQL support,下载后解压在D:sphinx目录下;2.在D:sphinx下新建一个data目录用来存放索引文件,一个log目录方日志文件,复制D:sphinxsphinx.conf.in到D:sphinxbinsphinx.conf(注意修改文件名);3.修改D:sphinxbinsphinx.conf,我这里列出需要修改的几个:type        = mysql # 数据源,我这里是mysqlsql_host    = localhost # 数据库服务器sql_user    = root # 数据库用户名sql_pass    = '' # 数据库密码sql_db      = test # 数据库sql_port    = 3306 # 数据库端口sql_query_pre   = SET NAMES utf8 # 去掉此行前面的注释,如果你的数据库是uft8编码的index test1{# 放索引的目录 path   = D:/sphinx/data/# 编码 charset_type  = utf-8 #  指定utf-8的编码表 charset_table  = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F # 简单分词,只支持0和1,如果要搜索中文,请指定为1 ngram_len    = 1# 需要分词的字符,如果要搜索中文,去掉前面的注释 ngram_chars   = U+3000..U+2FA1F}# index test1stemmed : test1# { # path   = @CONFDIR@/data/test1stemmed # morphology  = stem_en# }# 如果没有分布式索引,注释掉下面的内容# index dist1# { # 'distributed' index type MUST be specified # type    = distributed # local index to be searched # there can be many local indexes configured # local    = test1 # local    = test1stemmed # remote agent # multiple remote agents may be specified # syntax is 'hostname:port:index1,[index2[,...]] # agent    = localhost:3313:remote1 # agent    = localhost:3314:remote2,remote3 # remote agent connection timeout, milliseconds # optional, default is 1000 ms, ie. 1 sec # agent_connect_timeout = 1000 # remote agent query timeout, milliseconds # optional, default is 3000 ms, ie. 3 sec # agent_query_timeout  = 3000# }# 搜索服务需要修改的部分searchd{ # 日志 log     = D:/sphinx/log/searchd.log # PID file, searchd process ID file name pid_file   = D:/sphinx/log/searchd.pid # windows下启动searchd服务一定要注释掉这个 # seamless_rotate  = 1}4.导入测试数据C:Program FilesMySQLMySQL Server 5.0bin>mysql -uroot test<d:/sphinx/example.sql5.建立索引D:sphinxbin>indexer.exe –allSphinx 0.9.8-release (r1533)Copyright (c) 2001-2008, Andrew Aksyonoffusing config file ‘./sphinx.conf’…indexing index ‘test1′…collected 4 docs, 0.0 MBsorted 0.0 Mhits, 100.0% donetotal 4 docs, 193 bytestotal 0.101 sec, 1916.30 bytes/sec, 39.72 docs/secD:sphinxbin>6.搜索’test’试试D:sphinxbin>search.exe testSphinx 0.9.8-release (r1533)Copyright (c) 2001-2008, Andrew Aksyonoffusing config file ‘./sphinx.conf’…index ‘test1′: query ‘test ‘: returned 3 matches of 3 total in 0.000 secdisplaying matches:1. document=1, weight=2, group_id=1, date_added=Wed Nov 26 14:58:59 2008        id=1        group_id=1        group_id2=5        date_added=2008-11-26 14:58:59        title=test one        content=this is my test document number one. also checking search within phrases.2. document=2, weight=2, group_id=1, date_added=Wed Nov 26 14:58:59 2008        id=2        group_id=1        group_id2=6        date_added=2008-11-26 14:58:59        title=test two        content=this is my test document number two3. document=4, weight=1, group_id=2, date_added=Wed Nov 26 14:58:59 2008        id=4        group_id=2        group_id2=8        date_added=2008-11-26 14:58:59        title=doc number four        content=this is to test groupswords:1. ‘test’: 3 documents, 5 hitsD:sphinxbin>都所出来了吧。6.测试中文搜索修改test数据库中documents数据表,UPDATE `test`.`documents` SET `title` = ‘测试中文’, `content` = ‘this is my test document number two,应该搜的到吧’ WHERE `documents`.`id` = 2;重建索引:D:sphinxbin>indexer.exe –all搜索’中文’试试:D:sphinxbin>search.exe 中文Sphinx 0.9.8-release (r1533)Copyright (c) 2001-2008, Andrew Aksyonoffusing config file ‘./sphinx.conf’…index ‘test1′: query ‘中文 ‘: returned 0 matches of 0 total in 0.000 secwords:D:sphinxbin>貌似没有搜到,这是因为windows命令行中的编码是gbk,当然搜不出来。我们可以用程序试试,在D:sphinxapi下新建一个foo.php的文件,注意utf-8编码<?phprequire ‘sphinxapi.php’;$s = new SphinxClient();$s->SetServer(‘localhost’,3312);$result = $s->Query(‘中文’);var_dump($result);?>启动Sphinx searchd服务D:sphinxbin>searchd.exeSphinx 0.9.8-release (r1533)Copyright (c) 2001-2008, Andrew AksyonoffWARNING: forcing –console mode on Windowsusing config file ‘./sphinx.conf’…creating server socket on 0.0.0.0:3312accepting connections执行PHP查询:php d:/sphinx/api/foo.php结果是不是出来?剩下的工作就是去看手册,慢慢摸索高阶的配置。

0 0