构建和测试索引 您现在已经准备好为 Body Parts 应用程序构建索引。为此,需要执行以下步骤: 键入 $ sudo mkdir -p /var/data/sphinx 创建目录结构 /var/data/sphinx 假定 MySQL 正在运行,使用如下所示的代码运行索引器来创建索引。 清单 10. 创建索引$ sudo /usr/local/bin/indexer --config /usr/local/etc/sphinx.conf --allSphinx 0.9.7Copyright (c) 2001-2007, Andrew Aksyonoffusing config file '/usr/local/etc/sphinx.conf'...indexing index 'catalog'...collected 8 docs, 0.0 MBsorted 0.0 Mhits, 82.8% donetotal 8 docs, 149 bytestotal 0.010 sec, 14900.00 bytes/sec, 800.00 docs/sec 注: -all 参数将重构 sphinx.conf 中列出的所有索引。如果不需要重构所有索引,您可以使用其他参数只对部分索引进行重构。 您现在可以使用如下所示的代码用 search 实用程序测试索引(不必运行 searchd 即可使用 search)。 清单 11. 用 search 测试索引$ /usr/local/bin/search --config /usr/local/etc/sphinx.conf ENGSphinx 0.9.7Copyright (c) 2001-2007, Andrew Aksyonoffindex 'catalog': query 'ENG ': returned 2 matches of 2 total in 0.000 secdisplaying matches:1. document=8, weight=1, assembly=5, model=7id=8partno=ENG088descrīption=Cylinder headprice=552. document=9, weight=1, assembly=5, model=3id=9partno=ENG976descrīption=Large cylinder headprice=65words:1. 'eng': 2 documents, 2 hits$ /usr/local/bin/search --config /usr/local/etc/sphinx.conf windSphinx 0.9.7Copyright (c) 2001-2007, Andrew Aksyonoffindex 'catalog': query 'wind ': returned 2 matches of 2 total in 0.000 secdisplaying matches:1. document=1, weight=1, assembly=3, model=1id=1partno=WIN408descrīption=Portal windowprice=4232. document=5, weight=1, assembly=3, model=1id=5partno=WIN958descrīption=Windshield, frontprice=500words:1. 'wind': 2 documents, 2 hits$ /usr/local/bin/search /--config /usr/local/etc/sphinx.conf --filter model 3 ENGSphinx 0.9.7Copyright (c) 2001-2007, Andrew Aksyonoffindex 'catalog': query 'ENG ': returned 1 matches of 1 total in 0.000 secdisplaying matches:1. document=9, weight=1, assembly=5, model=3id=9partno=ENG976descrīption=Large cylinder headprice=65words:1. 'eng': 2 documents, 2 hits 第一条命令 /usr/local/bin/search --config /usr/local/etc/sphinx.conf ENG 在零件号中找到了两个含有 ENG 的结果。第二条命令 /usr/local/bin/search --config /usr/local/etc/sphinx.conf wind 在两个零件描述中找到了子字符串 wind。而第三条命令把结果限定为 model 为 3 的条目。 编写代码 最后,您可以编写 PHP 代码来调用 Sphinx 搜索引擎。Sphinx PHP API 非常小并且易于掌握。清单 12 是一个小型 PHP 应用程序,用于调用 searchd 以得到使用上面所示的最后一条命令得到的相同结果(“在属于型号 3 的名称中找到含有 ‘cylinder’ 的所有零件”)。 清单 12. 从 PHP 调用 Sphinx 搜索引擎<?phpinclude('sphinx-0.9.7/api/sphinxapi.php');$cl = new SphinxClient();$cl->SetServer( "localhost", 3312 );$cl->SetMatchMode( SPH_MATCH_ANY );$cl->SetFilter( 'model', array( 3 ) );$result = $cl->Query( 'cylinder', 'catalog' );if ( $result === false ) { echo "Query failed: " . $cl->GetLastError() . "./n";}else { if ( $cl->GetLastWarning() ) { echo "WARNING: " . $cl->GetLastWarning() . ""; } if ( ! empty($result["matches"]) ) { foreach ( $result["matches"] as $doc => $docinfo ) { echo "$doc/n"; } print_r( $result ); }}exit;?> 要测试代码,需要为 Sphinx 创建 log 目录,启动 searchd,然后运行 PHP 应用程序,如下所示: 清单 13. PHP 应用程序$ sudo mkdir -p /var/log/searchd$ sudo /usr/local/bin/searchd --config /usr/local/etc/sphinx.conf$ php search.php9Array ([fields] => Array ( [0] => partno [1] => descrīption)[attrs] => Array( [assembly] => 1 [model] => 1)[matches] => Array( [9] => Array( [weight] => 1 [attrs] => Array( [assembly] => 5 [model] => 3 ) ))[total] => 1[total_found] => 1[time] => 0.000[words] => Array( [cylind] => Array( [docs] => 2 [hits] => 2 ))) 输出为 9:匹配的单行的正确主键。如果 Sphinx 找到匹配,相关数组 $result 将包含名为 results 的元素。浏览 print_r() 的输出以查看返回的其他内容。 注意事项:total_found 是在索引中找到的匹配总数,而 found 是返回的结果数。这两者可能不同,因为您可以更改每次返回多少个匹配结果以及要返回哪批匹配结果,哪个结果利于对冗长的结果列表分页。请查看 API 调用 SetLimits()。一个分页示例是用 $cl->SetLimits( ( $page - 1 ) * SPAN, SPAN ) 调用搜索引擎返回第一批、第二批、第三批(依此类推)SPAN 匹配结果,这取决于显示哪个页面。 结束语 Sphinx 还有更多的功能可以利用。我在这里仅仅介绍了最浅显的一部分,但是您现在有一个可以工作的现实示例作为基石来扩展您的技能。 仔细研读随发行版附带的样例 Sphinx 配置文件 /usr/local/etc/sphinx.conf.dist。该文件中的注释将说明每个 Sphinx 参数可以实现的功能;展示如何创建分布式冗余配置;并说明如何继承基本设置以避免源代码及索引中的重复。Sphinx README 文件还是十分丰富的信息源,包括如何将 Sphinx 直接嵌入 MySQL V5 —— 不需要使用守护程序。