Search23Test.php 29 KB

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