Lucene.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520
  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. if ($count + 1 < count($argList) && is_integer($argList[$count+1])) {
  837. $count++;
  838. $sortArgs[] = &$argList[$count];
  839. if ($count + 1 < count($argList) && is_integer($argList[$count+1])) {
  840. $count++;
  841. $sortArgs[] = &$argList[$count];
  842. } else {
  843. if ($argList[$count] == SORT_ASC || $argList[$count] == SORT_DESC) {
  844. $sortArgs[] = &$sortReg;
  845. } else {
  846. $sortArgs[] = &$sortAsc;
  847. }
  848. }
  849. } else {
  850. $sortArgs[] = &$sortAsc;
  851. $sortArgs[] = &$sortReg;
  852. }
  853. }
  854. // Sort by id's if values are equal
  855. $sortArgs[] = &$ids;
  856. $sortArgs[] = &$sortAsc;
  857. $sortArgs[] = &$sortNum;
  858. // Array to be sorted
  859. $sortArgs[] = &$hits;
  860. // Do sort
  861. call_user_func_array('array_multisort', $sortArgs);
  862. }
  863. return $hits;
  864. }
  865. /**
  866. * Returns a list of all unique field names that exist in this index.
  867. *
  868. * @param boolean $indexed
  869. * @return array
  870. */
  871. public function getFieldNames($indexed = false)
  872. {
  873. $result = array();
  874. foreach( $this->_segmentInfos as $segmentInfo ) {
  875. $result = array_merge($result, $segmentInfo->getFields($indexed));
  876. }
  877. return $result;
  878. }
  879. /**
  880. * Returns a Zend_Search_Lucene_Document object for the document
  881. * number $id in this index.
  882. *
  883. * @param integer|Zend_Search_Lucene_Search_QueryHit $id
  884. * @return Zend_Search_Lucene_Document
  885. * @throws Zend_Search_Lucene_Exception Exception is thrown if $id is out of the range
  886. */
  887. public function getDocument($id)
  888. {
  889. if ($id instanceof Zend_Search_Lucene_Search_QueryHit) {
  890. /* @var $id Zend_Search_Lucene_Search_QueryHit */
  891. $id = $id->id;
  892. }
  893. if ($id >= $this->_docCount) {
  894. require_once 'Zend/Search/Lucene/Exception.php';
  895. throw new Zend_Search_Lucene_Exception('Document id is out of the range.');
  896. }
  897. $segmentStartId = 0;
  898. foreach ($this->_segmentInfos as $segmentInfo) {
  899. if ($segmentStartId + $segmentInfo->count() > $id) {
  900. break;
  901. }
  902. $segmentStartId += $segmentInfo->count();
  903. }
  904. $fdxFile = $segmentInfo->openCompoundFile('.fdx');
  905. $fdxFile->seek(($id-$segmentStartId)*8, SEEK_CUR);
  906. $fieldValuesPosition = $fdxFile->readLong();
  907. $fdtFile = $segmentInfo->openCompoundFile('.fdt');
  908. $fdtFile->seek($fieldValuesPosition, SEEK_CUR);
  909. $fieldCount = $fdtFile->readVInt();
  910. $doc = new Zend_Search_Lucene_Document();
  911. for ($count = 0; $count < $fieldCount; $count++) {
  912. $fieldNum = $fdtFile->readVInt();
  913. $bits = $fdtFile->readByte();
  914. $fieldInfo = $segmentInfo->getField($fieldNum);
  915. if (!($bits & 2)) { // Text data
  916. $field = new Zend_Search_Lucene_Field($fieldInfo->name,
  917. $fdtFile->readString(),
  918. 'UTF-8',
  919. true,
  920. $fieldInfo->isIndexed,
  921. $bits & 1 );
  922. } else { // Binary data
  923. $field = new Zend_Search_Lucene_Field($fieldInfo->name,
  924. $fdtFile->readBinary(),
  925. '',
  926. true,
  927. $fieldInfo->isIndexed,
  928. $bits & 1,
  929. true );
  930. }
  931. $doc->addField($field);
  932. }
  933. return $doc;
  934. }
  935. /**
  936. * Returns true if index contain documents with specified term.
  937. *
  938. * Is used for query optimization.
  939. *
  940. * @param Zend_Search_Lucene_Index_Term $term
  941. * @return boolean
  942. */
  943. public function hasTerm(Zend_Search_Lucene_Index_Term $term)
  944. {
  945. foreach ($this->_segmentInfos as $segInfo) {
  946. if ($segInfo->getTermInfo($term) instanceof Zend_Search_Lucene_Index_TermInfo) {
  947. return true;
  948. }
  949. }
  950. return false;
  951. }
  952. /**
  953. * Returns IDs of all documents containing term.
  954. *
  955. * @param Zend_Search_Lucene_Index_Term $term
  956. * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
  957. * @return array
  958. */
  959. public function termDocs(Zend_Search_Lucene_Index_Term $term, $docsFilter = null)
  960. {
  961. $subResults = array();
  962. $segmentStartDocId = 0;
  963. foreach ($this->_segmentInfos as $segmentInfo) {
  964. $subResults[] = $segmentInfo->termDocs($term, $segmentStartDocId, $docsFilter);
  965. $segmentStartDocId += $segmentInfo->count();
  966. }
  967. if (count($subResults) == 0) {
  968. return array();
  969. } else if (count($subResults) == 1) {
  970. // Index is optimized (only one segment)
  971. // Do not perform array reindexing
  972. return reset($subResults);
  973. } else {
  974. $result = call_user_func_array('array_merge', $subResults);
  975. }
  976. return $result;
  977. }
  978. /**
  979. * Returns documents filter for all documents containing term.
  980. *
  981. * It performs the same operation as termDocs, but return result as
  982. * Zend_Search_Lucene_Index_DocsFilter object
  983. *
  984. * @param Zend_Search_Lucene_Index_Term $term
  985. * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
  986. * @return Zend_Search_Lucene_Index_DocsFilter
  987. */
  988. public function termDocsFilter(Zend_Search_Lucene_Index_Term $term, $docsFilter = null)
  989. {
  990. $segmentStartDocId = 0;
  991. $result = new Zend_Search_Lucene_Index_DocsFilter();
  992. foreach ($this->_segmentInfos as $segmentInfo) {
  993. $subResults[] = $segmentInfo->termDocs($term, $segmentStartDocId, $docsFilter);
  994. $segmentStartDocId += $segmentInfo->count();
  995. }
  996. if (count($subResults) == 0) {
  997. return array();
  998. } else if (count($subResults) == 1) {
  999. // Index is optimized (only one segment)
  1000. // Do not perform array reindexing
  1001. return reset($subResults);
  1002. } else {
  1003. $result = call_user_func_array('array_merge', $subResults);
  1004. }
  1005. return $result;
  1006. }
  1007. /**
  1008. * Returns an array of all term freqs.
  1009. * Result array structure: array(docId => freq, ...)
  1010. *
  1011. * @param Zend_Search_Lucene_Index_Term $term
  1012. * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
  1013. * @return integer
  1014. */
  1015. public function termFreqs(Zend_Search_Lucene_Index_Term $term, $docsFilter = null)
  1016. {
  1017. $result = array();
  1018. $segmentStartDocId = 0;
  1019. foreach ($this->_segmentInfos as $segmentInfo) {
  1020. $result += $segmentInfo->termFreqs($term, $segmentStartDocId, $docsFilter);
  1021. $segmentStartDocId += $segmentInfo->count();
  1022. }
  1023. return $result;
  1024. }
  1025. /**
  1026. * Returns an array of all term positions in the documents.
  1027. * Result array structure: array(docId => array(pos1, pos2, ...), ...)
  1028. *
  1029. * @param Zend_Search_Lucene_Index_Term $term
  1030. * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
  1031. * @return array
  1032. */
  1033. public function termPositions(Zend_Search_Lucene_Index_Term $term, $docsFilter = null)
  1034. {
  1035. $result = array();
  1036. $segmentStartDocId = 0;
  1037. foreach ($this->_segmentInfos as $segmentInfo) {
  1038. $result += $segmentInfo->termPositions($term, $segmentStartDocId, $docsFilter);
  1039. $segmentStartDocId += $segmentInfo->count();
  1040. }
  1041. return $result;
  1042. }
  1043. /**
  1044. * Returns the number of documents in this index containing the $term.
  1045. *
  1046. * @param Zend_Search_Lucene_Index_Term $term
  1047. * @return integer
  1048. */
  1049. public function docFreq(Zend_Search_Lucene_Index_Term $term)
  1050. {
  1051. $result = 0;
  1052. foreach ($this->_segmentInfos as $segInfo) {
  1053. $termInfo = $segInfo->getTermInfo($term);
  1054. if ($termInfo !== null) {
  1055. $result += $termInfo->docFreq;
  1056. }
  1057. }
  1058. return $result;
  1059. }
  1060. /**
  1061. * Retrive similarity used by index reader
  1062. *
  1063. * @return Zend_Search_Lucene_Search_Similarity
  1064. */
  1065. public function getSimilarity()
  1066. {
  1067. return Zend_Search_Lucene_Search_Similarity::getDefault();
  1068. }
  1069. /**
  1070. * Returns a normalization factor for "field, document" pair.
  1071. *
  1072. * @param integer $id
  1073. * @param string $fieldName
  1074. * @return float
  1075. */
  1076. public function norm($id, $fieldName)
  1077. {
  1078. if ($id >= $this->_docCount) {
  1079. return null;
  1080. }
  1081. $segmentStartId = 0;
  1082. foreach ($this->_segmentInfos as $segInfo) {
  1083. if ($segmentStartId + $segInfo->count() > $id) {
  1084. break;
  1085. }
  1086. $segmentStartId += $segInfo->count();
  1087. }
  1088. if ($segInfo->isDeleted($id - $segmentStartId)) {
  1089. return 0;
  1090. }
  1091. return $segInfo->norm($id - $segmentStartId, $fieldName);
  1092. }
  1093. /**
  1094. * Returns true if any documents have been deleted from this index.
  1095. *
  1096. * @return boolean
  1097. */
  1098. public function hasDeletions()
  1099. {
  1100. foreach ($this->_segmentInfos as $segmentInfo) {
  1101. if ($segmentInfo->hasDeletions()) {
  1102. return true;
  1103. }
  1104. }
  1105. return false;
  1106. }
  1107. /**
  1108. * Deletes a document from the index.
  1109. * $id is an internal document id
  1110. *
  1111. * @param integer|Zend_Search_Lucene_Search_QueryHit $id
  1112. * @throws Zend_Search_Lucene_Exception
  1113. */
  1114. public function delete($id)
  1115. {
  1116. if ($id instanceof Zend_Search_Lucene_Search_QueryHit) {
  1117. /* @var $id Zend_Search_Lucene_Search_QueryHit */
  1118. $id = $id->id;
  1119. }
  1120. if ($id >= $this->_docCount) {
  1121. require_once 'Zend/Search/Lucene/Exception.php';
  1122. throw new Zend_Search_Lucene_Exception('Document id is out of the range.');
  1123. }
  1124. $segmentStartId = 0;
  1125. foreach ($this->_segmentInfos as $segmentInfo) {
  1126. if ($segmentStartId + $segmentInfo->count() > $id) {
  1127. break;
  1128. }
  1129. $segmentStartId += $segmentInfo->count();
  1130. }
  1131. $segmentInfo->delete($id - $segmentStartId);
  1132. $this->_hasChanges = true;
  1133. }
  1134. /**
  1135. * Adds a document to this index.
  1136. *
  1137. * @param Zend_Search_Lucene_Document $document
  1138. */
  1139. public function addDocument(Zend_Search_Lucene_Document $document)
  1140. {
  1141. $this->_getIndexWriter()->addDocument($document);
  1142. $this->_docCount++;
  1143. $this->_hasChanges = true;
  1144. }
  1145. /**
  1146. * Update document counter
  1147. */
  1148. private function _updateDocCount()
  1149. {
  1150. $this->_docCount = 0;
  1151. foreach ($this->_segmentInfos as $segInfo) {
  1152. $this->_docCount += $segInfo->count();
  1153. }
  1154. }
  1155. /**
  1156. * Commit changes resulting from delete() or undeleteAll() operations.
  1157. *
  1158. * @todo undeleteAll processing.
  1159. */
  1160. public function commit()
  1161. {
  1162. if ($this->_hasChanges) {
  1163. $this->_getIndexWriter()->commit();
  1164. $this->_updateDocCount();
  1165. $this->_hasChanges = false;
  1166. }
  1167. }
  1168. /**
  1169. * Optimize index.
  1170. *
  1171. * Merges all segments into one
  1172. */
  1173. public function optimize()
  1174. {
  1175. // Commit changes if any changes have been made
  1176. $this->commit();
  1177. if (count($this->_segmentInfos) > 1 || $this->hasDeletions()) {
  1178. $this->_getIndexWriter()->optimize();
  1179. $this->_updateDocCount();
  1180. }
  1181. }
  1182. /**
  1183. * Returns an array of all terms in this index.
  1184. *
  1185. * @return array
  1186. */
  1187. public function terms()
  1188. {
  1189. $result = array();
  1190. $segmentInfoQueue = new Zend_Search_Lucene_Index_TermsPriorityQueue();
  1191. foreach ($this->_segmentInfos as $segmentInfo) {
  1192. $segmentInfo->resetTermsStream();
  1193. // Skip "empty" segments
  1194. if ($segmentInfo->currentTerm() !== null) {
  1195. $segmentInfoQueue->put($segmentInfo);
  1196. }
  1197. }
  1198. while (($segmentInfo = $segmentInfoQueue->pop()) !== null) {
  1199. if ($segmentInfoQueue->top() === null ||
  1200. $segmentInfoQueue->top()->currentTerm()->key() !=
  1201. $segmentInfo->currentTerm()->key()) {
  1202. // We got new term
  1203. $result[] = $segmentInfo->currentTerm();
  1204. }
  1205. if ($segmentInfo->nextTerm() !== null) {
  1206. // Put segment back into the priority queue
  1207. $segmentInfoQueue->put($segmentInfo);
  1208. }
  1209. }
  1210. return $result;
  1211. }
  1212. /**
  1213. * Terms stream priority queue object
  1214. *
  1215. * @var Zend_Search_Lucene_TermStreamsPriorityQueue
  1216. */
  1217. private $_termsStream = null;
  1218. /**
  1219. * Reset terms stream.
  1220. */
  1221. public function resetTermsStream()
  1222. {
  1223. if ($this->_termsStream === null) {
  1224. $this->_termsStream = new Zend_Search_Lucene_TermStreamsPriorityQueue($this->_segmentInfos);
  1225. } else {
  1226. $this->_termsStream->resetTermsStream();
  1227. }
  1228. }
  1229. /**
  1230. * Skip terms stream up to specified term preffix.
  1231. *
  1232. * Prefix contains fully specified field info and portion of searched term
  1233. *
  1234. * @param Zend_Search_Lucene_Index_Term $prefix
  1235. */
  1236. public function skipTo(Zend_Search_Lucene_Index_Term $prefix)
  1237. {
  1238. $this->_termsStream->skipTo($prefix);
  1239. }
  1240. /**
  1241. * Scans terms dictionary and returns next term
  1242. *
  1243. * @return Zend_Search_Lucene_Index_Term|null
  1244. */
  1245. public function nextTerm()
  1246. {
  1247. return $this->_termsStream->nextTerm();
  1248. }
  1249. /**
  1250. * Returns term in current position
  1251. *
  1252. * @return Zend_Search_Lucene_Index_Term|null
  1253. */
  1254. public function currentTerm()
  1255. {
  1256. return $this->_termsStream->currentTerm();
  1257. }
  1258. /**
  1259. * Close terms stream
  1260. *
  1261. * Should be used for resources clean up if stream is not read up to the end
  1262. */
  1263. public function closeTermsStream()
  1264. {
  1265. $this->_termsStream->closeTermsStream();
  1266. $this->_termsStream = null;
  1267. }
  1268. /*************************************************************************
  1269. @todo UNIMPLEMENTED
  1270. *************************************************************************/
  1271. /**
  1272. * Undeletes all documents currently marked as deleted in this index.
  1273. *
  1274. * @todo Implementation
  1275. */
  1276. public function undeleteAll()
  1277. {}
  1278. }