Lucene.php 43 KB

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