- 论坛徽章:
- 0
|
【转】sphinx在windows下的安装与测试
.转自:http://www.cnblogs.com/ainiaa/archive/2010/12/21/1912459.html
1.直接在http://www.sphinxsearch.com/downloads.html找到最新的windows版本,我这里下的是Win32 release binaries with MySQL support,下载后解压在D:\sphinx目录下;
2.在D:\sphinx\下新建一个data目录用来存放索引文件,一个log目录方日志文件,复制D:\sphinx\sphinx.conf.in到D:\sphinx\bin\sphinx.conf(注意修改文件名);
3.修改D:\sphinx\bin\sphinx.conf,我这里列出需要修改的几个:
Php代码- 1.type = mysql # 数据源,我这里是mysql
- 2.sql_host = localhost # 数据库服务器
- 3.sql_user = root # 数据库用户名
- 4.sql_pass = '' # 数据库密码
- 5.sql_db = test # 数据库
- 6.sql_port = 3306 # 数据库端口
- 7.sql_query_pre = SET NAMES utf8 # 去掉此行前面的注释,如果你的数据库是uft8编码的
- 8.index test1
- 9.{
- 10.# 放索引的目录
- 11. path = D:/sphinx/data/
- 12.# 编码
- 13. charset_type = utf-8
- 14. # 指定utf-8的编码表
- 15. charset_table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F
- 16. # 简单分词,只支持0和1,如果要搜索中文,请指定为1
- 17. ngram_len = 1
- 18.# 需要分词的字符,如果要搜索中文,去掉前面的注释
- 19. ngram_chars = U+3000..U+2FA1F
- 20.}
- 21.# index test1stemmed : test1
- 22.# {
- 23. # path = @CONFDIR@/data/test1stemmed
- 24. # morphology = stem_en
- 25.# }
- 26.
- 27.# 如果没有分布式索引,注释掉下面的内容
- 28.# index dist1
- 29.# {
- 30. # 'distributed' index type MUST be specified
- 31. # type = distributed
- 32. # local index to be searched
- 33. # there can be many local indexes configured
- 34. # local = test1
- 35. # local = test1stemmed
- 36. # remote agent
- 37. # multiple remote agents may be specified
- 38. # syntax is 'hostname:port:index1,[index2[,...]]
- 39. # agent = localhost:3313:remote1
- 40. # agent = localhost:3314:remote2,remote3
- 41. # remote agent connection timeout, milliseconds
- 42. # optional, default is 1000 ms, ie. 1 sec
- 43. # agent_connect_timeout = 1000
- 44. # remote agent query timeout, milliseconds
- 45. # optional, default is 3000 ms, ie. 3 sec
- 46. # agent_query_timeout = 3000
- 47.# }
- 48.# 搜索服务需要修改的部分
- 49.searchd
- 50.{
- 51. # 日志
- 52. log = D:/sphinx/log/searchd.log
- 53. # PID file, searchd process ID file name
- 54. pid_file = D:/sphinx/log/searchd.pid
- 55. # windows下启动searchd服务一定要注释掉这个
- 56. # seamless_rotate = 1
- 57.}
- type = mysql # 数据源,我这里是mysql
- sql_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 Files\MySQL\MySQL Server 5.0\bin>mysql -uroot test<d:/sphinx/example.sql
复制代码 5.建立索引
Php代码- 1.D:\sphinx\bin>indexer.exe –all
- 2.Sphinx 0.9.8-release (r1533)
- 3.Copyright (c) 2001-2008, Andrew Aksyonoff
- 4.using config file ‘./sphinx.conf’…
- 5.indexing index ‘test1′…
- 6.collected 4 docs, 0.0 MB
- 7.sorted 0.0 Mhits, 100.0% done
- 8.total 4 docs, 193 bytes
- 9.total 0.101 sec, 1916.30 bytes/sec, 39.72 docs/sec
- 10.D:\sphinx\bin>
- D:\sphinx\bin>indexer.exe –all
- Sphinx 0.9.8-release (r1533)
- Copyright (c) 2001-2008, Andrew Aksyonoff
- using config file ‘./sphinx.conf’…
- indexing index ‘test1′…
- collected 4 docs, 0.0 MB
- sorted 0.0 Mhits, 100.0% done
- total 4 docs, 193 bytes
- total 0.101 sec, 1916.30 bytes/sec, 39.72 docs/sec
- D:\sphinx\bin>
复制代码 6.搜索’test’试试
Php代码- 1.D:\sphinx\bin>search.exe test
- 2.Sphinx 0.9.8-release (r1533)
- 3.Copyright (c) 2001-2008, Andrew Aksyonoff
- 4.using config file ‘./sphinx.conf’…
- 5.index ‘test1′: query ‘test ‘: returned 3 matches of 3 total in 0.000 sec
- 6.displaying matches:
- 7.1. document=1, weight=2, group_id=1, date_added=Wed Nov 26 14:58:59 2008
- 8. id=1
- 9. group_id=1
- 10. group_id2=5
- 11. date_added=2008-11-26 14:58:59
- 12. title=test one
- 13. content=this is my test document number one. also checking search within
- 14. phrases.
- 15.2. document=2, weight=2, group_id=1, date_added=Wed Nov 26 14:58:59 2008
- 16. id=2
- 17. group_id=1
- 18. group_id2=6
- 19. date_added=2008-11-26 14:58:59
- 20. title=test two
- 21. content=this is my test document number two
- 22.3. document=4, weight=1, group_id=2, date_added=Wed Nov 26 14:58:59 2008
- 23. id=4
- 24. group_id=2
- 25. group_id2=8
- 26. date_added=2008-11-26 14:58:59
- 27. title=doc number four
- 28. content=this is to test groups
- 29.words:
- 30.1. ‘test’: 3 documents, 5 hits
- 31.D:\sphinx\bin>
- D:\sphinx\bin>search.exe test
- Sphinx 0.9.8-release (r1533)
- Copyright (c) 2001-2008, Andrew Aksyonoff
- using config file ‘./sphinx.conf’…
- index ‘test1′: query ‘test ‘: returned 3 matches of 3 total in 0.000 sec
- displaying 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 two
- 3. 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 groups
- words:
- 1. ‘test’: 3 documents, 5 hits
- D:\sphinx\bin>
复制代码 6.测试中文搜索
修改test数据库中documents数据表,
Php代码- 1.UPDATE `test`.`documents` SET `title` = ‘测试中文’, `content` = ‘this is my test document number two,应该搜的到吧’ WHERE `documents`.`id` = 2;
- UPDATE `test`.`documents` SET `title` = ‘测试中文’, `content` = ‘this is my test document number two,应该搜的到吧’ WHERE `documents`.`id` = 2;
复制代码 重建索引:
D:\sphinx\bin>indexer.exe –all
搜索’中文’试试:
Php代码- 1.D:\sphinx\bin>search.exe 中文
- 2.Sphinx 0.9.8-release (r1533)
- 3.Copyright (c) 2001-2008, Andrew Aksyonoff
- 4.using config file ‘./sphinx.conf’…
- 5.index ‘test1′: query ‘中文 ‘: returned 0 matches of 0 total in 0.000 sec
- 6.words:
- 7.D:\sphinx\bin>
- D:\sphinx\bin>search.exe 中文
- Sphinx 0.9.8-release (r1533)
- Copyright (c) 2001-2008, Andrew Aksyonoff
- using config file ‘./sphinx.conf’…
- index ‘test1′: query ‘中文 ‘: returned 0 matches of 0 total in 0.000 sec
- words:
- D:\sphinx\bin>
复制代码 貌似没有搜到,这是因为windows命令行中的编码是gbk,当然搜不出来。我们可以用程序试试,在D:\sphinx\api下新建一个foo.php的文件,注意utf-8编码
Php代码- 1.<?php
- 2.require ‘sphinxapi.php’;
- 3.$s = new SphinxClient();
- 4.$s->SetServer(‘localhost’,3312);
- 5.$result = $s->Query(‘中文’);
- 6.var_dump($result);
- 7.?>
- <?php
- require ‘sphinxapi.php’;
- $s = new SphinxClient();
- $s->SetServer(‘localhost’,3312);
- $result = $s->Query(‘中文’);
- var_dump($result);
- ?>
复制代码 启动Sphinx searchd服务
Php代码- 1.D:\sphinx\bin>searchd.exe
- 2.Sphinx 0.9.8-release (r1533)
- 3.Copyright (c) 2001-2008, Andrew Aksyonoff
- 4.WARNING: forcing –console mode on Windows
- 5.using config file ‘./sphinx.conf’…
- 6.creating server socket on 0.0.0.0:3312
- 7.accepting connections
- D:\sphinx\bin>searchd.exe
- Sphinx 0.9.8-release (r1533)
- Copyright (c) 2001-2008, Andrew Aksyonoff
- WARNING: forcing –console mode on Windows
- using config file ‘./sphinx.conf’…
- creating server socket on 0.0.0.0:3312
- accepting connections
复制代码 执行PHP查询:
Php代码- 1.php d:/sphinx/api/foo.php
复制代码 |
|