Lucene.php 44 KB

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