Search23Test.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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_Search23Test 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. $rewrittenQueries = array('+(title:"the right way") +(text:go)',
  83. '+(title:"do it right") +(pathkeyword:right path:right modified:right contents:right)',
  84. '(title:do) (pathkeyword:it path:it modified:it contents:it) (pathkeyword:right path:right modified:right contents:right)',
  85. '(contents:test contents:text)',
  86. '(contents:test contents:tested)',
  87. '(contents:test contents:text)',
  88. '(contents:amazon contents:email)',
  89. // ....
  90. '((contents:test) (contents:text^0.5))',
  91. '((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))',
  92. '((pathkeyword:"jakarta apache"~10) (path:"jakarta apache"~10) (modified:"jakarta apache"~10) (contents:"jakarta apache"~10))',
  93. '(contents:business contents:but contents:buy contents:buying contents:by)',
  94. '(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)',
  95. '(pathkeyword:jakarta path:jakarta modified:jakarta contents:jakarta) (pathkeyword:apache path:apache modified:apache contents:apache)',
  96. '((pathkeyword:jakarta path:jakarta modified:jakarta contents:jakarta)^4) (pathkeyword:apache path:apache modified:apache contents:apache)',
  97. '(((pathkeyword:"jakarta apache") (path:"jakarta apache") (modified:"jakarta apache") (contents:"jakarta apache"))^4) ((pathkeyword:"apache lucene") (path:"apache lucene") (modified:"apache lucene") (contents:"apache lucene"))',
  98. '((pathkeyword:"jakarta apache") (path:"jakarta apache") (modified:"jakarta apache") (contents:"jakarta apache")) (pathkeyword:jakarta path:jakarta modified:jakarta contents:jakarta)',
  99. '((pathkeyword:"jakarta apache") (path:"jakarta apache") (modified:"jakarta apache") (contents:"jakarta apache")) (pathkeyword:jakarta path:jakarta modified:jakarta contents:jakarta)',
  100. '((pathkeyword:"jakarta apache") (path:"jakarta apache") (modified:"jakarta apache") (contents:"jakarta apache")) (pathkeyword:jakarta path:jakarta modified:jakarta contents:jakarta)',
  101. '+((pathkeyword:"jakarta apache") (path:"jakarta apache") (modified:"jakarta apache") (contents:"jakarta apache")) +((pathkeyword:"apache lucene") (path:"apache lucene") (modified:"apache lucene") (contents:"apache lucene"))',
  102. '+((pathkeyword:"jakarta apache") (path:"jakarta apache") (modified:"jakarta apache") (contents:"jakarta apache")) +((pathkeyword:"apache lucene") (path:"apache lucene") (modified:"apache lucene") (contents:"apache lucene"))',
  103. '+(pathkeyword:jakarta path:jakarta modified:jakarta contents:jakarta) (pathkeyword:apache path:apache modified:apache contents:apache)',
  104. '+((pathkeyword:"jakarta apache") (path:"jakarta apache") (modified:"jakarta apache") (contents:"jakarta apache")) -((pathkeyword:"apache lucene") (path:"apache lucene") (modified:"apache lucene") (contents:"apache lucene"))',
  105. '+((pathkeyword:"jakarta apache") (path:"jakarta apache") (modified:"jakarta apache") (contents:"jakarta apache")) -((pathkeyword:"apache lucene") (path:"apache lucene") (modified:"apache lucene") (contents:"apache lucene"))',
  106. '(<InsignificantQuery>)',
  107. '<InsignificantQuery>',
  108. '<InsignificantQuery>',
  109. '((pathkeyword:"jakarta apache") (path:"jakarta apache") (modified:"jakarta apache") (contents:"jakarta apache")) -((pathkeyword:"apache lucene") (path:"apache lucene") (modified:"apache lucene") (contents:"apache lucene"))',
  110. '+((pathkeyword:jakarta path:jakarta modified:jakarta contents:jakarta) (pathkeyword:apache path:apache modified:apache contents:apache)) +(pathkeyword:website path:website modified:website contents:website)',
  111. '+((pathkeyword:jakarta path:jakarta modified:jakarta contents:jakarta) (pathkeyword:apache path:apache modified:apache contents:apache)) +(pathkeyword:website path:website modified:website contents:website)',
  112. '(+(title:return) +(title:"pink panther"))',
  113. '(+(+title:return +title:value) +(title:"pink panther") +(body:cool))',
  114. '+(contents:apache) +(<InsignificantQuery>) +(<InsignificantQuery>)',
  115. '+(contents:apache) +(<InsignificantQuery>) +(<InsignificantQuery>)',
  116. '(f1:word) (+(f1:word) +(f1:word))',
  117. '(f1:word) (-(f1:word) +(f1:word))');
  118. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_index23Sample/_files');
  119. foreach ($queries as $id => $queryString) {
  120. $query = Zend_Search_Lucene_Search_QueryParser::parse($queryString);
  121. $this->assertTrue($query instanceof Zend_Search_Lucene_Search_Query);
  122. $this->assertEquals($query->rewrite($index)->__toString(), $rewrittenQueries[$id]);
  123. }
  124. Zend_Search_Lucene_Search_Query_Wildcard::setMinPrefixLength($wildcardMinPrefix);
  125. Zend_Search_Lucene_Search_Query_Fuzzy::setDefaultPrefixLength($defaultPrefixLength);
  126. }
  127. public function testQueryParserExceptionsHandling()
  128. {
  129. $this->assertTrue(Zend_Search_Lucene_Search_QueryParser::queryParsingExceptionsSuppressed());
  130. try {
  131. $query = Zend_Search_Lucene_Search_QueryParser::parse('contents:[business TO by}');
  132. } catch (Zend_Search_Lucene_Exception $e) {
  133. $this->fail('exception raised while parsing a query');
  134. }
  135. $this->assertEquals('contents business to by', $query->__toString());
  136. Zend_Search_Lucene_Search_QueryParser::dontSuppressQueryParsingExceptions();
  137. $this->assertFalse(Zend_Search_Lucene_Search_QueryParser::queryParsingExceptionsSuppressed());
  138. try {
  139. $query = Zend_Search_Lucene_Search_QueryParser::parse('contents:[business TO by}');
  140. $this->fail('exception wasn\'t raised while parsing a query');
  141. } catch (Zend_Search_Lucene_Exception $e) {
  142. $this->assertEquals('Syntax error at char position 25.', $e->getMessage());
  143. }
  144. Zend_Search_Lucene_Search_QueryParser::suppressQueryParsingExceptions();
  145. $this->assertTrue(Zend_Search_Lucene_Search_QueryParser::queryParsingExceptionsSuppressed());
  146. }
  147. public function testEmptyQuery()
  148. {
  149. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_index23Sample/_files');
  150. $hits = $index->find('');
  151. $this->assertEquals(count($hits), 0);
  152. }
  153. public function testTermQuery()
  154. {
  155. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_index23Sample/_files');
  156. $hits = $index->find('submitting');
  157. $this->assertEquals(count($hits), 3);
  158. $expectedResultset = array(array(2, 0.114555, 'IndexSource/contributing.patches.html'),
  159. array(7, 0.112241, 'IndexSource/contributing.bugs.html'),
  160. array(8, 0.112241, 'IndexSource/contributing.html'));
  161. foreach ($hits as $resId => $hit) {
  162. $this->assertEquals($hit->id, $expectedResultset[$resId][0]);
  163. $this->assertTrue( abs($hit->score - $expectedResultset[$resId][1]) < 0.000001 );
  164. $this->assertEquals($hit->path, $expectedResultset[$resId][2]);
  165. }
  166. }
  167. public function testMultiTermQuery()
  168. {
  169. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_index23Sample/_files');
  170. $hits = $index->find('submitting AND wishlists');
  171. $this->assertEquals(count($hits), 1);
  172. $this->assertEquals($hits[0]->id, 8);
  173. $this->assertTrue( abs($hits[0]->score - 0.141633) < 0.000001 );
  174. $this->assertEquals($hits[0]->path, 'IndexSource/contributing.html');
  175. }
  176. public function testPraseQuery()
  177. {
  178. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_index23Sample/_files');
  179. $hits = $index->find('"reporting bugs"');
  180. $this->assertEquals(count($hits), 4);
  181. $expectedResultset = array(array(0, 0.247795, 'IndexSource/contributing.documentation.html'),
  182. array(7, 0.212395, 'IndexSource/contributing.bugs.html'),
  183. array(8, 0.212395, 'IndexSource/contributing.html'),
  184. array(2, 0.176996, 'IndexSource/contributing.patches.html'));
  185. foreach ($hits as $resId => $hit) {
  186. $this->assertEquals($hit->id, $expectedResultset[$resId][0]);
  187. $this->assertTrue( abs($hit->score - $expectedResultset[$resId][1]) < 0.000001 );
  188. $this->assertEquals($hit->path, $expectedResultset[$resId][2]);
  189. }
  190. }
  191. public function testQueryParserKeywordsHandlingPhrase()
  192. {
  193. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_index23Sample/_files');
  194. $query = Zend_Search_Lucene_Search_QueryParser::parse('"IndexSource/contributing.wishlist.html" AND Home');
  195. $this->assertEquals($query->__toString(), '+("IndexSource/contributing.wishlist.html") +(Home)');
  196. $this->assertEquals($query->rewrite($index)->__toString(),
  197. '+((pathkeyword:IndexSource/contributing.wishlist.html) (path:"indexsource contributing wishlist html") (modified:"indexsource contributing wishlist html") (contents:"indexsource contributing wishlist html")) +(pathkeyword:home path:home modified:home contents:home)');
  198. $this->assertEquals($query->rewrite($index)->optimize($index)->__toString(), '+( (path:"indexsource contributing wishlist html") (pathkeyword:IndexSource/contributing.wishlist.html)) +(contents:home)');
  199. $hits = $index->find('"IndexSource/contributing.bugs.html"');
  200. $this->assertEquals(count($hits), 1);
  201. $expectedResultset = array(array(7, 1, 'IndexSource/contributing.bugs.html'));
  202. foreach ($hits as $resId => $hit) {
  203. $this->assertEquals($hit->id, $expectedResultset[$resId][0]);
  204. $this->assertTrue( abs($hit->score - $expectedResultset[$resId][1]) < 0.000001 );
  205. $this->assertEquals($hit->path, $expectedResultset[$resId][2]);
  206. }
  207. }
  208. public function testQueryParserKeywordsHandlingTerm()
  209. {
  210. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_index23Sample/_files');
  211. $query = Zend_Search_Lucene_Search_QueryParser::parse('IndexSource\/contributing\.wishlist\.html AND Home');
  212. $this->assertEquals($query->__toString(), '+(IndexSource/contributing.wishlist.html) +(Home)');
  213. $this->assertEquals($query->rewrite($index)->__toString(),
  214. '+(pathkeyword:IndexSource/contributing.wishlist.html path:indexsource path:contributing path:wishlist path:html modified:indexsource modified:contributing modified:wishlist modified:html contents:indexsource contents:contributing contents:wishlist contents:html) +(pathkeyword:home path:home modified:home contents:home)');
  215. $this->assertEquals($query->rewrite($index)->optimize($index)->__toString(), '+(pathkeyword:IndexSource/contributing.wishlist.html path:indexsource path:contributing path:wishlist path:html contents:contributing contents:wishlist contents:html) +(contents:home)');
  216. $hits = $index->find('IndexSource\/contributing\.wishlist\.html AND Home');
  217. $this->assertEquals(count($hits), 9);
  218. $expectedResultset = array(array(1, 1.000000, 'IndexSource/contributing.wishlist.html'),
  219. array(8, 0.167593, 'IndexSource/contributing.html'),
  220. array(0, 0.154047, 'IndexSource/contributing.documentation.html'),
  221. array(7, 0.108574, 'IndexSource/contributing.bugs.html'),
  222. array(2, 0.104248, 'IndexSource/contributing.patches.html'),
  223. array(3, 0.048998, 'IndexSource/about-pear.html'),
  224. array(9, 0.039942, 'IndexSource/core.html'),
  225. array(5, 0.038530, 'IndexSource/authors.html'),
  226. array(4, 0.036261, 'IndexSource/copyright.html'));
  227. foreach ($hits as $resId => $hit) {
  228. $this->assertEquals($hit->id, $expectedResultset[$resId][0]);
  229. $this->assertTrue( abs($hit->score - $expectedResultset[$resId][1]) < 0.000001 );
  230. $this->assertEquals($hit->path, $expectedResultset[$resId][2]);
  231. }
  232. }
  233. public function testBooleanQuery()
  234. {
  235. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_index23Sample/_files');
  236. $hits = $index->find('submitting AND (wishlists OR requirements)');
  237. $this->assertEquals(count($hits), 2);
  238. $expectedResultset = array(array(7, 0.095697, 'IndexSource/contributing.bugs.html'),
  239. array(8, 0.075573, 'IndexSource/contributing.html'));
  240. foreach ($hits as $resId => $hit) {
  241. $this->assertEquals($hit->id, $expectedResultset[$resId][0]);
  242. $this->assertTrue( abs($hit->score - $expectedResultset[$resId][1]) < 0.000001 );
  243. $this->assertEquals($hit->path, $expectedResultset[$resId][2]);
  244. }
  245. }
  246. public function testBooleanQueryWithPhraseSubquery()
  247. {
  248. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_index23Sample/_files');
  249. $hits = $index->find('"PEAR developers" AND Home');
  250. $this->assertEquals(count($hits), 1);
  251. $expectedResultset = array(array(1, 0.168270, 'IndexSource/contributing.wishlist.html'));
  252. foreach ($hits as $resId => $hit) {
  253. $this->assertEquals($hit->id, $expectedResultset[$resId][0]);
  254. $this->assertTrue( abs($hit->score - $expectedResultset[$resId][1]) < 0.000001 );
  255. $this->assertEquals($hit->path, $expectedResultset[$resId][2]);
  256. }
  257. }
  258. public function testBooleanQueryWithNonExistingPhraseSubquery()
  259. {
  260. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_index23Sample/_files');
  261. $query = Zend_Search_Lucene_Search_QueryParser::parse('"Non-existing phrase" AND Home');
  262. $this->assertEquals($query->__toString(), '+("Non-existing phrase") +(Home)');
  263. $this->assertEquals($query->rewrite($index)->__toString(),
  264. '+((pathkeyword:"non existing phrase") (path:"non existing phrase") (modified:"non existing phrase") (contents:"non existing phrase")) +(pathkeyword:home path:home modified:home contents:home)');
  265. $this->assertEquals($query->rewrite($index)->optimize($index)->__toString(), '<EmptyQuery>');
  266. }
  267. public function testFilteredTokensQueryParserProcessing()
  268. {
  269. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_index23Sample/_files');
  270. $this->assertEquals(count(Zend_Search_Lucene_Analysis_Analyzer::getDefault()->tokenize('123456787654321')), 0);
  271. $hits = $index->find('"PEAR developers" AND Home AND 123456787654321');
  272. $this->assertEquals(count($hits), 1);
  273. $expectedResultset = array(array(1, 0.168270, 'IndexSource/contributing.wishlist.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. }
  280. public function testWildcardQuery()
  281. {
  282. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_index23Sample/_files');
  283. $wildcardMinPrefix = Zend_Search_Lucene_Search_Query_Wildcard::getMinPrefixLength();
  284. Zend_Search_Lucene_Search_Query_Wildcard::setMinPrefixLength(0);
  285. $hits = $index->find('*cont*');
  286. $this->assertEquals(count($hits), 9);
  287. $expectedResultset = array(array(8, 0.328087, 'IndexSource/contributing.html'),
  288. array(2, 0.318592, 'IndexSource/contributing.patches.html'),
  289. array(7, 0.260137, 'IndexSource/contributing.bugs.html'),
  290. array(0, 0.203372, 'IndexSource/contributing.documentation.html'),
  291. array(1, 0.202366, 'IndexSource/contributing.wishlist.html'),
  292. array(4, 0.052931, 'IndexSource/copyright.html'),
  293. array(3, 0.017070, 'IndexSource/about-pear.html'),
  294. array(5, 0.010150, 'IndexSource/authors.html'),
  295. array(9, 0.003504, 'IndexSource/core.html'));
  296. foreach ($hits as $resId => $hit) {
  297. $this->assertEquals($hit->id, $expectedResultset[$resId][0]);
  298. $this->assertTrue( abs($hit->score - $expectedResultset[$resId][1]) < 0.000001 );
  299. $this->assertEquals($hit->path, $expectedResultset[$resId][2]);
  300. }
  301. Zend_Search_Lucene_Search_Query_Wildcard::setMinPrefixLength($wildcardMinPrefix);
  302. }
  303. public function testFuzzyQuery()
  304. {
  305. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_index23Sample/_files');
  306. $defaultPrefixLength = Zend_Search_Lucene_Search_Query_Fuzzy::getDefaultPrefixLength();
  307. Zend_Search_Lucene_Search_Query_Fuzzy::setDefaultPrefixLength(0);
  308. $hits = $index->find('tesd~0.4');
  309. $this->assertEquals(count($hits), 9);
  310. $expectedResultset = array(array(2, 0.037139, 'IndexSource/contributing.patches.html'),
  311. array(0, 0.008735, 'IndexSource/contributing.documentation.html'),
  312. array(7, 0.002449, 'IndexSource/contributing.bugs.html'),
  313. array(1, 0.000483, 'IndexSource/contributing.wishlist.html'),
  314. array(3, 0.000483, 'IndexSource/about-pear.html'),
  315. array(9, 0.000483, 'IndexSource/core.html'),
  316. array(5, 0.000414, 'IndexSource/authors.html'),
  317. array(8, 0.000414, 'IndexSource/contributing.html'),
  318. array(4, 0.000345, 'IndexSource/copyright.html'));
  319. foreach ($hits as $resId => $hit) {
  320. $this->assertEquals($hit->id, $expectedResultset[$resId][0]);
  321. $this->assertTrue( abs($hit->score - $expectedResultset[$resId][1]) < 0.000001 );
  322. $this->assertEquals($hit->path, $expectedResultset[$resId][2]);
  323. }
  324. Zend_Search_Lucene_Search_Query_Fuzzy::setDefaultPrefixLength($defaultPrefixLength);
  325. }
  326. public function testInclusiveRangeQuery()
  327. {
  328. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_index23Sample/_files');
  329. $hits = $index->find('[xml TO zzzzz]');
  330. $this->assertEquals(count($hits), 5);
  331. $expectedResultset = array(array(4, 0.156366, 'IndexSource/copyright.html'),
  332. array(2, 0.080458, 'IndexSource/contributing.patches.html'),
  333. array(7, 0.060214, 'IndexSource/contributing.bugs.html'),
  334. array(1, 0.009687, 'IndexSource/contributing.wishlist.html'),
  335. array(5, 0.005871, 'IndexSource/authors.html'));
  336. foreach ($hits as $resId => $hit) {
  337. $this->assertEquals($hit->id, $expectedResultset[$resId][0]);
  338. $this->assertTrue( abs($hit->score - $expectedResultset[$resId][1]) < 0.000001 );
  339. $this->assertEquals($hit->path, $expectedResultset[$resId][2]);
  340. }
  341. }
  342. public function testNonInclusiveRangeQuery()
  343. {
  344. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_index23Sample/_files');
  345. $hits = $index->find('{xml TO zzzzz}');
  346. $this->assertEquals(count($hits), 5);
  347. $expectedResultset = array(array(2, 0.1308671, 'IndexSource/contributing.patches.html'),
  348. array(7, 0.0979391, 'IndexSource/contributing.bugs.html'),
  349. array(4, 0.0633930, 'IndexSource/copyright.html'),
  350. array(1, 0.0157556, 'IndexSource/contributing.wishlist.html'),
  351. array(5, 0.0095493, 'IndexSource/authors.html'));
  352. foreach ($hits as $resId => $hit) {
  353. $this->assertEquals($hit->id, $expectedResultset[$resId][0]);
  354. $this->assertTrue( abs($hit->score - $expectedResultset[$resId][1]) < 0.000001 );
  355. $this->assertEquals($hit->path, $expectedResultset[$resId][2]);
  356. }
  357. }
  358. public function testDefaultSearchField()
  359. {
  360. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_index23Sample/_files');
  361. $storedDefaultSearchField = Zend_Search_Lucene::getDefaultSearchField();
  362. Zend_Search_Lucene::setDefaultSearchField('path');
  363. $hits = $index->find('contributing');
  364. $this->assertEquals(count($hits), 5);
  365. $expectedResultset = array(array(8, 0.847922, 'IndexSource/contributing.html'),
  366. array(0, 0.678337, 'IndexSource/contributing.documentation.html'),
  367. array(1, 0.678337, 'IndexSource/contributing.wishlist.html'),
  368. array(2, 0.678337, 'IndexSource/contributing.patches.html'),
  369. array(7, 0.678337, 'IndexSource/contributing.bugs.html'));
  370. foreach ($hits as $resId => $hit) {
  371. $this->assertEquals($hit->id, $expectedResultset[$resId][0]);
  372. $this->assertTrue( abs($hit->score - $expectedResultset[$resId][1]) < 0.000001 );
  373. $this->assertEquals($hit->path, $expectedResultset[$resId][2]);
  374. }
  375. Zend_Search_Lucene::setDefaultSearchField($storedDefaultSearchField);
  376. }
  377. public function testQueryHit()
  378. {
  379. // Restore default search field if it wasn't done by previous test because of failure
  380. Zend_Search_Lucene::setDefaultSearchField(null);
  381. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_index23Sample/_files');
  382. $hits = $index->find('submitting AND wishlists');
  383. $hit = $hits[0];
  384. $this->assertTrue($hit instanceof Zend_Search_Lucene_Search_QueryHit);
  385. $this->assertTrue($hit->getIndex() instanceof Zend_Search_Lucene_Interface);
  386. $doc = $hit->getDocument();
  387. $this->assertTrue($doc instanceof Zend_Search_Lucene_Document);
  388. $this->assertEquals($doc->path, 'IndexSource/contributing.html');
  389. }
  390. public function testDelayedResourceCleanUp()
  391. {
  392. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_index23Sample/_files');
  393. $hits = $index->find('submitting AND wishlists');
  394. unset($index);
  395. $hit = $hits[0];
  396. $this->assertTrue($hit instanceof Zend_Search_Lucene_Search_QueryHit);
  397. $this->assertTrue($hit->getIndex() instanceof Zend_Search_Lucene_Interface);
  398. $doc = $hit->getDocument();
  399. $this->assertTrue($doc instanceof Zend_Search_Lucene_Document);
  400. $this->assertTrue($hit->getIndex() instanceof Zend_Search_Lucene_Interface);
  401. $this->assertEquals($doc->path, 'IndexSource/contributing.html');
  402. }
  403. public function testSortingResult()
  404. {
  405. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_index23Sample/_files');
  406. $hits = $index->find('"reporting bugs"', 'path');
  407. $this->assertEquals(count($hits), 4);
  408. $expectedResultset = array(array(7, 0.212395, 'IndexSource/contributing.bugs.html'),
  409. array(0, 0.247795, 'IndexSource/contributing.documentation.html'),
  410. array(8, 0.212395, 'IndexSource/contributing.html'),
  411. array(2, 0.176996, 'IndexSource/contributing.patches.html'));
  412. foreach ($hits as $resId => $hit) {
  413. $this->assertEquals($hit->id, $expectedResultset[$resId][0]);
  414. $this->assertTrue( abs($hit->score - $expectedResultset[$resId][1]) < 0.000001 );
  415. $this->assertEquals($hit->path, $expectedResultset[$resId][2]);
  416. }
  417. }
  418. public function testSortingResultByScore()
  419. {
  420. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_index23Sample/_files');
  421. $hits = $index->find('"reporting bugs"', 'score', SORT_NUMERIC, SORT_ASC,
  422. 'path', SORT_STRING, SORT_ASC);
  423. $this->assertEquals(count($hits), 4);
  424. $expectedResultset = array(array(2, 0.176996, 'IndexSource/contributing.patches.html'),
  425. array(7, 0.212395, 'IndexSource/contributing.bugs.html'),
  426. array(8, 0.212395, 'IndexSource/contributing.html'),
  427. array(0, 0.247795, 'IndexSource/contributing.documentation.html'));
  428. foreach ($hits as $resId => $hit) {
  429. $this->assertEquals($hit->id, $expectedResultset[$resId][0]);
  430. $this->assertTrue( abs($hit->score - $expectedResultset[$resId][1]) < 0.000001 );
  431. $this->assertEquals($hit->path, $expectedResultset[$resId][2]);
  432. }
  433. $hits = $index->find('"reporting bugs"', 'score', SORT_NUMERIC, SORT_ASC,
  434. 'path', SORT_STRING, SORT_DESC);
  435. $this->assertEquals(count($hits), 4);
  436. $expectedResultset = array(array(2, 0.176996, 'IndexSource/contributing.patches.html'),
  437. array(8, 0.212395, 'IndexSource/contributing.html'),
  438. array(7, 0.212395, 'IndexSource/contributing.bugs.html'),
  439. array(0, 0.247795, 'IndexSource/contributing.documentation.html'));
  440. foreach ($hits as $resId => $hit) {
  441. $this->assertEquals($hit->id, $expectedResultset[$resId][0]);
  442. $this->assertTrue( abs($hit->score - $expectedResultset[$resId][1]) < 0.000001 );
  443. $this->assertEquals($hit->path, $expectedResultset[$resId][2]);
  444. }
  445. }
  446. public function testLimitingResult()
  447. {
  448. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_index23Sample/_files');
  449. $storedResultSetLimit = Zend_Search_Lucene::getResultSetLimit();
  450. Zend_Search_Lucene::setResultSetLimit(3);
  451. $hits = $index->find('"reporting bugs"', 'path');
  452. $this->assertEquals(count($hits), 3);
  453. $expectedResultset = array(array(7, 0.212395, 'IndexSource/contributing.bugs.html'),
  454. array(0, 0.247795, 'IndexSource/contributing.documentation.html'),
  455. array(2, 0.176996, 'IndexSource/contributing.patches.html'));
  456. foreach ($hits as $resId => $hit) {
  457. $this->assertEquals($hit->id, $expectedResultset[$resId][0]);
  458. $this->assertTrue( abs($hit->score - $expectedResultset[$resId][1]) < 0.000001 );
  459. $this->assertEquals($hit->path, $expectedResultset[$resId][2]);
  460. }
  461. Zend_Search_Lucene::setResultSetLimit($storedResultSetLimit);
  462. }
  463. }