Search23Test.php 31 KB

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