<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>
<channel>
	<title>超群.com的博客 &#187; Full Text Search</title>
	<atom:link href="http://www.fuchaoqun.com/tag/%e5%85%a8%e6%96%87%e6%a3%80%e7%b4%a2/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.fuchaoqun.com</link>
	<description></description>
	<lastBuildDate>Thu, 08 Sep 2011 15:08:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Sphinx在windows下安装使用[支持中文全文检索]</title>
		<link>http://www.fuchaoqun.com/2008/11/sphinx-on-windows-xp/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sphinx-on-windows-xp</link>
		<comments>http://www.fuchaoqun.com/2008/11/sphinx-on-windows-xp/#comments</comments>
		<pubDate>Wed, 26 Nov 2008 07:29:23 +0000</pubDate>
		<dc:creator>超群.com</dc:creator>
				<category><![CDATA[Full Text Search]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[默认分类]]></category>
		<category><![CDATA[chinese]]></category>
		<category><![CDATA[sphinx]]></category>
		<guid isPermaLink="false">http://chaoqun.17348.com/?p=106</guid>
		<description><![CDATA[本博客所有原创文章采用知识共享署名-非商业性使用-相同方式共享，转载请保留链接http://chaoqun.17348.com/2008/11/sphinx-on-windows-xp/ 前一阵子尝试使用了一下Sphinx，一个能够被各种语言(PHP/Python/Ruby/etc)方便调用的全文检索系统。网上的资料大多是在linux环境下的安装使用，当然，作为生产环境很有必要部署在*nix环境下，作为学习测试，还是windows环境比较方便些。 本文旨在提供一种便捷的方式让Sphinx在windows下安装配置以支持中文全文检索，配置部分在linux下通用。 一、关于Sphinx Sphinx 是一个在GPLv2 下发布的一个全文检索引擎，商业授权（例如, 嵌入到其他程序中）需要联系作者（Sphinxsearch.com）以获得商业授权。 一般而言，Sphinx是一个独立的搜索引擎，意图为其他应用提供高速、低空间占用、高结果相关度的全文搜索功能。Sphinx可以非常容易的与SQL数据库和脚本语言集成。 当前系统内置MySQL和PostgreSQL 数据库数据源的支持，也支持从标准输入读取特定格式的XML数据。通过修改源代码，用户可以自行增加新的数据源（例如：其他类型的DBMS的原生支持）。 搜索API支持PHP、Python、Perl、Rudy和Java，并且也可以用作MySQL存储引擎。搜索API非常简单，可以在若干个小时之内移植到新的语言上。 Sphinx特性： 高速的建立索引(在当代CPU上，峰值性能可达到10MB/秒); 高性能的搜索(在2–4GB的文本数据上，平均每次检索响应时间小于0.1秒); 可处理海量数据(目前已知可以处理超过100GB的文本数据,在单一CPU的系统上可处理100M文档); 提供了优秀的相关度算法，基于短语相似度和统计（BM25）的复合Ranking方法; 支持分布式搜索; 提供文件的摘录生成; 可作为MySQL的存储引擎提供搜索服务; 支持布尔、短语、词语相似度等多种检索模式; 文档支持多个全文检索字段(最大不超过32个); 文档支持多个额外的属性信息(例如：分组信息，时间戳等); 停止词查询; 支持单一字节编码和UTF-8编码; 原生的MySQL支持(同时支持MyISAM和InnoDB); 原生的PostgreSQL支持. 中文手册可以在这里获得，感谢译者的辛勤工作。 二、Sphinx在windows上的安装 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，我这里列出需要修改的几个： type        = mysql # 数据源，我这里是mysql sql_host    = localhost # 数据库服务器 sql_user    = root # 数据库用户名 sql_pass    = '' # 数据库密码 [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>本博客所有原创文章采用<a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"><span style="color: #356aa0;">知识共享署名-非商业性使用-相同方式共享</span></a>，转载请保留链接<a href="http://chaoqun.17348.com/2008/11/sphinx-on-windows-xp/" target="_blank">http://chaoqun.17348.com/2008/11/sphinx-on-windows-xp/</a></p></blockquote>
<p>前一阵子尝试使用了一下Sphinx，一个能够被各种语言(PHP/Python/Ruby/etc)方便调用的全文检索系统。网上的资料大多是在linux环境下的安装使用，当然，作为生产环境很有必要部署在*nix环境下，作为学习测试，还是windows环境比较方便些。</p>
<p>本文旨在提供一种便捷的方式让Sphinx在windows下安装配置以支持中文全文检索，配置部分在linux下通用。</p>
<p>一、关于Sphinx</p>
<p>Sphinx 是一个在GPLv2 下发布的一个全文检索引擎，商业授权（例如, 嵌入到其他程序中）需要联系作者（Sphinxsearch.com）以获得商业授权。</p>
<p>一般而言，Sphinx是一个独立的搜索引擎，意图为其他应用提供高速、低空间占用、高结果相关度的全文搜索功能。Sphinx可以非常容易的与SQL数据库和脚本语言集成。</p>
<p>当前系统内置MySQL和PostgreSQL 数据库数据源的支持，也支持从标准输入读取特定格式的XML数据。通过修改源代码，用户可以自行增加新的数据源（例如：其他类型的DBMS的原生支持）。</p>
<p>搜索API支持PHP、Python、Perl、Rudy和Java，并且也可以用作MySQL存储引擎。搜索API非常简单，可以在若干个小时之内移植到新的语言上。</p>
<p>Sphinx特性：</p>
<ul>
<li>高速的建立索引(在当代CPU上，峰值性能可达到10MB/秒);</li>
<li>高性能的搜索(在2–4GB的文本数据上，平均每次检索响应时间小于0.1秒);</li>
<li>可处理海量数据(目前已知可以处理超过100GB的文本数据,在单一CPU的系统上可处理100M文档);</li>
<li>提供了优秀的相关度算法，基于短语相似度和统计（BM25）的复合Ranking方法;</li>
<li>支持分布式搜索;</li>
<li>
<div id="result_box" style="TEXT-ALIGN: left" dir="ltr">提供文件的摘录生成;</div>
</li>
<li>
<div style="TEXT-ALIGN: left" dir="ltr">可作为MySQL的存储引擎提供搜索服务;</div>
</li>
<li>
<div style="TEXT-ALIGN: left" dir="ltr">支持布尔、短语、词语相似度等多种检索模式;</div>
</li>
<li>
<div style="TEXT-ALIGN: left" dir="ltr">文档支持多个全文检索字段(最大不超过32个);</div>
</li>
<li>
<div style="TEXT-ALIGN: left" dir="ltr">文档支持多个额外的属性信息(例如：分组信息，时间戳等);</div>
</li>
<li>
<div style="TEXT-ALIGN: left" dir="ltr">停止词查询;</div>
</li>
<li>
<div style="TEXT-ALIGN: left" dir="ltr">支持单一字节编码和UTF-8编码;</div>
</li>
<li>
<div style="TEXT-ALIGN: left" dir="ltr">原生的MySQL支持(同时支持MyISAM和InnoDB);</div>
</li>
<li>
<div style="TEXT-ALIGN: left" dir="ltr">原生的PostgreSQL支持.</div>
</li>
</ul>
<p style="TEXT-ALIGN: left" dir="ltr">中文手册可以在<a href="http://www.coreseek.com/uploads/pdf/sphinx_doc_zhcn_0.9.pdf" target="_blank">这里</a>获得，感谢<a href="http://www.coreseek.com" target="_blank">译者</a>的辛勤工作。</p>
<p style="TEXT-ALIGN: left" dir="ltr">二、Sphinx在windows上的安装</p>
<p style="TEXT-ALIGN: left" dir="ltr">1.直接在<a href="http://www.sphinxsearch.com/downloads.html" target="_blank">http://www.sphinxsearch.com/downloads.html</a>找到最新的windows版本，我这里下的是<a href="http://www.sphinxsearch.com/downloads/sphinx-0.9.8.1-win32.zip" target="_blank">Win32 release binaries with MySQL support</a>，下载后解压在D:\sphinx目录下；</p>
<p style="TEXT-ALIGN: left" dir="ltr">2.在D:\sphinx\下新建一个data目录用来存放索引文件，一个log目录方日志文件，复制D:\sphinx\sphinx.conf.in到D:\sphinx\bin\sphinx.conf（注意修改文件名）；</p>
<p style="TEXT-ALIGN: left" dir="ltr">3.修改D:\sphinx\bin\sphinx.conf，我这里列出需要修改的几个：</p>
<blockquote>
<pre style="TEXT-ALIGN: left" dir="ltr">type        = mysql # 数据源，我这里是mysql
sql_host    = localhost # 数据库服务器
sql_user    = root # 数据库用户名
sql_pass    = '' # 数据库密码
sql_db      = test # 数据库
sql_port    = 3306 # 数据库端口</pre>
<pre style="TEXT-ALIGN: left" dir="ltr">sql_query_pre   = SET NAMES utf8 # 去掉此行前面的注释，如果你的数据库是uft8编码的</pre>
<pre style="TEXT-ALIGN: left" dir="ltr">index test1
{
# 放索引的目录
 path   = D:/sphinx/data/
# 编码
 charset_type  = utf-8
 #  指定utf-8的编码表
 charset_table  = 0..9, A..Z-&gt;a..z, _, a..z, U+410..U+42F-&gt;U+430..U+44F, U+430..U+44F
 # 简单分词，只支持0和1，如果要搜索中文，请指定为1
 ngram_len    = 1
# 需要分词的字符，如果要搜索中文，去掉前面的注释
 ngram_chars   = U+3000..U+2FA1F
}</pre>
<pre style="TEXT-ALIGN: left" dir="ltr"># index test1stemmed : test1
# {
 # path   = @CONFDIR@/data/test1stemmed
 # morphology  = stem_en
# }
# 如果没有分布式索引，注释掉下面的内容
# index dist1
# {
 # 'distributed' index type MUST be specified
 # type    = distributed</pre>
<pre style="TEXT-ALIGN: left" dir="ltr"> # local index to be searched
 # there can be many local indexes configured
 # local    = test1
 # local    = test1stemmed</pre>
<pre style="TEXT-ALIGN: left" dir="ltr"> # remote agent
 # multiple remote agents may be specified
 # syntax is 'hostname:port:index1,[index2[,...]]
 # agent    = localhost:3313:remote1
 # agent    = localhost:3314:remote2,remote3</pre>
<pre style="TEXT-ALIGN: left" dir="ltr"> # remote agent connection timeout, milliseconds
 # optional, default is 1000 ms, ie. 1 sec
 # agent_connect_timeout = 1000</pre>
<pre style="TEXT-ALIGN: left" dir="ltr"> # remote agent query timeout, milliseconds
 # optional, default is 3000 ms, ie. 3 sec
 # agent_query_timeout  = 3000
# }</pre>
<pre style="TEXT-ALIGN: left" dir="ltr"># 搜索服务需要修改的部分
searchd
{
 # 日志
 log     = D:/sphinx/log/searchd.log</pre>
<pre style="TEXT-ALIGN: left" dir="ltr"> # PID file, searchd process ID file name
 pid_file   = D:/sphinx/log/searchd.pid</pre>
<pre style="TEXT-ALIGN: left" dir="ltr"> # windows下启动searchd服务一定要注释掉这个
 # seamless_rotate  = 1
}</pre>
</blockquote>
<p>4.导入测试数据</p>
<p>C:\Program Files\MySQL\MySQL Server 5.0\bin&gt;mysql -uroot test&lt;d:/sphinx/example.sql</p>
<p>5.建立索引</p>
<blockquote><p>D:\sphinx\bin&gt;indexer.exe &#8211;all<br />
Sphinx 0.9.8-release (r1533)<br />
Copyright (c) 2001-2008, Andrew Aksyonoff</p>
<p>using config file &#8216;./sphinx.conf&#8217;&#8230;<br />
indexing index &#8216;test1&#8242;&#8230;<br />
collected 4 docs, 0.0 MB<br />
sorted 0.0 Mhits, 100.0% done<br />
total 4 docs, 193 bytes<br />
total 0.101 sec, 1916.30 bytes/sec, 39.72 docs/sec</p>
<p>D:\sphinx\bin&gt;</p></blockquote>
<p>6.搜索&#8217;test&#8217;试试</p>
<blockquote><p>D:\sphinx\bin&gt;search.exe test<br />
Sphinx 0.9.8-release (r1533)<br />
Copyright (c) 2001-2008, Andrew Aksyonoff</p>
<p>using config file &#8216;./sphinx.conf&#8217;&#8230;<br />
index &#8216;test1&#8242;: query &#8216;test &#8216;: returned 3 matches of 3 total in 0.000 sec</p>
<p>displaying matches:<br />
1. document=1, weight=2, group_id=1, date_added=Wed Nov 26 14:58:59 2008<br />
        id=1<br />
        group_id=1<br />
        group_id2=5<br />
        date_added=2008-11-26 14:58:59<br />
        title=test one<br />
        content=this is my test document number one. also checking search within<br />
 phrases.<br />
2. document=2, weight=2, group_id=1, date_added=Wed Nov 26 14:58:59 2008<br />
        id=2<br />
        group_id=1<br />
        group_id2=6<br />
        date_added=2008-11-26 14:58:59<br />
        title=test two<br />
        content=this is my test document number two<br />
3. document=4, weight=1, group_id=2, date_added=Wed Nov 26 14:58:59 2008<br />
        id=4<br />
        group_id=2<br />
        group_id2=8<br />
        date_added=2008-11-26 14:58:59<br />
        title=doc number four<br />
        content=this is to test groups</p>
<p>words:<br />
1. &#8216;test&#8217;: 3 documents, 5 hits<br />
D:\sphinx\bin&gt;</p></blockquote>
<p>都所出来了吧。</p>
<p>6.测试中文搜索</p>
<p>修改test数据库中documents数据表，</p>
<blockquote><p>UPDATE `test`.`documents` SET `title` = &#8216;测试中文&#8217;, `content` = &#8216;this is my test document number two，应该搜的到吧&#8217; WHERE `documents`.`id` = 2;</p></blockquote>
<p>重建索引：</p>
<p>D:\sphinx\bin&gt;indexer.exe &#8211;all</p>
<p>搜索&#8217;中文&#8217;试试：</p>
<blockquote><p>D:\sphinx\bin&gt;search.exe 中文<br />
Sphinx 0.9.8-release (r1533)<br />
Copyright (c) 2001-2008, Andrew Aksyonoff</p>
<p>using config file &#8216;./sphinx.conf&#8217;&#8230;<br />
index &#8216;test1&#8242;: query &#8216;中文 &#8216;: returned 0 matches of 0 total in 0.000 sec</p>
<p>words:<br />
D:\sphinx\bin&gt;</p></blockquote>
<p>貌似没有搜到，这是因为windows命令行中的编码是gbk，当然搜不出来。我们可以用程序试试，在D:\sphinx\api下新建一个foo.php的文件，注意utf-8编码</p>
<blockquote><p>&lt;?php<br />
require &#8216;sphinxapi.php&#8217;;<br />
$s = new SphinxClient();<br />
$s-&gt;SetServer(&#8216;localhost&#8217;,3312);<br />
$result = $s-&gt;Query(&#8216;中文&#8217;);<br />
var_dump($result);<br />
?&gt;</p></blockquote>
<p>启动Sphinx searchd服务</p>
<blockquote><p>D:\sphinx\bin&gt;searchd.exe<br />
Sphinx 0.9.8-release (r1533)<br />
Copyright (c) 2001-2008, Andrew Aksyonoff</p>
<p>WARNING: forcing &#8211;console mode on Windows<br />
using config file &#8216;./sphinx.conf&#8217;&#8230;<br />
creating server socket on 0.0.0.0:3312<br />
accepting connections</p></blockquote>
<p>执行PHP查询：</p>
<blockquote><p>php d:/sphinx/api/foo.php</p></blockquote>
<p>结果是不是出来？剩下的工作就是去看手册，慢慢摸索高阶的配置。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fuchaoqun.com/2008/11/sphinx-on-windows-xp/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
