SearchTest.php 25 KB

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