Lucene.php 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521
  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. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /** Zend_Search_Lucene_Document */
  22. require_once 'Zend/Search/Lucene/Document.php';
  23. /** Zend_Search_Lucene_Document_Html */
  24. require_once 'Zend/Search/Lucene/Document/Html.php';
  25. /** Zend_Search_Lucene_Document_Docx */
  26. require_once 'Zend/Search/Lucene/Document/Docx.php';
  27. /** Zend_Search_Lucene_Document_Pptx */
  28. require_once 'Zend/Search/Lucene/Document/Pptx.php';
  29. /** Zend_Search_Lucene_Document_Xlsx */
  30. require_once 'Zend/Search/Lucene/Document/Xlsx.php';
  31. /** Zend_Search_Lucene_Storage_Directory_Filesystem */
  32. require_once 'Zend/Search/Lucene/Storage/Directory/Filesystem.php';
  33. /** Zend_Search_Lucene_Storage_File_Memory */
  34. require_once 'Zend/Search/Lucene/Storage/File/Memory.php';
  35. /** Zend_Search_Lucene_Index_Term */
  36. require_once 'Zend/Search/Lucene/Index/Term.php';
  37. /** Zend_Search_Lucene_Index_TermInfo */
  38. require_once 'Zend/Search/Lucene/Index/TermInfo.php';
  39. /** Zend_Search_Lucene_Index_SegmentInfo */
  40. require_once 'Zend/Search/Lucene/Index/SegmentInfo.php';
  41. /** Zend_Search_Lucene_Index_FieldInfo */
  42. require_once 'Zend/Search/Lucene/Index/FieldInfo.php';
  43. /** Zend_Search_Lucene_Index_Writer */
  44. require_once 'Zend/Search/Lucene/Index/Writer.php';
  45. /** Zend_Search_Lucene_Search_QueryParser */
  46. require_once 'Zend/Search/Lucene/Search/QueryParser.php';
  47. /** Zend_Search_Lucene_Search_QueryHit */
  48. require_once 'Zend/Search/Lucene/Search/QueryHit.php';
  49. /** Zend_Search_Lucene_Search_Similarity */
  50. require_once 'Zend/Search/Lucene/Search/Similarity.php';
  51. /** Zend_Search_Lucene_Index_TermsPriorityQueue */
  52. require_once 'Zend/Search/Lucene/Index/TermsPriorityQueue.php';
  53. /** Zend_Search_Lucene_TermStreamsPriorityQueue */
  54. require_once 'Zend/Search/Lucene/TermStreamsPriorityQueue.php';
  55. /** Zend_Search_Lucene_Index_DocsFilter */
  56. require_once 'Zend/Search/Lucene/Index/DocsFilter.php';
  57. /** Zend_Search_Lucene_LockManager */
  58. require_once 'Zend/Search/Lucene/LockManager.php';
  59. /** Zend_Search_Lucene_Interface */
  60. require_once 'Zend/Search/Lucene/Interface.php';
  61. /** Zend_Search_Lucene_Proxy */
  62. require_once 'Zend/Search/Lucene/Proxy.php';
  63. /**
  64. * @category Zend
  65. * @package Zend_Search_Lucene
  66. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  67. * @license http://framework.zend.com/license/new-bsd New BSD License
  68. */
  69. class Zend_Search_Lucene implements Zend_Search_Lucene_Interface
  70. {
  71. /**
  72. * Default field name for search
  73. *
  74. * Null means search through all fields
  75. *
  76. * @var string
  77. */
  78. private static $_defaultSearchField = null;
  79. /**
  80. * Result set limit
  81. *
  82. * 0 means no limit
  83. *
  84. * @var integer
  85. */
  86. private static $_resultSetLimit = 0;
  87. /**
  88. * Terms per query limit
  89. *
  90. * 0 means no limit
  91. *
  92. * @var integer
  93. */
  94. private static $_termsPerQueryLimit = 1024;
  95. /**
  96. * File system adapter.
  97. *
  98. * @var Zend_Search_Lucene_Storage_Directory
  99. */
  100. private $_directory = null;
  101. /**
  102. * File system adapter closing option
  103. *
  104. * @var boolean
  105. */
  106. private $_closeDirOnExit = true;
  107. /**
  108. * Writer for this index, not instantiated unless required.
  109. *
  110. * @var Zend_Search_Lucene_Index_Writer
  111. */
  112. private $_writer = null;
  113. /**
  114. * Array of Zend_Search_Lucene_Index_SegmentInfo objects for this index.
  115. *
  116. * @var array Zend_Search_Lucene_Index_SegmentInfo
  117. */
  118. private $_segmentInfos = array();
  119. /**
  120. * Number of documents in this index.
  121. *
  122. * @var integer
  123. */
  124. private $_docCount = 0;
  125. /**
  126. * Flag for index changes
  127. *
  128. * @var boolean
  129. */
  130. private $_hasChanges = false;
  131. /**
  132. * Signal, that index is already closed, changes are fixed and resources are cleaned up
  133. *
  134. * @var boolean
  135. */
  136. private $_closed = false;
  137. /**
  138. * Number of references to the index object
  139. *
  140. * @var integer
  141. */
  142. private $_refCount = 0;
  143. /**
  144. * Current segment generation
  145. *
  146. * @var integer
  147. */
  148. private $_generation;
  149. const FORMAT_PRE_2_1 = 0;
  150. const FORMAT_2_1 = 1;
  151. const FORMAT_2_3 = 2;
  152. /**
  153. * Index format version
  154. *
  155. * @var integer
  156. */
  157. private $_formatVersion;
  158. /**
  159. * Create index
  160. *
  161. * @param mixed $directory
  162. * @return Zend_Search_Lucene_Interface
  163. */
  164. public static function create($directory)
  165. {
  166. return new Zend_Search_Lucene_Proxy(new Zend_Search_Lucene($directory, true));
  167. }
  168. /**
  169. * Open index
  170. *
  171. * @param mixed $directory
  172. * @return Zend_Search_Lucene_Interface
  173. */
  174. public static function open($directory)
  175. {
  176. return new Zend_Search_Lucene_Proxy(new Zend_Search_Lucene($directory, false));
  177. }
  178. /** Generation retrieving counter */
  179. const GENERATION_RETRIEVE_COUNT = 10;
  180. /** Pause between generation retrieving attempts in milliseconds */
  181. const GENERATION_RETRIEVE_PAUSE = 50;
  182. /**
  183. * Get current generation number
  184. *
  185. * Returns generation number
  186. * 0 means pre-2.1 index format
  187. * -1 means there are no segments files.
  188. *
  189. * @param Zend_Search_Lucene_Storage_Directory $directory
  190. * @return integer
  191. * @throws Zend_Search_Lucene_Exception
  192. */
  193. public static function getActualGeneration(Zend_Search_Lucene_Storage_Directory $directory)
  194. {
  195. /**
  196. * Zend_Search_Lucene uses segments.gen file to retrieve current generation number
  197. *
  198. * Apache Lucene index format documentation mentions this method only as a fallback method
  199. *
  200. * Nevertheless we use it according to the performance considerations
  201. *
  202. * @todo check if we can use some modification of Apache Lucene generation determination algorithm
  203. * without performance problems
  204. */
  205. require_once 'Zend/Search/Lucene/Exception.php';
  206. try {
  207. for ($count = 0; $count < self::GENERATION_RETRIEVE_COUNT; $count++) {
  208. // Try to get generation file
  209. $genFile = $directory->getFileObject('segments.gen', false);
  210. $format = $genFile->readInt();
  211. if ($format != (int)0xFFFFFFFE) {
  212. throw new Zend_Search_Lucene_Exception('Wrong segments.gen file format');
  213. }
  214. $gen1 = $genFile->readLong();
  215. $gen2 = $genFile->readLong();
  216. if ($gen1 == $gen2) {
  217. return $gen1;
  218. }
  219. usleep(self::GENERATION_RETRIEVE_PAUSE * 1000);
  220. }
  221. // All passes are failed
  222. throw new Zend_Search_Lucene_Exception('Index is under processing now');
  223. } catch (Zend_Search_Lucene_Exception $e) {
  224. if (strpos($e->getMessage(), 'is not readable') !== false) {
  225. try {
  226. // Try to open old style segments file
  227. $segmentsFile = $directory->getFileObject('segments', false);
  228. // It's pre-2.1 index
  229. return 0;
  230. } catch (Zend_Search_Lucene_Exception $e) {
  231. if (strpos($e->getMessage(), 'is not readable') !== false) {
  232. return -1;
  233. } else {
  234. throw $e;
  235. }
  236. }
  237. } else {
  238. throw $e;
  239. }
  240. }
  241. return -1;
  242. }
  243. /**
  244. * Get segments file name
  245. *
  246. * @param integer $generation
  247. * @return string
  248. */
  249. public static function getSegmentFileName($generation)
  250. {
  251. if ($generation == 0) {
  252. return 'segments';
  253. }
  254. return 'segments_' . base_convert($generation, 10, 36);
  255. }
  256. /**
  257. * Get index format version
  258. *
  259. * @return integer
  260. */
  261. public function getFormatVersion()
  262. {
  263. return $this->_formatVersion;
  264. }
  265. /**
  266. * Set index format version.
  267. * Index is converted to this format at the nearest upfdate time
  268. *
  269. * @param int $formatVersion
  270. * @throws Zend_Search_Lucene_Exception
  271. */
  272. public function setFormatVersion($formatVersion)
  273. {
  274. if ($formatVersion != self::FORMAT_PRE_2_1 &&
  275. $formatVersion != self::FORMAT_2_1 &&
  276. $formatVersion != self::FORMAT_2_3) {
  277. require_once 'Zend/Search/Lucene/Exception.php';
  278. throw new Zend_Search_Lucene_Exception('Unsupported index format');
  279. }
  280. $this->_formatVersion = $formatVersion;
  281. }
  282. /**
  283. * Read segments file for pre-2.1 Lucene index format
  284. *
  285. * @throws Zend_Search_Lucene_Exception
  286. */
  287. private function _readPre21SegmentsFile()
  288. {
  289. $segmentsFile = $this->_directory->getFileObject('segments');
  290. $format = $segmentsFile->readInt();
  291. if ($format != (int)0xFFFFFFFF) {
  292. require_once 'Zend/Search/Lucene/Exception.php';
  293. throw new Zend_Search_Lucene_Exception('Wrong segments file format');
  294. }
  295. // read version
  296. $segmentsFile->readLong();
  297. // read segment name counter
  298. $segmentsFile->readInt();
  299. $segments = $segmentsFile->readInt();
  300. $this->_docCount = 0;
  301. // read segmentInfos
  302. for ($count = 0; $count < $segments; $count++) {
  303. $segName = $segmentsFile->readString();
  304. $segSize = $segmentsFile->readInt();
  305. $this->_docCount += $segSize;
  306. $this->_segmentInfos[$segName] =
  307. new Zend_Search_Lucene_Index_SegmentInfo($this->_directory,
  308. $segName,
  309. $segSize);
  310. }
  311. // Use 2.1 as a target version. Index will be reorganized at update time.
  312. $this->_formatVersion = self::FORMAT_2_1;
  313. }
  314. /**
  315. * Read segments file
  316. *
  317. * @throws Zend_Search_Lucene_Exception
  318. */
  319. private function _readSegmentsFile()
  320. {
  321. $segmentsFile = $this->_directory->getFileObject(self::getSegmentFileName($this->_generation));
  322. $format = $segmentsFile->readInt();
  323. if ($format == (int)0xFFFFFFFC) {
  324. $this->_formatVersion = self::FORMAT_2_3;
  325. } else if ($format == (int)0xFFFFFFFD) {
  326. $this->_formatVersion = self::FORMAT_2_1;
  327. } else {
  328. require_once 'Zend/Search/Lucene/Exception.php';
  329. throw new Zend_Search_Lucene_Exception('Unsupported segments file format');
  330. }
  331. // read version
  332. $segmentsFile->readLong();
  333. // read segment name counter
  334. $segmentsFile->readInt();
  335. $segments = $segmentsFile->readInt();
  336. $this->_docCount = 0;
  337. // read segmentInfos
  338. for ($count = 0; $count < $segments; $count++) {
  339. $segName = $segmentsFile->readString();
  340. $segSize = $segmentsFile->readInt();
  341. // 2.1+ specific properties
  342. $delGen = $segmentsFile->readLong();
  343. if ($this->_formatVersion == self::FORMAT_2_3) {
  344. $docStoreOffset = $segmentsFile->readInt();
  345. if ($docStoreOffset != (int)0xFFFFFFFF) {
  346. $docStoreSegment = $segmentsFile->readString();
  347. $docStoreIsCompoundFile = $segmentsFile->readByte();
  348. $docStoreOptions = array('offset' => $docStoreOffset,
  349. 'segment' => $docStoreSegment,
  350. 'isCompound' => ($docStoreIsCompoundFile == 1));
  351. } else {
  352. $docStoreOptions = null;
  353. }
  354. } else {
  355. $docStoreOptions = null;
  356. }
  357. $hasSingleNormFile = $segmentsFile->readByte();
  358. $numField = $segmentsFile->readInt();
  359. $normGens = array();
  360. if ($numField != (int)0xFFFFFFFF) {
  361. for ($count1 = 0; $count1 < $numField; $count1++) {
  362. $normGens[] = $segmentsFile->readLong();
  363. }
  364. require_once 'Zend/Search/Lucene/Exception.php';
  365. throw new Zend_Search_Lucene_Exception('Separate norm files are not supported. Optimize index to use it with Zend_Search_Lucene.');
  366. }
  367. $isCompoundByte = $segmentsFile->readByte();
  368. if ($isCompoundByte == 0xFF) {
  369. // The segment is not a compound file
  370. $isCompound = false;
  371. } else if ($isCompoundByte == 0x00) {
  372. // The status is unknown
  373. $isCompound = null;
  374. } else if ($isCompoundByte == 0x01) {
  375. // The segment is a compound file
  376. $isCompound = true;
  377. }
  378. $this->_docCount += $segSize;
  379. $this->_segmentInfos[$segName] =
  380. new Zend_Search_Lucene_Index_SegmentInfo($this->_directory,
  381. $segName,
  382. $segSize,
  383. $delGen,
  384. $docStoreOptions,
  385. $hasSingleNormFile,
  386. $isCompound);
  387. }
  388. }
  389. /**
  390. * Opens the index.
  391. *
  392. * IndexReader constructor needs Directory as a parameter. It should be
  393. * a string with a path to the index folder or a Directory object.
  394. *
  395. * @param mixed $directory
  396. * @throws Zend_Search_Lucene_Exception
  397. */
  398. public function __construct($directory = null, $create = false)
  399. {
  400. if ($directory === null) {
  401. require_once 'Zend/Search/Lucene/Exception.php';
  402. throw new Zend_Search_Exception('No index directory specified');
  403. }
  404. if ($directory instanceof Zend_Search_Lucene_Storage_Directory_Filesystem) {
  405. $this->_directory = $directory;
  406. $this->_closeDirOnExit = false;
  407. } else {
  408. $this->_directory = new Zend_Search_Lucene_Storage_Directory_Filesystem($directory);
  409. $this->_closeDirOnExit = true;
  410. }
  411. $this->_segmentInfos = array();
  412. // Mark index as "under processing" to prevent other processes from premature index cleaning
  413. Zend_Search_Lucene_LockManager::obtainReadLock($this->_directory);
  414. $this->_generation = self::getActualGeneration($this->_directory);
  415. if ($create) {
  416. require_once 'Zend/Search/Lucene/Exception.php';
  417. try {
  418. Zend_Search_Lucene_LockManager::obtainWriteLock($this->_directory);
  419. } catch (Zend_Search_Lucene_Exception $e) {
  420. Zend_Search_Lucene_LockManager::releaseReadLock($this->_directory);
  421. if (strpos($e->getMessage(), 'Can\'t obtain exclusive index lock') === false) {
  422. throw $e;
  423. } else {
  424. throw new Zend_Search_Lucene_Exception('Can\'t create index. It\'s under processing now');
  425. }
  426. }
  427. if ($this->_generation == -1) {
  428. // Directory doesn't contain existing index, start from 1
  429. $this->_generation = 1;
  430. $nameCounter = 0;
  431. } else {
  432. // Directory contains existing index
  433. $segmentsFile = $this->_directory->getFileObject(self::getSegmentFileName($this->_generation));
  434. $segmentsFile->seek(12); // 12 = 4 (int, file format marker) + 8 (long, index version)
  435. $nameCounter = $segmentsFile->readInt();
  436. $this->_generation++;
  437. }
  438. Zend_Search_Lucene_Index_Writer::createIndex($this->_directory, $this->_generation, $nameCounter);
  439. Zend_Search_Lucene_LockManager::releaseWriteLock($this->_directory);
  440. }
  441. if ($this->_generation == -1) {
  442. require_once 'Zend/Search/Lucene/Exception.php';
  443. throw new Zend_Search_Lucene_Exception('Index doesn\'t exists in the specified directory.');
  444. } else if ($this->_generation == 0) {
  445. $this->_readPre21SegmentsFile();
  446. } else {
  447. $this->_readSegmentsFile();
  448. }
  449. }
  450. /**
  451. * Close current index and free resources
  452. */
  453. private function _close()
  454. {
  455. if ($this->_closed) {
  456. // index is already closed and resources are cleaned up
  457. return;
  458. }
  459. $this->commit();
  460. // Release "under processing" flag
  461. Zend_Search_Lucene_LockManager::releaseReadLock($this->_directory);
  462. if ($this->_closeDirOnExit) {
  463. $this->_directory->close();
  464. }
  465. $this->_directory = null;
  466. $this->_writer = null;
  467. $this->_segmentInfos = null;
  468. $this->_closed = true;
  469. }
  470. /**
  471. * Add reference to the index object
  472. *
  473. * @internal
  474. */
  475. public function addReference()
  476. {
  477. $this->_refCount++;
  478. }
  479. /**
  480. * Remove reference from the index object
  481. *
  482. * When reference count becomes zero, index is closed and resources are cleaned up
  483. *
  484. * @internal
  485. */
  486. public function removeReference()
  487. {
  488. $this->_refCount--;
  489. if ($this->_refCount == 0) {
  490. $this->_close();
  491. }
  492. }
  493. /**
  494. * Object destructor
  495. */
  496. public function __destruct()
  497. {
  498. $this->_close();
  499. }
  500. /**
  501. * Returns an instance of Zend_Search_Lucene_Index_Writer for the index
  502. *
  503. * @return Zend_Search_Lucene_Index_Writer
  504. */
  505. private function _getIndexWriter()
  506. {
  507. if (!$this->_writer instanceof Zend_Search_Lucene_Index_Writer) {
  508. $this->_writer = new Zend_Search_Lucene_Index_Writer($this->_directory, $this->_segmentInfos, $this->_formatVersion);
  509. }
  510. return $this->_writer;
  511. }
  512. /**
  513. * Returns the Zend_Search_Lucene_Storage_Directory instance for this index.
  514. *
  515. * @return Zend_Search_Lucene_Storage_Directory
  516. */
  517. public function getDirectory()
  518. {
  519. return $this->_directory;
  520. }
  521. /**
  522. * Returns the total number of documents in this index (including deleted documents).
  523. *
  524. * @return integer
  525. */
  526. public function count()
  527. {
  528. return $this->_docCount;
  529. }
  530. /**
  531. * Returns one greater than the largest possible document number.
  532. * This may be used to, e.g., determine how big to allocate a structure which will have
  533. * an element for every document number in an index.
  534. *
  535. * @return integer
  536. */
  537. public function maxDoc()
  538. {
  539. return $this->count();
  540. }
  541. /**
  542. * Returns the total number of non-deleted documents in this index.
  543. *
  544. * @return integer
  545. */
  546. public function numDocs()
  547. {
  548. $numDocs = 0;
  549. foreach ($this->_segmentInfos as $segmentInfo) {
  550. $numDocs += $segmentInfo->numDocs();
  551. }
  552. return $numDocs;
  553. }
  554. /**
  555. * Checks, that document is deleted
  556. *
  557. * @param integer $id
  558. * @return boolean
  559. * @throws Zend_Search_Lucene_Exception Exception is thrown if $id is out of the range
  560. */
  561. public function isDeleted($id)
  562. {
  563. if ($id >= $this->_docCount) {
  564. require_once 'Zend/Search/Lucene/Exception.php';
  565. throw new Zend_Search_Lucene_Exception('Document id is out of the range.');
  566. }
  567. $segmentStartId = 0;
  568. foreach ($this->_segmentInfos as $segmentInfo) {
  569. if ($segmentStartId + $segmentInfo->count() > $id) {
  570. break;
  571. }
  572. $segmentStartId += $segmentInfo->count();
  573. }
  574. return $segmentInfo->isDeleted($id - $segmentStartId);
  575. }
  576. /**
  577. * Set default search field.
  578. *
  579. * Null means, that search is performed through all fields by default
  580. *
  581. * Default value is null
  582. *
  583. * @param string $fieldName
  584. */
  585. public static function setDefaultSearchField($fieldName)
  586. {
  587. self::$_defaultSearchField = $fieldName;
  588. }
  589. /**
  590. * Get default search field.
  591. *
  592. * Null means, that search is performed through all fields by default
  593. *
  594. * @return string
  595. */
  596. public static function getDefaultSearchField()
  597. {
  598. return self::$_defaultSearchField;
  599. }
  600. /**
  601. * Set result set limit.
  602. *
  603. * 0 (default) means no limit
  604. *
  605. * @param integer $limit
  606. */
  607. public static function setResultSetLimit($limit)
  608. {
  609. self::$_resultSetLimit = $limit;
  610. }
  611. /**
  612. * Get result set limit.
  613. *
  614. * 0 means no limit
  615. *
  616. * @return integer
  617. */
  618. public static function getResultSetLimit()
  619. {
  620. return self::$_resultSetLimit;
  621. }
  622. /**
  623. * Set terms per query limit.
  624. *
  625. * 0 means no limit
  626. *
  627. * @param integer $limit
  628. */
  629. public static function setTermsPerQueryLimit($limit)
  630. {
  631. self::$_termsPerQueryLimit = $limit;
  632. }
  633. /**
  634. * Get result set limit.
  635. *
  636. * 0 (default) means no limit
  637. *
  638. * @return integer
  639. */
  640. public static function getTermsPerQueryLimit()
  641. {
  642. return self::$_termsPerQueryLimit;
  643. }
  644. /**
  645. * Retrieve index maxBufferedDocs option
  646. *
  647. * maxBufferedDocs is a minimal number of documents required before
  648. * the buffered in-memory documents are written into a new Segment
  649. *
  650. * Default value is 10
  651. *
  652. * @return integer
  653. */
  654. public function getMaxBufferedDocs()
  655. {
  656. return $this->_getIndexWriter()->maxBufferedDocs;
  657. }
  658. /**
  659. * Set index maxBufferedDocs option
  660. *
  661. * maxBufferedDocs is a minimal number of documents required before
  662. * the buffered in-memory documents are written into a new Segment
  663. *
  664. * Default value is 10
  665. *
  666. * @param integer $maxBufferedDocs
  667. */
  668. public function setMaxBufferedDocs($maxBufferedDocs)
  669. {
  670. $this->_getIndexWriter()->maxBufferedDocs = $maxBufferedDocs;
  671. }
  672. /**
  673. * Retrieve index maxMergeDocs option
  674. *
  675. * maxMergeDocs is a largest number of documents ever merged by addDocument().
  676. * Small values (e.g., less than 10,000) are best for interactive indexing,
  677. * as this limits the length of pauses while indexing to a few seconds.
  678. * Larger values are best for batched indexing and speedier searches.
  679. *
  680. * Default value is PHP_INT_MAX
  681. *
  682. * @return integer
  683. */
  684. public function getMaxMergeDocs()
  685. {
  686. return $this->_getIndexWriter()->maxMergeDocs;
  687. }
  688. /**
  689. * Set index maxMergeDocs option
  690. *
  691. * maxMergeDocs is a largest number of documents ever merged by addDocument().
  692. * Small values (e.g., less than 10,000) are best for interactive indexing,
  693. * as this limits the length of pauses while indexing to a few seconds.
  694. * Larger values are best for batched indexing and speedier searches.
  695. *
  696. * Default value is PHP_INT_MAX
  697. *
  698. * @param integer $maxMergeDocs
  699. */
  700. public function setMaxMergeDocs($maxMergeDocs)
  701. {
  702. $this->_getIndexWriter()->maxMergeDocs = $maxMergeDocs;
  703. }
  704. /**
  705. * Retrieve index mergeFactor option
  706. *
  707. * mergeFactor determines how often segment indices are merged by addDocument().
  708. * With smaller values, less RAM is used while indexing,
  709. * and searches on unoptimized indices are faster,
  710. * but indexing speed is slower.
  711. * With larger values, more RAM is used during indexing,
  712. * and while searches on unoptimized indices are slower,
  713. * indexing is faster.
  714. * Thus larger values (> 10) are best for batch index creation,
  715. * and smaller values (< 10) for indices that are interactively maintained.
  716. *
  717. * Default value is 10
  718. *
  719. * @return integer
  720. */
  721. public function getMergeFactor()
  722. {
  723. return $this->_getIndexWriter()->mergeFactor;
  724. }
  725. /**
  726. * Set index mergeFactor option
  727. *
  728. * mergeFactor determines how often segment indices are merged by addDocument().
  729. * With smaller values, less RAM is used while indexing,
  730. * and searches on unoptimized indices are faster,
  731. * but indexing speed is slower.
  732. * With larger values, more RAM is used during indexing,
  733. * and while searches on unoptimized indices are slower,
  734. * indexing is faster.
  735. * Thus larger values (> 10) are best for batch index creation,
  736. * and smaller values (< 10) for indices that are interactively maintained.
  737. *
  738. * Default value is 10
  739. *
  740. * @param integer $maxMergeDocs
  741. */
  742. public function setMergeFactor($mergeFactor)
  743. {
  744. $this->_getIndexWriter()->mergeFactor = $mergeFactor;
  745. }
  746. /**
  747. * Performs a query against the index and returns an array
  748. * of Zend_Search_Lucene_Search_QueryHit objects.
  749. * Input is a string or Zend_Search_Lucene_Search_Query.
  750. *
  751. * @param mixed $query
  752. * @return array Zend_Search_Lucene_Search_QueryHit
  753. * @throws Zend_Search_Lucene_Exception
  754. */
  755. public function find($query)
  756. {
  757. if (is_string($query)) {
  758. $query = Zend_Search_Lucene_Search_QueryParser::parse($query);
  759. }
  760. if (!$query instanceof Zend_Search_Lucene_Search_Query) {
  761. require_once 'Zend/Search/Lucene/Exception.php';
  762. throw new Zend_Search_Lucene_Exception('Query must be a string or Zend_Search_Lucene_Search_Query object');
  763. }
  764. $this->commit();
  765. $hits = array();
  766. $scores = array();
  767. $ids = array();
  768. $query = $query->rewrite($this)->optimize($this);
  769. $query->execute($this);
  770. $topScore = 0;
  771. foreach ($query->matchedDocs() as $id => $num) {
  772. $docScore = $query->score($id, $this);
  773. if( $docScore != 0 ) {
  774. $hit = new Zend_Search_Lucene_Search_QueryHit($this);
  775. $hit->id = $id;
  776. $hit->score = $docScore;
  777. $hits[] = $hit;
  778. $ids[] = $id;
  779. $scores[] = $docScore;
  780. if ($docScore > $topScore) {
  781. $topScore = $docScore;
  782. }
  783. }
  784. if (self::$_resultSetLimit != 0 && count($hits) >= self::$_resultSetLimit) {
  785. break;
  786. }
  787. }
  788. if (count($hits) == 0) {
  789. // skip sorting, which may cause a error on empty index
  790. return array();
  791. }
  792. if ($topScore > 1) {
  793. foreach ($hits as $hit) {
  794. $hit->score /= $topScore;
  795. }
  796. }
  797. if (func_num_args() == 1) {
  798. // sort by scores
  799. array_multisort($scores, SORT_DESC, SORT_NUMERIC,
  800. $ids, SORT_ASC, SORT_NUMERIC,
  801. $hits);
  802. } else {
  803. // sort by given field names
  804. $argList = func_get_args();
  805. $fieldNames = $this->getFieldNames();
  806. $sortArgs = array();
  807. // PHP 5.3 now expects all arguments to array_multisort be passed by
  808. // reference (if it's invoked through call_user_func_array());
  809. // since constants can't be passed by reference, create some placeholder variables.
  810. $sortReg = SORT_REGULAR;
  811. $sortAsc = SORT_ASC;
  812. $sortNum = SORT_NUMERIC;
  813. require_once 'Zend/Search/Lucene/Exception.php';
  814. for ($count = 1; $count < count($argList); $count++) {
  815. $fieldName = $argList[$count];
  816. if (!is_string($fieldName)) {
  817. throw new Zend_Search_Lucene_Exception('Field name must be a string.');
  818. }
  819. if (!in_array($fieldName, $fieldNames)) {
  820. throw new Zend_Search_Lucene_Exception('Wrong field name.');
  821. }
  822. $valuesArray = array();
  823. foreach ($hits as $hit) {
  824. try {
  825. $value = $hit->getDocument()->getFieldValue($fieldName);
  826. } catch (Zend_Search_Lucene_Exception $e) {
  827. if (strpos($e->getMessage(), 'not found') === false) {
  828. throw $e;
  829. } else {
  830. $value = null;
  831. }
  832. }
  833. $valuesArray[] = $value;
  834. }
  835. $sortArgs[] = &$valuesArray;
  836. unset($valuesArray);
  837. if ($count + 1 < count($argList) && is_integer($argList[$count+1])) {
  838. $count++;
  839. $sortArgs[] = &$argList[$count];
  840. if ($count + 1 < count($argList) && is_integer($argList[$count+1])) {
  841. $count++;
  842. $sortArgs[] = &$argList[$count];
  843. } else {
  844. if ($argList[$count] == SORT_ASC || $argList[$count] == SORT_DESC) {
  845. $sortArgs[] = &$sortReg;
  846. } else {
  847. $sortArgs[] = &$sortAsc;
  848. }
  849. }
  850. } else {
  851. $sortArgs[] = &$sortAsc;
  852. $sortArgs[] = &$sortReg;
  853. }
  854. }
  855. // Sort by id's if values are equal
  856. $sortArgs[] = &$ids;
  857. $sortArgs[] = &$sortAsc;
  858. $sortArgs[] = &$sortNum;
  859. // Array to be sorted
  860. $sortArgs[] = &$hits;
  861. // Do sort
  862. call_user_func_array('array_multisort', $sortArgs);
  863. }
  864. return $hits;
  865. }
  866. /**
  867. * Returns a list of all unique field names that exist in this index.
  868. *
  869. * @param boolean $indexed
  870. * @return array
  871. */
  872. public function getFieldNames($indexed = false)
  873. {
  874. $result = array();
  875. foreach( $this->_segmentInfos as $segmentInfo ) {
  876. $result = array_merge($result, $segmentInfo->getFields($indexed));
  877. }
  878. return $result;
  879. }
  880. /**
  881. * Returns a Zend_Search_Lucene_Document object for the document
  882. * number $id in this index.
  883. *
  884. * @param integer|Zend_Search_Lucene_Search_QueryHit $id
  885. * @return Zend_Search_Lucene_Document
  886. * @throws Zend_Search_Lucene_Exception Exception is thrown if $id is out of the range
  887. */
  888. public function getDocument($id)
  889. {
  890. if ($id instanceof Zend_Search_Lucene_Search_QueryHit) {
  891. /* @var $id Zend_Search_Lucene_Search_QueryHit */
  892. $id = $id->id;
  893. }
  894. if ($id >= $this->_docCount) {
  895. require_once 'Zend/Search/Lucene/Exception.php';
  896. throw new Zend_Search_Lucene_Exception('Document id is out of the range.');
  897. }
  898. $segmentStartId = 0;
  899. foreach ($this->_segmentInfos as $segmentInfo) {
  900. if ($segmentStartId + $segmentInfo->count() > $id) {
  901. break;
  902. }
  903. $segmentStartId += $segmentInfo->count();
  904. }
  905. $fdxFile = $segmentInfo->openCompoundFile('.fdx');
  906. $fdxFile->seek(($id-$segmentStartId)*8, SEEK_CUR);
  907. $fieldValuesPosition = $fdxFile->readLong();
  908. $fdtFile = $segmentInfo->openCompoundFile('.fdt');
  909. $fdtFile->seek($fieldValuesPosition, SEEK_CUR);
  910. $fieldCount = $fdtFile->readVInt();
  911. $doc = new Zend_Search_Lucene_Document();
  912. for ($count = 0; $count < $fieldCount; $count++) {
  913. $fieldNum = $fdtFile->readVInt();
  914. $bits = $fdtFile->readByte();
  915. $fieldInfo = $segmentInfo->getField($fieldNum);
  916. if (!($bits & 2)) { // Text data
  917. $field = new Zend_Search_Lucene_Field($fieldInfo->name,
  918. $fdtFile->readString(),
  919. 'UTF-8',
  920. true,
  921. $fieldInfo->isIndexed,
  922. $bits & 1 );
  923. } else { // Binary data
  924. $field = new Zend_Search_Lucene_Field($fieldInfo->name,
  925. $fdtFile->readBinary(),
  926. '',
  927. true,
  928. $fieldInfo->isIndexed,
  929. $bits & 1,
  930. true );
  931. }
  932. $doc->addField($field);
  933. }
  934. return $doc;
  935. }
  936. /**
  937. * Returns true if index contain documents with specified term.
  938. *
  939. * Is used for query optimization.
  940. *
  941. * @param Zend_Search_Lucene_Index_Term $term
  942. * @return boolean
  943. */
  944. public function hasTerm(Zend_Search_Lucene_Index_Term $term)
  945. {
  946. foreach ($this->_segmentInfos as $segInfo) {
  947. if ($segInfo->getTermInfo($term) instanceof Zend_Search_Lucene_Index_TermInfo) {
  948. return true;
  949. }
  950. }
  951. return false;
  952. }
  953. /**
  954. * Returns IDs of all documents containing term.
  955. *
  956. * @param Zend_Search_Lucene_Index_Term $term
  957. * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
  958. * @return array
  959. */
  960. public function termDocs(Zend_Search_Lucene_Index_Term $term, $docsFilter = null)
  961. {
  962. $subResults = array();
  963. $segmentStartDocId = 0;
  964. foreach ($this->_segmentInfos as $segmentInfo) {
  965. $subResults[] = $segmentInfo->termDocs($term, $segmentStartDocId, $docsFilter);
  966. $segmentStartDocId += $segmentInfo->count();
  967. }
  968. if (count($subResults) == 0) {
  969. return array();
  970. } else if (count($subResults) == 1) {
  971. // Index is optimized (only one segment)
  972. // Do not perform array reindexing
  973. return reset($subResults);
  974. } else {
  975. $result = call_user_func_array('array_merge', $subResults);
  976. }
  977. return $result;
  978. }
  979. /**
  980. * Returns documents filter for all documents containing term.
  981. *
  982. * It performs the same operation as termDocs, but return result as
  983. * Zend_Search_Lucene_Index_DocsFilter object
  984. *
  985. * @param Zend_Search_Lucene_Index_Term $term
  986. * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
  987. * @return Zend_Search_Lucene_Index_DocsFilter
  988. */
  989. public function termDocsFilter(Zend_Search_Lucene_Index_Term $term, $docsFilter = null)
  990. {
  991. $segmentStartDocId = 0;
  992. $result = new Zend_Search_Lucene_Index_DocsFilter();
  993. foreach ($this->_segmentInfos as $segmentInfo) {
  994. $subResults[] = $segmentInfo->termDocs($term, $segmentStartDocId, $docsFilter);
  995. $segmentStartDocId += $segmentInfo->count();
  996. }
  997. if (count($subResults) == 0) {
  998. return array();
  999. } else if (count($subResults) == 1) {
  1000. // Index is optimized (only one segment)
  1001. // Do not perform array reindexing
  1002. return reset($subResults);
  1003. } else {
  1004. $result = call_user_func_array('array_merge', $subResults);
  1005. }
  1006. return $result;
  1007. }
  1008. /**
  1009. * Returns an array of all term freqs.
  1010. * Result array structure: array(docId => freq, ...)
  1011. *
  1012. * @param Zend_Search_Lucene_Index_Term $term
  1013. * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
  1014. * @return integer
  1015. */
  1016. public function termFreqs(Zend_Search_Lucene_Index_Term $term, $docsFilter = null)
  1017. {
  1018. $result = array();
  1019. $segmentStartDocId = 0;
  1020. foreach ($this->_segmentInfos as $segmentInfo) {
  1021. $result += $segmentInfo->termFreqs($term, $segmentStartDocId, $docsFilter);
  1022. $segmentStartDocId += $segmentInfo->count();
  1023. }
  1024. return $result;
  1025. }
  1026. /**
  1027. * Returns an array of all term positions in the documents.
  1028. * Result array structure: array(docId => array(pos1, pos2, ...), ...)
  1029. *
  1030. * @param Zend_Search_Lucene_Index_Term $term
  1031. * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
  1032. * @return array
  1033. */
  1034. public function termPositions(Zend_Search_Lucene_Index_Term $term, $docsFilter = null)
  1035. {
  1036. $result = array();
  1037. $segmentStartDocId = 0;
  1038. foreach ($this->_segmentInfos as $segmentInfo) {
  1039. $result += $segmentInfo->termPositions($term, $segmentStartDocId, $docsFilter);
  1040. $segmentStartDocId += $segmentInfo->count();
  1041. }
  1042. return $result;
  1043. }
  1044. /**
  1045. * Returns the number of documents in this index containing the $term.
  1046. *
  1047. * @param Zend_Search_Lucene_Index_Term $term
  1048. * @return integer
  1049. */
  1050. public function docFreq(Zend_Search_Lucene_Index_Term $term)
  1051. {
  1052. $result = 0;
  1053. foreach ($this->_segmentInfos as $segInfo) {
  1054. $termInfo = $segInfo->getTermInfo($term);
  1055. if ($termInfo !== null) {
  1056. $result += $termInfo->docFreq;
  1057. }
  1058. }
  1059. return $result;
  1060. }
  1061. /**
  1062. * Retrive similarity used by index reader
  1063. *
  1064. * @return Zend_Search_Lucene_Search_Similarity
  1065. */
  1066. public function getSimilarity()
  1067. {
  1068. return Zend_Search_Lucene_Search_Similarity::getDefault();
  1069. }
  1070. /**
  1071. * Returns a normalization factor for "field, document" pair.
  1072. *
  1073. * @param integer $id
  1074. * @param string $fieldName
  1075. * @return float
  1076. */
  1077. public function norm($id, $fieldName)
  1078. {
  1079. if ($id >= $this->_docCount) {
  1080. return null;
  1081. }
  1082. $segmentStartId = 0;
  1083. foreach ($this->_segmentInfos as $segInfo) {
  1084. if ($segmentStartId + $segInfo->count() > $id) {
  1085. break;
  1086. }
  1087. $segmentStartId += $segInfo->count();
  1088. }
  1089. if ($segInfo->isDeleted($id - $segmentStartId)) {
  1090. return 0;
  1091. }
  1092. return $segInfo->norm($id - $segmentStartId, $fieldName);
  1093. }
  1094. /**
  1095. * Returns true if any documents have been deleted from this index.
  1096. *
  1097. * @return boolean
  1098. */
  1099. public function hasDeletions()
  1100. {
  1101. foreach ($this->_segmentInfos as $segmentInfo) {
  1102. if ($segmentInfo->hasDeletions()) {
  1103. return true;
  1104. }
  1105. }
  1106. return false;
  1107. }
  1108. /**
  1109. * Deletes a document from the index.
  1110. * $id is an internal document id
  1111. *
  1112. * @param integer|Zend_Search_Lucene_Search_QueryHit $id
  1113. * @throws Zend_Search_Lucene_Exception
  1114. */
  1115. public function delete($id)
  1116. {
  1117. if ($id instanceof Zend_Search_Lucene_Search_QueryHit) {
  1118. /* @var $id Zend_Search_Lucene_Search_QueryHit */
  1119. $id = $id->id;
  1120. }
  1121. if ($id >= $this->_docCount) {
  1122. require_once 'Zend/Search/Lucene/Exception.php';
  1123. throw new Zend_Search_Lucene_Exception('Document id is out of the range.');
  1124. }
  1125. $segmentStartId = 0;
  1126. foreach ($this->_segmentInfos as $segmentInfo) {
  1127. if ($segmentStartId + $segmentInfo->count() > $id) {
  1128. break;
  1129. }
  1130. $segmentStartId += $segmentInfo->count();
  1131. }
  1132. $segmentInfo->delete($id - $segmentStartId);
  1133. $this->_hasChanges = true;
  1134. }
  1135. /**
  1136. * Adds a document to this index.
  1137. *
  1138. * @param Zend_Search_Lucene_Document $document
  1139. */
  1140. public function addDocument(Zend_Search_Lucene_Document $document)
  1141. {
  1142. $this->_getIndexWriter()->addDocument($document);
  1143. $this->_docCount++;
  1144. $this->_hasChanges = true;
  1145. }
  1146. /**
  1147. * Update document counter
  1148. */
  1149. private function _updateDocCount()
  1150. {
  1151. $this->_docCount = 0;
  1152. foreach ($this->_segmentInfos as $segInfo) {
  1153. $this->_docCount += $segInfo->count();
  1154. }
  1155. }
  1156. /**
  1157. * Commit changes resulting from delete() or undeleteAll() operations.
  1158. *
  1159. * @todo undeleteAll processing.
  1160. */
  1161. public function commit()
  1162. {
  1163. if ($this->_hasChanges) {
  1164. $this->_getIndexWriter()->commit();
  1165. $this->_updateDocCount();
  1166. $this->_hasChanges = false;
  1167. }
  1168. }
  1169. /**
  1170. * Optimize index.
  1171. *
  1172. * Merges all segments into one
  1173. */
  1174. public function optimize()
  1175. {
  1176. // Commit changes if any changes have been made
  1177. $this->commit();
  1178. if (count($this->_segmentInfos) > 1 || $this->hasDeletions()) {
  1179. $this->_getIndexWriter()->optimize();
  1180. $this->_updateDocCount();
  1181. }
  1182. }
  1183. /**
  1184. * Returns an array of all terms in this index.
  1185. *
  1186. * @return array
  1187. */
  1188. public function terms()
  1189. {
  1190. $result = array();
  1191. $segmentInfoQueue = new Zend_Search_Lucene_Index_TermsPriorityQueue();
  1192. foreach ($this->_segmentInfos as $segmentInfo) {
  1193. $segmentInfo->resetTermsStream();
  1194. // Skip "empty" segments
  1195. if ($segmentInfo->currentTerm() !== null) {
  1196. $segmentInfoQueue->put($segmentInfo);
  1197. }
  1198. }
  1199. while (($segmentInfo = $segmentInfoQueue->pop()) !== null) {
  1200. if ($segmentInfoQueue->top() === null ||
  1201. $segmentInfoQueue->top()->currentTerm()->key() !=
  1202. $segmentInfo->currentTerm()->key()) {
  1203. // We got new term
  1204. $result[] = $segmentInfo->currentTerm();
  1205. }
  1206. if ($segmentInfo->nextTerm() !== null) {
  1207. // Put segment back into the priority queue
  1208. $segmentInfoQueue->put($segmentInfo);
  1209. }
  1210. }
  1211. return $result;
  1212. }
  1213. /**
  1214. * Terms stream priority queue object
  1215. *
  1216. * @var Zend_Search_Lucene_TermStreamsPriorityQueue
  1217. */
  1218. private $_termsStream = null;
  1219. /**
  1220. * Reset terms stream.
  1221. */
  1222. public function resetTermsStream()
  1223. {
  1224. if ($this->_termsStream === null) {
  1225. $this->_termsStream = new Zend_Search_Lucene_TermStreamsPriorityQueue($this->_segmentInfos);
  1226. } else {
  1227. $this->_termsStream->resetTermsStream();
  1228. }
  1229. }
  1230. /**
  1231. * Skip terms stream up to specified term preffix.
  1232. *
  1233. * Prefix contains fully specified field info and portion of searched term
  1234. *
  1235. * @param Zend_Search_Lucene_Index_Term $prefix
  1236. */
  1237. public function skipTo(Zend_Search_Lucene_Index_Term $prefix)
  1238. {
  1239. $this->_termsStream->skipTo($prefix);
  1240. }
  1241. /**
  1242. * Scans terms dictionary and returns next term
  1243. *
  1244. * @return Zend_Search_Lucene_Index_Term|null
  1245. */
  1246. public function nextTerm()
  1247. {
  1248. return $this->_termsStream->nextTerm();
  1249. }
  1250. /**
  1251. * Returns term in current position
  1252. *
  1253. * @return Zend_Search_Lucene_Index_Term|null
  1254. */
  1255. public function currentTerm()
  1256. {
  1257. return $this->_termsStream->currentTerm();
  1258. }
  1259. /**
  1260. * Close terms stream
  1261. *
  1262. * Should be used for resources clean up if stream is not read up to the end
  1263. */
  1264. public function closeTermsStream()
  1265. {
  1266. $this->_termsStream->closeTermsStream();
  1267. $this->_termsStream = null;
  1268. }
  1269. /*************************************************************************
  1270. @todo UNIMPLEMENTED
  1271. *************************************************************************/
  1272. /**
  1273. * Undeletes all documents currently marked as deleted in this index.
  1274. *
  1275. * @todo Implementation
  1276. */
  1277. public function undeleteAll()
  1278. {}
  1279. }