Lucene.php 45 KB

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