SearchTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Search_Lucene
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Zend_Search_Lucene
  24. */
  25. require_once 'Zend/Search/Lucene.php';
  26. /**
  27. * PHPUnit test case
  28. */
  29. require_once 'PHPUnit/Framework/TestCase.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Search_Lucene
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Search_Lucene
  37. */
  38. class Zend_Search_Lucene_SearchTest extends PHPUnit_Framework_TestCase
  39. {
  40. public function testQueryParser()
  41. {
  42. $wildcardMinPrefix = Zend_Search_Lucene_Search_Query_Wildcard::getMinPrefixLength();
  43. Zend_Search_Lucene_Search_Query_Wildcard::setMinPrefixLength(0);
  44. $defaultPrefixLength = Zend_Search_Lucene_Search_Query_Fuzzy::getDefaultPrefixLength();
  45. Zend_Search_Lucene_Search_Query_Fuzzy::setDefaultPrefixLength(0);
  46. $queries = array('title:"The Right Way" AND text:go',
  47. 'title:"Do it right" AND right',
  48. 'title:Do it right',
  49. 'te?t',
  50. 'test*',
  51. 'te*t',
  52. '?Ma*',
  53. // 'te?t~20^0.8',
  54. 'test~',
  55. 'test~0.4',
  56. '"jakarta apache"~10',
  57. 'contents:[business TO by]',
  58. '{wish TO zzz}',
  59. 'jakarta apache',
  60. 'jakarta^4 apache',
  61. '"jakarta apache"^4 "Apache Lucene"',
  62. '"jakarta apache" jakarta',
  63. '"jakarta apache" OR jakarta',
  64. '"jakarta apache" || jakarta',
  65. '"jakarta apache" AND "Apache Lucene"',
  66. '"jakarta apache" && "Apache Lucene"',
  67. '+jakarta apache',
  68. '"jakarta apache" AND NOT "Apache Lucene"',
  69. '"jakarta apache" && !"Apache Lucene"',
  70. '\\ ',
  71. 'NOT "jakarta apache"',
  72. '!"jakarta apache"',
  73. '"jakarta apache" -"Apache Lucene"',
  74. '(jakarta OR apache) AND website',
  75. '(jakarta || apache) && website',
  76. 'title:(+return +"pink panther")',
  77. 'title:(+re\\turn\\ value +"pink panther\\"" +body:cool)',
  78. '+contents:apache +type:1 +id:5',
  79. 'contents:apache AND type:1 AND id:5',
  80. 'f1:word1 f1:word2 and f1:word3',
  81. 'f1:word1 not f1:word2 and f1:word3'
  82. );
  83. $rewrittenQueries = array('+(title:"the right way") +(text:go)',
  84. '+(title:"do it right") +(path:right modified:right contents:right)',
  85. '(title:do) (path:it modified:it contents:it) (path:right modified:right contents:right)',
  86. '(contents:test contents:text)',
  87. '(contents:test contents:tested)',
  88. '(contents:test contents:text)',
  89. '(contents:amazon contents:email)',
  90. // ....
  91. '((contents:test) (contents:text^0.5))',
  92. '((contents:test) (contents:text^0.5833) (contents:latest^0.1667) (contents:left^0.1667) (contents:list^0.1667) (contents:meet^0.1667) (contents:must^0.1667) (contents:next^0.1667) (contents:post^0.1667) (contents:sect^0.1667) (contents:task^0.1667) (contents:tested^0.1667) (contents:that^0.1667) (contents:tort^0.1667))',
  93. '((path:"jakarta apache"~10) (modified:"jakarta apache"~10) (contents:"jakarta apache"~10))',
  94. '(contents:business contents:but contents:buy contents:buying contents:by)',
  95. '(path:wishlist contents:wishlist contents:wishlists contents:with contents:without contents:won contents:work contents:would contents:write contents:writing contents:written contents:www contents:xml contents:xmlrpc contents:you contents:your)',
  96. '(path:jakarta modified:jakarta contents:jakarta) (path:apache modified:apache contents:apache)',
  97. '((path:jakarta modified:jakarta contents:jakarta)^4) (path:apache modified:apache contents:apache)',
  98. '(((path:"jakarta apache") (modified:"jakarta apache") (contents:"jakarta apache"))^4) ((path:"apache lucene") (modified:"apache lucene") (contents:"apache lucene"))',
  99. '((path:"jakarta apache") (modified:"jakarta apache") (contents:"jakarta apache")) (path:jakarta modified:jakarta contents:jakarta)',
  100. '((path:"jakarta apache") (modified:"jakarta apache") (contents:"jakarta apache")) (path:jakarta modified:jakarta contents:jakarta)',
  101. '((path:"jakarta apache") (modified:"jakarta apache") (contents:"jakarta apache")) (path:jakarta modified:jakarta contents:jakarta)',
  102. '+((path:"jakarta apache") (modified:"jakarta apache") (contents:"jakarta apache")) +((path:"apache lucene") (modified:"apache lucene") (contents:"apache lucene"))',
  103. '+((path:"jakarta apache") (modified:"jakarta apache") (contents:"jakarta apache")) +((path:"apache lucene") (modified:"apache lucene") (contents:"apache lucene"))',
  104. '+(path:jakarta modified:jakarta contents:jakarta) (path:apache modified:apache contents:apache)',
  105. '+((path:"jakarta apache") (modified:"jakarta apache") (contents:"jakarta apache")) -((path:"apache lucene") (modified:"apache lucene") (contents:"apache lucene"))',
  106. '+((path:"jakarta apache") (modified:"jakarta apache") (contents:"jakarta apache")) -((path:"apache lucene") (modified:"apache lucene") (contents:"apache lucene"))',
  107. '(<InsignificantQuery>)',
  108. '<InsignificantQuery>',
  109. '<InsignificantQuery>',
  110. '((path:"jakarta apache") (modified:"jakarta apache") (contents:"jakarta apache")) -((path:"apache lucene") (modified:"apache lucene") (contents:"apache lucene"))',
  111. '+((path:jakarta modified:jakarta contents:jakarta) (path:apache modified:apache contents:apache)) +(path:website modified:website contents:website)',
  112. '+((path:jakarta modified:jakarta contents:jakarta) (path:apache modified:apache contents:apache)) +(path:website modified:website contents:website)',
  113. '(+(title:return) +(title:"pink panther"))',
  114. '(+(+title:return +title:value) +(title:"pink panther") +(body:cool))',
  115. '+(contents:apache) +(<InsignificantQuery>) +(<InsignificantQuery>)',
  116. '+(contents:apache) +(<InsignificantQuery>) +(<InsignificantQuery>)',
  117. '(f1:word) (+(f1:word) +(f1:word))',
  118. '(f1:word) (-(f1:word) +(f1:word))');
  119. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  120. foreach ($queries as $id => $queryString) {
  121. $query = Zend_Search_Lucene_Search_QueryParser::parse($queryString);
  122. $this->assertTrue($query instanceof Zend_Search_Lucene_Search_Query);
  123. $this->assertEquals($query->rewrite($index)->__toString(), $rewrittenQueries[$id]);
  124. }
  125. Zend_Search_Lucene_Search_Query_Wildcard::setMinPrefixLength($wildcardMinPrefix);
  126. Zend_Search_Lucene_Search_Query_Fuzzy::setDefaultPrefixLength($defaultPrefixLength);
  127. }
  128. public function testQueryParserExceptionsHandling()
  129. {
  130. $this->assertTrue(Zend_Search_Lucene_Search_QueryParser::queryParsingExceptionsSuppressed());
  131. try {
  132. $query = Zend_Search_Lucene_Search_QueryParser::parse('contents:[business TO by}');
  133. } catch (Zend_Search_Lucene_Exception $e) {
  134. $this->fail('exception raised while parsing a query');
  135. }
  136. $this->assertEquals('contents business to by', $query->__toString());
  137. Zend_Search_Lucene_Search_QueryParser::dontSuppressQueryParsingExceptions();
  138. $this->assertFalse(Zend_Search_Lucene_Search_QueryParser::queryParsingExceptionsSuppressed());
  139. try {
  140. $query = Zend_Search_Lucene_Search_QueryParser::parse('contents:[business TO by}');
  141. $this->fail('exception wasn\'t raised while parsing a query');
  142. } catch (Zend_Search_Lucene_Exception $e) {
  143. $this->assertEquals('Syntax error at char position 25.', $e->getMessage());
  144. }
  145. Zend_Search_Lucene_Search_QueryParser::suppressQueryParsingExceptions();
  146. $this->assertTrue(Zend_Search_Lucene_Search_QueryParser::queryParsingExceptionsSuppressed());
  147. }
  148. public function testEmptyQuery()
  149. {
  150. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  151. $hits = $index->find('');
  152. $this->assertEquals(count($hits), 0);
  153. }
  154. public function testTermQuery()
  155. {
  156. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  157. $hits = $index->find('submitting');
  158. $this->assertEquals(count($hits), 3);
  159. $expectedResultset = array(array(2, 0.114555, 'IndexSource/contributing.patches.html'),
  160. array(7, 0.112241, 'IndexSource/contributing.bugs.html'),
  161. array(8, 0.112241, 'IndexSource/contributing.html'));
  162. foreach ($hits as $resId => $hit) {
  163. $this->assertEquals($hit->id, $expectedResultset[$resId][0]);
  164. $this->assertTrue( abs($hit->score - $expectedResultset[$resId][1]) < 0.000001 );
  165. $this->assertEquals($hit->path, $expectedResultset[$resId][2]);
  166. }
  167. }
  168. public function testMultiTermQuery()
  169. {
  170. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  171. $hits = $index->find('submitting AND wishlists');
  172. $this->assertEquals(count($hits), 1);
  173. $this->assertEquals($hits[0]->id, 8);
  174. $this->assertTrue( abs($hits[0]->score - 0.141633) < 0.000001 );
  175. $this->assertEquals($hits[0]->path, 'IndexSource/contributing.html');
  176. }
  177. public function testPraseQuery()
  178. {
  179. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  180. $hits = $index->find('"reporting bugs"');
  181. $this->assertEquals(count($hits), 4);
  182. $expectedResultset = array(array(0, 0.247795, 'IndexSource/contributing.documentation.html'),
  183. array(7, 0.212395, 'IndexSource/contributing.bugs.html'),
  184. array(8, 0.212395, 'IndexSource/contributing.html'),
  185. array(2, 0.176996, 'IndexSource/contributing.patches.html'));
  186. foreach ($hits as $resId => $hit) {
  187. $this->assertEquals($hit->id, $expectedResultset[$resId][0]);
  188. $this->assertTrue( abs($hit->score - $expectedResultset[$resId][1]) < 0.000001 );
  189. $this->assertEquals($hit->path, $expectedResultset[$resId][2]);
  190. }
  191. }
  192. public function testBooleanQuery()
  193. {
  194. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  195. $hits = $index->find('submitting AND (wishlists OR requirements)');
  196. $this->assertEquals(count($hits), 2);
  197. $expectedResultset = array(array(7, 0.095697, 'IndexSource/contributing.bugs.html'),
  198. array(8, 0.075573, 'IndexSource/contributing.html'));
  199. foreach ($hits as $resId => $hit) {
  200. $this->assertEquals($hit->id, $expectedResultset[$resId][0]);
  201. $this->assertTrue( abs($hit->score - $expectedResultset[$resId][1]) < 0.000001 );
  202. $this->assertEquals($hit->path, $expectedResultset[$resId][2]);
  203. }
  204. }
  205. public function testBooleanQueryWithPhraseSubquery()
  206. {
  207. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  208. $hits = $index->find('"PEAR developers" AND Home');
  209. $this->assertEquals(count($hits), 1);
  210. $expectedResultset = array(array(1, 0.168270, 'IndexSource/contributing.wishlist.html'));
  211. foreach ($hits as $resId => $hit) {
  212. $this->assertEquals($hit->id, $expectedResultset[$resId][0]);
  213. $this->assertTrue( abs($hit->score - $expectedResultset[$resId][1]) < 0.000001 );
  214. $this->assertEquals($hit->path, $expectedResultset[$resId][2]);
  215. }
  216. }
  217. public function testBooleanQueryWithNonExistingPhraseSubquery()
  218. {
  219. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  220. $query = Zend_Search_Lucene_Search_QueryParser::parse('"Non-existing phrase" AND Home');
  221. $this->assertEquals($query->__toString(), '+("Non-existing phrase") +(Home)');
  222. $this->assertEquals($query->rewrite($index)->__toString(),
  223. '+((path:"non existing phrase") (modified:"non existing phrase") (contents:"non existing phrase")) +(path:home modified:home contents:home)');
  224. $this->assertEquals($query->rewrite($index)->optimize($index)->__toString(), '<EmptyQuery>');
  225. }
  226. public function testFilteredTokensQueryParserProcessing()
  227. {
  228. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  229. $this->assertEquals(count(Zend_Search_Lucene_Analysis_Analyzer::getDefault()->tokenize('123456787654321')), 0);
  230. $hits = $index->find('"PEAR developers" AND Home AND 123456787654321');
  231. $this->assertEquals(count($hits), 1);
  232. $expectedResultset = array(array(1, 0.168270, 'IndexSource/contributing.wishlist.html'));
  233. foreach ($hits as $resId => $hit) {
  234. $this->assertEquals($hit->id, $expectedResultset[$resId][0]);
  235. $this->assertTrue( abs($hit->score - $expectedResultset[$resId][1]) < 0.000001 );
  236. $this->assertEquals($hit->path, $expectedResultset[$resId][2]);
  237. }
  238. }
  239. public function testWildcardQuery()
  240. {
  241. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  242. $wildcardMinPrefix = Zend_Search_Lucene_Search_Query_Wildcard::getMinPrefixLength();
  243. Zend_Search_Lucene_Search_Query_Wildcard::setMinPrefixLength(0);
  244. $hits = $index->find('*cont*');
  245. $this->assertEquals(count($hits), 9);
  246. $expectedResultset = array(array(8, 0.125253, 'IndexSource/contributing.html'),
  247. array(4, 0.112122, 'IndexSource/copyright.html'),
  248. array(2, 0.108491, 'IndexSource/contributing.patches.html'),
  249. array(7, 0.077716, 'IndexSource/contributing.bugs.html'),
  250. array(0, 0.050760, 'IndexSource/contributing.documentation.html'),
  251. array(1, 0.049163, 'IndexSource/contributing.wishlist.html'),
  252. array(3, 0.036159, 'IndexSource/about-pear.html'),
  253. array(5, 0.021500, 'IndexSource/authors.html'),
  254. array(9, 0.007422, 'IndexSource/core.html'));
  255. foreach ($hits as $resId => $hit) {
  256. $this->assertEquals($hit->id, $expectedResultset[$resId][0]);
  257. $this->assertTrue( abs($hit->score - $expectedResultset[$resId][1]) < 0.000001 );
  258. $this->assertEquals($hit->path, $expectedResultset[$resId][2]);
  259. }
  260. Zend_Search_Lucene_Search_Query_Wildcard::setMinPrefixLength($wildcardMinPrefix);
  261. }
  262. public function testFuzzyQuery()
  263. {
  264. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  265. $defaultPrefixLength = Zend_Search_Lucene_Search_Query_Fuzzy::getDefaultPrefixLength();
  266. Zend_Search_Lucene_Search_Query_Fuzzy::setDefaultPrefixLength(0);
  267. $hits = $index->find('tesd~0.4');
  268. $this->assertEquals(count($hits), 9);
  269. $expectedResultset = array(array(2, 0.037139, 'IndexSource/contributing.patches.html'),
  270. array(0, 0.008735, 'IndexSource/contributing.documentation.html'),
  271. array(7, 0.002449, 'IndexSource/contributing.bugs.html'),
  272. array(1, 0.000483, 'IndexSource/contributing.wishlist.html'),
  273. array(3, 0.000483, 'IndexSource/about-pear.html'),
  274. array(9, 0.000483, 'IndexSource/core.html'),
  275. array(5, 0.000414, 'IndexSource/authors.html'),
  276. array(8, 0.000414, 'IndexSource/contributing.html'),
  277. array(4, 0.000345, 'IndexSource/copyright.html'));
  278. foreach ($hits as $resId => $hit) {
  279. $this->assertEquals($hit->id, $expectedResultset[$resId][0]);
  280. $this->assertTrue( abs($hit->score - $expectedResultset[$resId][1]) < 0.000001 );
  281. $this->assertEquals($hit->path, $expectedResultset[$resId][2]);
  282. }
  283. Zend_Search_Lucene_Search_Query_Fuzzy::setDefaultPrefixLength($defaultPrefixLength);
  284. }
  285. public function testInclusiveRangeQuery()
  286. {
  287. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  288. $hits = $index->find('[xml TO zzzzz]');
  289. $this->assertEquals(count($hits), 5);
  290. $expectedResultset = array(array(4, 0.156366, 'IndexSource/copyright.html'),
  291. array(2, 0.080458, 'IndexSource/contributing.patches.html'),
  292. array(7, 0.060214, 'IndexSource/contributing.bugs.html'),
  293. array(1, 0.009687, 'IndexSource/contributing.wishlist.html'),
  294. array(5, 0.005871, 'IndexSource/authors.html'));
  295. foreach ($hits as $resId => $hit) {
  296. $this->assertEquals($hit->id, $expectedResultset[$resId][0]);
  297. $this->assertTrue( abs($hit->score - $expectedResultset[$resId][1]) < 0.000001 );
  298. $this->assertEquals($hit->path, $expectedResultset[$resId][2]);
  299. }
  300. }
  301. public function testNonInclusiveRangeQuery()
  302. {
  303. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  304. $hits = $index->find('{xml TO zzzzz}');
  305. $this->assertEquals(count($hits), 5);
  306. $expectedResultset = array(array(2, 0.1308671, 'IndexSource/contributing.patches.html'),
  307. array(7, 0.0979391, 'IndexSource/contributing.bugs.html'),
  308. array(4, 0.0633930, 'IndexSource/copyright.html'),
  309. array(1, 0.0157556, 'IndexSource/contributing.wishlist.html'),
  310. array(5, 0.0095493, 'IndexSource/authors.html'));
  311. foreach ($hits as $resId => $hit) {
  312. $this->assertEquals($hit->id, $expectedResultset[$resId][0]);
  313. $this->assertTrue( abs($hit->score - $expectedResultset[$resId][1]) < 0.000001 );
  314. $this->assertEquals($hit->path, $expectedResultset[$resId][2]);
  315. }
  316. }
  317. public function testDefaultSearchField()
  318. {
  319. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  320. $storedDefaultSearchField = Zend_Search_Lucene::getDefaultSearchField();
  321. Zend_Search_Lucene::setDefaultSearchField('path');
  322. $hits = $index->find('contributing');
  323. $this->assertEquals(count($hits), 5);
  324. $expectedResultset = array(array(8, 0.847922, 'IndexSource/contributing.html'),
  325. array(0, 0.678337, 'IndexSource/contributing.documentation.html'),
  326. array(1, 0.678337, 'IndexSource/contributing.wishlist.html'),
  327. array(2, 0.678337, 'IndexSource/contributing.patches.html'),
  328. array(7, 0.678337, 'IndexSource/contributing.bugs.html'));
  329. foreach ($hits as $resId => $hit) {
  330. $this->assertEquals($hit->id, $expectedResultset[$resId][0]);
  331. $this->assertTrue( abs($hit->score - $expectedResultset[$resId][1]) < 0.000001 );
  332. $this->assertEquals($hit->path, $expectedResultset[$resId][2]);
  333. }
  334. Zend_Search_Lucene::setDefaultSearchField($storedDefaultSearchField);
  335. }
  336. public function testQueryHit()
  337. {
  338. // Restore default search field if it wasn't done by previous test because of failure
  339. Zend_Search_Lucene::setDefaultSearchField(null);
  340. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  341. $hits = $index->find('submitting AND wishlists');
  342. $hit = $hits[0];
  343. $this->assertTrue($hit instanceof Zend_Search_Lucene_Search_QueryHit);
  344. $this->assertTrue($hit->getIndex() instanceof Zend_Search_Lucene_Interface);
  345. $doc = $hit->getDocument();
  346. $this->assertTrue($doc instanceof Zend_Search_Lucene_Document);
  347. $this->assertEquals($doc->path, 'IndexSource/contributing.html');
  348. }
  349. public function testDelayedResourceCleanUp()
  350. {
  351. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  352. $hits = $index->find('submitting AND wishlists');
  353. unset($index);
  354. $hit = $hits[0];
  355. $this->assertTrue($hit instanceof Zend_Search_Lucene_Search_QueryHit);
  356. $this->assertTrue($hit->getIndex() instanceof Zend_Search_Lucene_Interface);
  357. $doc = $hit->getDocument();
  358. $this->assertTrue($doc instanceof Zend_Search_Lucene_Document);
  359. $this->assertTrue($hit->getIndex() instanceof Zend_Search_Lucene_Interface);
  360. $this->assertEquals($doc->path, 'IndexSource/contributing.html');
  361. }
  362. public function testSortingResult()
  363. {
  364. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  365. $hits = $index->find('"reporting bugs"', 'path');
  366. $this->assertEquals(count($hits), 4);
  367. $expectedResultset = array(array(7, 0.212395, 'IndexSource/contributing.bugs.html'),
  368. array(0, 0.247795, 'IndexSource/contributing.documentation.html'),
  369. array(8, 0.212395, 'IndexSource/contributing.html'),
  370. array(2, 0.176996, 'IndexSource/contributing.patches.html'));
  371. foreach ($hits as $resId => $hit) {
  372. $this->assertEquals($hit->id, $expectedResultset[$resId][0]);
  373. $this->assertTrue( abs($hit->score - $expectedResultset[$resId][1]) < 0.000001 );
  374. $this->assertEquals($hit->path, $expectedResultset[$resId][2]);
  375. }
  376. }
  377. public function testLimitingResult()
  378. {
  379. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  380. $storedResultSetLimit = Zend_Search_Lucene::getResultSetLimit();
  381. Zend_Search_Lucene::setResultSetLimit(3);
  382. $hits = $index->find('"reporting bugs"', 'path');
  383. $this->assertEquals(count($hits), 3);
  384. $expectedResultset = array(array(7, 0.212395, 'IndexSource/contributing.bugs.html'),
  385. array(0, 0.247795, 'IndexSource/contributing.documentation.html'),
  386. array(2, 0.176996, 'IndexSource/contributing.patches.html'));
  387. foreach ($hits as $resId => $hit) {
  388. $this->assertEquals($hit->id, $expectedResultset[$resId][0]);
  389. $this->assertTrue( abs($hit->score - $expectedResultset[$resId][1]) < 0.000001 );
  390. $this->assertEquals($hit->path, $expectedResultset[$resId][2]);
  391. }
  392. Zend_Search_Lucene::setResultSetLimit($storedResultSetLimit);
  393. }
  394. }