MultiSearcher.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /** Zend_Search_Lucene_Interface */
  22. require_once 'Zend/Search/Lucene/Interface.php';
  23. /**
  24. * Import Zend_Search_Lucene_Interface_MultiSearcher for BC (see ZF-12067)
  25. * @see Zend_Search_Lucene_Interface_MultiSearcher
  26. */
  27. require_once 'Zend/Search/Lucene/Interface/MultiSearcher.php';
  28. /**
  29. * Multisearcher allows to search through several independent indexes.
  30. *
  31. * @category Zend
  32. * @package Zend_Search_Lucene
  33. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Search_Lucene_MultiSearcher implements Zend_Search_Lucene_Interface
  37. {
  38. /**
  39. * List of indices for searching.
  40. * Array of Zend_Search_Lucene_Interface objects
  41. *
  42. * @var array
  43. */
  44. protected $_indices;
  45. /**
  46. * Object constructor.
  47. *
  48. * @param array $indices Arrays of indices for search
  49. * @throws Zend_Search_Lucene_Exception
  50. */
  51. public function __construct($indices = array())
  52. {
  53. $this->_indices = $indices;
  54. foreach ($this->_indices as $index) {
  55. if (!$index instanceof Zend_Search_Lucene_Interface) {
  56. require_once 'Zend/Search/Lucene/Exception.php';
  57. throw new Zend_Search_Lucene_Exception('sub-index objects have to implement Zend_Search_Lucene_Interface.');
  58. }
  59. }
  60. }
  61. /**
  62. * Add index for searching.
  63. *
  64. * @param Zend_Search_Lucene_Interface $index
  65. */
  66. public function addIndex(Zend_Search_Lucene_Interface $index)
  67. {
  68. $this->_indices[] = $index;
  69. }
  70. /**
  71. * Get current generation number
  72. *
  73. * Returns generation number
  74. * 0 means pre-2.1 index format
  75. * -1 means there are no segments files.
  76. *
  77. * @param Zend_Search_Lucene_Storage_Directory $directory
  78. * @return integer
  79. * @throws Zend_Search_Lucene_Exception
  80. */
  81. public static function getActualGeneration(Zend_Search_Lucene_Storage_Directory $directory)
  82. {
  83. require_once 'Zend/Search/Lucene/Exception.php';
  84. throw new Zend_Search_Lucene_Exception("Generation number can't be retrieved for multi-searcher");
  85. }
  86. /**
  87. * Get segments file name
  88. *
  89. * @param integer $generation
  90. * @return string
  91. */
  92. public static function getSegmentFileName($generation)
  93. {
  94. return Zend_Search_Lucene::getSegmentFileName($generation);
  95. }
  96. /**
  97. * Get index format version
  98. *
  99. * @return integer
  100. * @throws Zend_Search_Lucene_Exception
  101. */
  102. public function getFormatVersion()
  103. {
  104. require_once 'Zend/Search/Lucene/Exception.php';
  105. throw new Zend_Search_Lucene_Exception("Format version can't be retrieved for multi-searcher");
  106. }
  107. /**
  108. * Set index format version.
  109. * Index is converted to this format at the nearest upfdate time
  110. *
  111. * @param int $formatVersion
  112. */
  113. public function setFormatVersion($formatVersion)
  114. {
  115. foreach ($this->_indices as $index) {
  116. $index->setFormatVersion($formatVersion);
  117. }
  118. }
  119. /**
  120. * Returns the Zend_Search_Lucene_Storage_Directory instance for this index.
  121. *
  122. * @return Zend_Search_Lucene_Storage_Directory
  123. */
  124. public function getDirectory()
  125. {
  126. require_once 'Zend/Search/Lucene/Exception.php';
  127. throw new Zend_Search_Lucene_Exception("Index directory can't be retrieved for multi-searcher");
  128. }
  129. /**
  130. * Returns the total number of documents in this index (including deleted documents).
  131. *
  132. * @return integer
  133. */
  134. public function count()
  135. {
  136. $count = 0;
  137. foreach ($this->_indices as $index) {
  138. $count += $index->count();
  139. }
  140. return $count;
  141. }
  142. /**
  143. * Returns one greater than the largest possible document number.
  144. * This may be used to, e.g., determine how big to allocate a structure which will have
  145. * an element for every document number in an index.
  146. *
  147. * @return integer
  148. */
  149. public function maxDoc()
  150. {
  151. return $this->count();
  152. }
  153. /**
  154. * Returns the total number of non-deleted documents in this index.
  155. *
  156. * @return integer
  157. */
  158. public function numDocs()
  159. {
  160. $docs = 0;
  161. foreach ($this->_indices as $index) {
  162. $docs += $index->numDocs();
  163. }
  164. return $docs;
  165. }
  166. /**
  167. * Checks, that document is deleted
  168. *
  169. * @param integer $id
  170. * @return boolean
  171. * @throws Zend_Search_Lucene_Exception Exception is thrown if $id is out of the range
  172. */
  173. public function isDeleted($id)
  174. {
  175. foreach ($this->_indices as $index) {
  176. $indexCount = $index->count();
  177. if ($indexCount > $id) {
  178. return $index->isDeleted($id);
  179. }
  180. $id -= $indexCount;
  181. }
  182. require_once 'Zend/Search/Lucene/Exception.php';
  183. throw new Zend_Search_Lucene_Exception('Document id is out of the range.');
  184. }
  185. /**
  186. * Set default search field.
  187. *
  188. * Null means, that search is performed through all fields by default
  189. *
  190. * Default value is null
  191. *
  192. * @param string $fieldName
  193. */
  194. public static function setDefaultSearchField($fieldName)
  195. {
  196. foreach ($this->_indices as $index) {
  197. $index->setDefaultSearchField($fieldName);
  198. }
  199. }
  200. /**
  201. * Get default search field.
  202. *
  203. * Null means, that search is performed through all fields by default
  204. *
  205. * @return string
  206. * @throws Zend_Search_Lucene_Exception
  207. */
  208. public static function getDefaultSearchField()
  209. {
  210. if (count($this->_indices) == 0) {
  211. require_once 'Zend/Search/Lucene/Exception.php';
  212. throw new Zend_Search_Lucene_Exception('Indices list is empty');
  213. }
  214. $defaultSearchField = reset($this->_indices)->getDefaultSearchField();
  215. foreach ($this->_indices as $index) {
  216. if ($index->getDefaultSearchField() !== $defaultSearchField) {
  217. require_once 'Zend/Search/Lucene/Exception.php';
  218. throw new Zend_Search_Lucene_Exception('Indices have different default search field.');
  219. }
  220. }
  221. return $defaultSearchField;
  222. }
  223. /**
  224. * Set result set limit.
  225. *
  226. * 0 (default) means no limit
  227. *
  228. * @param integer $limit
  229. */
  230. public static function setResultSetLimit($limit)
  231. {
  232. foreach ($this->_indices as $index) {
  233. $index->setResultSetLimit($limit);
  234. }
  235. }
  236. /**
  237. * Set result set limit.
  238. *
  239. * 0 means no limit
  240. *
  241. * @return integer
  242. * @throws Zend_Search_Lucene_Exception
  243. */
  244. public static function getResultSetLimit()
  245. {
  246. if (count($this->_indices) == 0) {
  247. require_once 'Zend/Search/Lucene/Exception.php';
  248. throw new Zend_Search_Lucene_Exception('Indices list is empty');
  249. }
  250. $defaultResultSetLimit = reset($this->_indices)->getResultSetLimit();
  251. foreach ($this->_indices as $index) {
  252. if ($index->getResultSetLimit() !== $defaultResultSetLimit) {
  253. require_once 'Zend/Search/Lucene/Exception.php';
  254. throw new Zend_Search_Lucene_Exception('Indices have different default search field.');
  255. }
  256. }
  257. return $defaultResultSetLimit;
  258. }
  259. /**
  260. * Retrieve index maxBufferedDocs option
  261. *
  262. * maxBufferedDocs is a minimal number of documents required before
  263. * the buffered in-memory documents are written into a new Segment
  264. *
  265. * Default value is 10
  266. *
  267. * @return integer
  268. * @throws Zend_Search_Lucene_Exception
  269. */
  270. public function getMaxBufferedDocs()
  271. {
  272. if (count($this->_indices) == 0) {
  273. require_once 'Zend/Search/Lucene/Exception.php';
  274. throw new Zend_Search_Lucene_Exception('Indices list is empty');
  275. }
  276. $maxBufferedDocs = reset($this->_indices)->getMaxBufferedDocs();
  277. foreach ($this->_indices as $index) {
  278. if ($index->getMaxBufferedDocs() !== $maxBufferedDocs) {
  279. require_once 'Zend/Search/Lucene/Exception.php';
  280. throw new Zend_Search_Lucene_Exception('Indices have different default search field.');
  281. }
  282. }
  283. return $maxBufferedDocs;
  284. }
  285. /**
  286. * Set index maxBufferedDocs option
  287. *
  288. * maxBufferedDocs is a minimal number of documents required before
  289. * the buffered in-memory documents are written into a new Segment
  290. *
  291. * Default value is 10
  292. *
  293. * @param integer $maxBufferedDocs
  294. */
  295. public function setMaxBufferedDocs($maxBufferedDocs)
  296. {
  297. foreach ($this->_indices as $index) {
  298. $index->setMaxBufferedDocs($maxBufferedDocs);
  299. }
  300. }
  301. /**
  302. * Retrieve index maxMergeDocs option
  303. *
  304. * maxMergeDocs is a largest number of documents ever merged by addDocument().
  305. * Small values (e.g., less than 10,000) are best for interactive indexing,
  306. * as this limits the length of pauses while indexing to a few seconds.
  307. * Larger values are best for batched indexing and speedier searches.
  308. *
  309. * Default value is PHP_INT_MAX
  310. *
  311. * @return integer
  312. * @throws Zend_Search_Lucene_Exception
  313. */
  314. public function getMaxMergeDocs()
  315. {
  316. if (count($this->_indices) == 0) {
  317. require_once 'Zend/Search/Lucene/Exception.php';
  318. throw new Zend_Search_Lucene_Exception('Indices list is empty');
  319. }
  320. $maxMergeDocs = reset($this->_indices)->getMaxMergeDocs();
  321. foreach ($this->_indices as $index) {
  322. if ($index->getMaxMergeDocs() !== $maxMergeDocs) {
  323. require_once 'Zend/Search/Lucene/Exception.php';
  324. throw new Zend_Search_Lucene_Exception('Indices have different default search field.');
  325. }
  326. }
  327. return $maxMergeDocs;
  328. }
  329. /**
  330. * Set index maxMergeDocs option
  331. *
  332. * maxMergeDocs is a largest number of documents ever merged by addDocument().
  333. * Small values (e.g., less than 10,000) are best for interactive indexing,
  334. * as this limits the length of pauses while indexing to a few seconds.
  335. * Larger values are best for batched indexing and speedier searches.
  336. *
  337. * Default value is PHP_INT_MAX
  338. *
  339. * @param integer $maxMergeDocs
  340. */
  341. public function setMaxMergeDocs($maxMergeDocs)
  342. {
  343. foreach ($this->_indices as $index) {
  344. $index->setMaxMergeDocs($maxMergeDocs);
  345. }
  346. }
  347. /**
  348. * Retrieve index mergeFactor option
  349. *
  350. * mergeFactor determines how often segment indices are merged by addDocument().
  351. * With smaller values, less RAM is used while indexing,
  352. * and searches on unoptimized indices are faster,
  353. * but indexing speed is slower.
  354. * With larger values, more RAM is used during indexing,
  355. * and while searches on unoptimized indices are slower,
  356. * indexing is faster.
  357. * Thus larger values (> 10) are best for batch index creation,
  358. * and smaller values (< 10) for indices that are interactively maintained.
  359. *
  360. * Default value is 10
  361. *
  362. * @return integer
  363. * @throws Zend_Search_Lucene_Exception
  364. */
  365. public function getMergeFactor()
  366. {
  367. if (count($this->_indices) == 0) {
  368. require_once 'Zend/Search/Lucene/Exception.php';
  369. throw new Zend_Search_Lucene_Exception('Indices list is empty');
  370. }
  371. $mergeFactor = reset($this->_indices)->getMergeFactor();
  372. foreach ($this->_indices as $index) {
  373. if ($index->getMergeFactor() !== $mergeFactor) {
  374. require_once 'Zend/Search/Lucene/Exception.php';
  375. throw new Zend_Search_Lucene_Exception('Indices have different default search field.');
  376. }
  377. }
  378. return $mergeFactor;
  379. }
  380. /**
  381. * Set index mergeFactor option
  382. *
  383. * mergeFactor determines how often segment indices are merged by addDocument().
  384. * With smaller values, less RAM is used while indexing,
  385. * and searches on unoptimized indices are faster,
  386. * but indexing speed is slower.
  387. * With larger values, more RAM is used during indexing,
  388. * and while searches on unoptimized indices are slower,
  389. * indexing is faster.
  390. * Thus larger values (> 10) are best for batch index creation,
  391. * and smaller values (< 10) for indices that are interactively maintained.
  392. *
  393. * Default value is 10
  394. *
  395. * @param integer $maxMergeDocs
  396. */
  397. public function setMergeFactor($mergeFactor)
  398. {
  399. foreach ($this->_indices as $index) {
  400. $index->setMaxMergeDocs($mergeFactor);
  401. }
  402. }
  403. /**
  404. * Performs a query against the index and returns an array
  405. * of Zend_Search_Lucene_Search_QueryHit objects.
  406. * Input is a string or Zend_Search_Lucene_Search_Query.
  407. *
  408. * @param mixed $query
  409. * @return array Zend_Search_Lucene_Search_QueryHit
  410. * @throws Zend_Search_Lucene_Exception
  411. */
  412. public function find($query)
  413. {
  414. if (count($this->_indices) == 0) {
  415. return array();
  416. }
  417. $hitsList = array();
  418. $indexShift = 0;
  419. foreach ($this->_indices as $index) {
  420. $hits = $index->find($query);
  421. if ($indexShift != 0) {
  422. foreach ($hits as $hit) {
  423. $hit->id += $indexShift;
  424. }
  425. }
  426. $indexShift += $index->count();
  427. $hitsList[] = $hits;
  428. }
  429. /** @todo Implement advanced sorting */
  430. return call_user_func_array('array_merge', $hitsList);
  431. }
  432. /**
  433. * Returns a list of all unique field names that exist in this index.
  434. *
  435. * @param boolean $indexed
  436. * @return array
  437. */
  438. public function getFieldNames($indexed = false)
  439. {
  440. $fieldNamesList = array();
  441. foreach ($this->_indices as $index) {
  442. $fieldNamesList[] = $index->getFieldNames($indexed);
  443. }
  444. return array_unique(call_user_func_array('array_merge', $fieldNamesList));
  445. }
  446. /**
  447. * Returns a Zend_Search_Lucene_Document object for the document
  448. * number $id in this index.
  449. *
  450. * @param integer|Zend_Search_Lucene_Search_QueryHit $id
  451. * @return Zend_Search_Lucene_Document
  452. * @throws Zend_Search_Lucene_Exception Exception is thrown if $id is out of the range
  453. */
  454. public function getDocument($id)
  455. {
  456. if ($id instanceof Zend_Search_Lucene_Search_QueryHit) {
  457. /* @var $id Zend_Search_Lucene_Search_QueryHit */
  458. $id = $id->id;
  459. }
  460. foreach ($this->_indices as $index) {
  461. $indexCount = $index->count();
  462. if ($indexCount > $id) {
  463. return $index->getDocument($id);
  464. }
  465. $id -= $indexCount;
  466. }
  467. require_once 'Zend/Search/Lucene/Exception.php';
  468. throw new Zend_Search_Lucene_Exception('Document id is out of the range.');
  469. }
  470. /**
  471. * Returns true if index contain documents with specified term.
  472. *
  473. * Is used for query optimization.
  474. *
  475. * @param Zend_Search_Lucene_Index_Term $term
  476. * @return boolean
  477. */
  478. public function hasTerm(Zend_Search_Lucene_Index_Term $term)
  479. {
  480. foreach ($this->_indices as $index) {
  481. if ($index->hasTerm($term)) {
  482. return true;
  483. }
  484. }
  485. return false;
  486. }
  487. /**
  488. * Returns IDs of all the documents containing term.
  489. *
  490. * @param Zend_Search_Lucene_Index_Term $term
  491. * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
  492. * @return array
  493. * @throws Zend_Search_Lucene_Exception
  494. */
  495. public function termDocs(Zend_Search_Lucene_Index_Term $term, $docsFilter = null)
  496. {
  497. if ($docsFilter != null) {
  498. require_once 'Zend/Search/Lucene/Exception.php';
  499. throw new Zend_Search_Lucene_Exception('Document filters could not used with multi-searcher');
  500. }
  501. $docsList = array();
  502. $indexShift = 0;
  503. foreach ($this->_indices as $index) {
  504. $docs = $index->termDocs($term);
  505. if ($indexShift != 0) {
  506. foreach ($docs as $id => $docId) {
  507. $docs[$id] += $indexShift;
  508. }
  509. }
  510. $indexShift += $index->count();
  511. $docsList[] = $docs;
  512. }
  513. return call_user_func_array('array_merge', $docsList);
  514. }
  515. /**
  516. * Returns documents filter for all documents containing term.
  517. *
  518. * It performs the same operation as termDocs, but return result as
  519. * Zend_Search_Lucene_Index_DocsFilter object
  520. *
  521. * @param Zend_Search_Lucene_Index_Term $term
  522. * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
  523. * @return Zend_Search_Lucene_Index_DocsFilter
  524. * @throws Zend_Search_Lucene_Exception
  525. */
  526. public function termDocsFilter(Zend_Search_Lucene_Index_Term $term, $docsFilter = null)
  527. {
  528. require_once 'Zend/Search/Lucene/Exception.php';
  529. throw new Zend_Search_Lucene_Exception('Document filters could not used with multi-searcher');
  530. }
  531. /**
  532. * Returns an array of all term freqs.
  533. * Return array structure: array( docId => freq, ...)
  534. *
  535. * @param Zend_Search_Lucene_Index_Term $term
  536. * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
  537. * @return integer
  538. * @throws Zend_Search_Lucene_Exception
  539. */
  540. public function termFreqs(Zend_Search_Lucene_Index_Term $term, $docsFilter = null)
  541. {
  542. if ($docsFilter != null) {
  543. require_once 'Zend/Search/Lucene/Exception.php';
  544. throw new Zend_Search_Lucene_Exception('Document filters could not used with multi-searcher');
  545. }
  546. $freqsList = array();
  547. $indexShift = 0;
  548. foreach ($this->_indices as $index) {
  549. $freqs = $index->termFreqs($term);
  550. if ($indexShift != 0) {
  551. $freqsShifted = array();
  552. foreach ($freqs as $docId => $freq) {
  553. $freqsShifted[$docId + $indexShift] = $freq;
  554. }
  555. $freqs = $freqsShifted;
  556. }
  557. $indexShift += $index->count();
  558. $freqsList[] = $freqs;
  559. }
  560. return call_user_func_array('array_merge', $freqsList);
  561. }
  562. /**
  563. * Returns an array of all term positions in the documents.
  564. * Return array structure: array( docId => array( pos1, pos2, ...), ...)
  565. *
  566. * @param Zend_Search_Lucene_Index_Term $term
  567. * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
  568. * @return array
  569. * @throws Zend_Search_Lucene_Exception
  570. */
  571. public function termPositions(Zend_Search_Lucene_Index_Term $term, $docsFilter = null)
  572. {
  573. if ($docsFilter != null) {
  574. require_once 'Zend/Search/Lucene/Exception.php';
  575. throw new Zend_Search_Lucene_Exception('Document filters could not used with multi-searcher');
  576. }
  577. $termPositionsList = array();
  578. $indexShift = 0;
  579. foreach ($this->_indices as $index) {
  580. $termPositions = $index->termPositions($term);
  581. if ($indexShift != 0) {
  582. $termPositionsShifted = array();
  583. foreach ($termPositions as $docId => $positions) {
  584. $termPositions[$docId + $indexShift] = $positions;
  585. }
  586. $termPositions = $termPositionsShifted;
  587. }
  588. $indexShift += $index->count();
  589. $termPositionsList[] = $termPositions;
  590. }
  591. return call_user_func_array('array_merge', $termPositions);
  592. }
  593. /**
  594. * Returns the number of documents in this index containing the $term.
  595. *
  596. * @param Zend_Search_Lucene_Index_Term $term
  597. * @return integer
  598. */
  599. public function docFreq(Zend_Search_Lucene_Index_Term $term)
  600. {
  601. $docFreq = 0;
  602. foreach ($this->_indices as $index) {
  603. $docFreq += $index->docFreq($term);
  604. }
  605. return $docFreq;
  606. }
  607. /**
  608. * Retrive similarity used by index reader
  609. *
  610. * @return Zend_Search_Lucene_Search_Similarity
  611. * @throws Zend_Search_Lucene_Exception
  612. */
  613. public function getSimilarity()
  614. {
  615. if (count($this->_indices) == 0) {
  616. require_once 'Zend/Search/Lucene/Exception.php';
  617. throw new Zend_Search_Lucene_Exception('Indices list is empty');
  618. }
  619. $similarity = reset($this->_indices)->getSimilarity();
  620. foreach ($this->_indices as $index) {
  621. if ($index->getSimilarity() !== $similarity) {
  622. require_once 'Zend/Search/Lucene/Exception.php';
  623. throw new Zend_Search_Lucene_Exception('Indices have different similarity.');
  624. }
  625. }
  626. return $similarity;
  627. }
  628. /**
  629. * Returns a normalization factor for "field, document" pair.
  630. *
  631. * @param integer $id
  632. * @param string $fieldName
  633. * @return float
  634. */
  635. public function norm($id, $fieldName)
  636. {
  637. foreach ($this->_indices as $index) {
  638. $indexCount = $index->count();
  639. if ($indexCount > $id) {
  640. return $index->norm($id, $fieldName);
  641. }
  642. $id -= $indexCount;
  643. }
  644. return null;
  645. }
  646. /**
  647. * Returns true if any documents have been deleted from this index.
  648. *
  649. * @return boolean
  650. */
  651. public function hasDeletions()
  652. {
  653. foreach ($this->_indices as $index) {
  654. if ($index->hasDeletions()) {
  655. return true;
  656. }
  657. }
  658. return false;
  659. }
  660. /**
  661. * Deletes a document from the index.
  662. * $id is an internal document id
  663. *
  664. * @param integer|Zend_Search_Lucene_Search_QueryHit $id
  665. * @throws Zend_Search_Lucene_Exception
  666. */
  667. public function delete($id)
  668. {
  669. foreach ($this->_indices as $index) {
  670. $indexCount = $index->count();
  671. if ($indexCount > $id) {
  672. $index->delete($id);
  673. return;
  674. }
  675. $id -= $indexCount;
  676. }
  677. require_once 'Zend/Search/Lucene/Exception.php';
  678. throw new Zend_Search_Lucene_Exception('Document id is out of the range.');
  679. }
  680. /**
  681. * Callback used to choose target index for new documents
  682. *
  683. * Function/method signature:
  684. * Zend_Search_Lucene_Interface callbackFunction(Zend_Search_Lucene_Document $document, array $indices);
  685. *
  686. * null means "default documents distributing algorithm"
  687. *
  688. * @var callback
  689. */
  690. protected $_documentDistributorCallBack = null;
  691. /**
  692. * Set callback for choosing target index.
  693. *
  694. * @param callback $callback
  695. * @throws Zend_Search_Lucene_Exception
  696. */
  697. public function setDocumentDistributorCallback($callback)
  698. {
  699. if ($callback !== null && !is_callable($callback)) {
  700. require_once 'Zend/Search/Lucene/Exception.php';
  701. throw new Zend_Search_Lucene_Exception('$callback parameter must be a valid callback.');
  702. }
  703. $this->_documentDistributorCallBack = $callback;
  704. }
  705. /**
  706. * Get callback for choosing target index.
  707. *
  708. * @return callback
  709. */
  710. public function getDocumentDistributorCallback()
  711. {
  712. return $this->_documentDistributorCallBack;
  713. }
  714. /**
  715. * Adds a document to this index.
  716. *
  717. * @param Zend_Search_Lucene_Document $document
  718. * @throws Zend_Search_Lucene_Exception
  719. */
  720. public function addDocument(Zend_Search_Lucene_Document $document)
  721. {
  722. if ($this->_documentDistributorCallBack !== null) {
  723. $index = call_user_func($this->_documentDistributorCallBack, $document, $this->_indices);
  724. } else {
  725. $index = $this->_indices[array_rand($this->_indices)];
  726. }
  727. $index->addDocument($document);
  728. }
  729. /**
  730. * Commit changes resulting from delete() or undeleteAll() operations.
  731. */
  732. public function commit()
  733. {
  734. foreach ($this->_indices as $index) {
  735. $index->commit();
  736. }
  737. }
  738. /**
  739. * Optimize index.
  740. *
  741. * Merges all segments into one
  742. */
  743. public function optimize()
  744. {
  745. foreach ($this->_indices as $index) {
  746. $index->optimise();
  747. }
  748. }
  749. /**
  750. * Returns an array of all terms in this index.
  751. *
  752. * @return array
  753. */
  754. public function terms()
  755. {
  756. $termsList = array();
  757. foreach ($this->_indices as $index) {
  758. $termsList[] = $index->terms();
  759. }
  760. return array_unique(call_user_func_array('array_merge', $termsList));
  761. }
  762. /**
  763. * Terms stream priority queue object
  764. *
  765. * @var Zend_Search_Lucene_TermStreamsPriorityQueue
  766. */
  767. private $_termsStream = null;
  768. /**
  769. * Reset terms stream.
  770. */
  771. public function resetTermsStream()
  772. {
  773. if ($this->_termsStream === null) {
  774. /** Zend_Search_Lucene_TermStreamsPriorityQueue */
  775. require_once 'Zend/Search/Lucene/TermStreamsPriorityQueue.php';
  776. $this->_termsStream = new Zend_Search_Lucene_TermStreamsPriorityQueue($this->_indices);
  777. } else {
  778. $this->_termsStream->resetTermsStream();
  779. }
  780. }
  781. /**
  782. * Skip terms stream up to specified term preffix.
  783. *
  784. * Prefix contains fully specified field info and portion of searched term
  785. *
  786. * @param Zend_Search_Lucene_Index_Term $prefix
  787. */
  788. public function skipTo(Zend_Search_Lucene_Index_Term $prefix)
  789. {
  790. $this->_termsStream->skipTo($prefix);
  791. }
  792. /**
  793. * Scans terms dictionary and returns next term
  794. *
  795. * @return Zend_Search_Lucene_Index_Term|null
  796. */
  797. public function nextTerm()
  798. {
  799. return $this->_termsStream->nextTerm();
  800. }
  801. /**
  802. * Returns term in current position
  803. *
  804. * @return Zend_Search_Lucene_Index_Term|null
  805. */
  806. public function currentTerm()
  807. {
  808. return $this->_termsStream->currentTerm();
  809. }
  810. /**
  811. * Close terms stream
  812. *
  813. * Should be used for resources clean up if stream is not read up to the end
  814. */
  815. public function closeTermsStream()
  816. {
  817. $this->_termsStream->closeTermsStream();
  818. $this->_termsStream = null;
  819. }
  820. /**
  821. * Undeletes all documents currently marked as deleted in this index.
  822. */
  823. public function undeleteAll()
  824. {
  825. foreach ($this->_indices as $index) {
  826. $index->undeleteAll();
  827. }
  828. }
  829. /**
  830. * Add reference to the index object
  831. *
  832. * @internal
  833. */
  834. public function addReference()
  835. {
  836. // Do nothing, since it's never referenced by indices
  837. }
  838. /**
  839. * Remove reference from the index object
  840. *
  841. * When reference count becomes zero, index is closed and resources are cleaned up
  842. *
  843. * @internal
  844. */
  845. public function removeReference()
  846. {
  847. // Do nothing, since it's never referenced by indices
  848. }
  849. }
  850. /**
  851. * This class is provided for backwards-compatibility (See ZF-12067)
  852. *
  853. * @category Zend
  854. * @package Zend_Search_Lucene
  855. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  856. * @license http://framework.zend.com/license/new-bsd New BSD License
  857. */
  858. class Zend_Search_Lucene_Interface_MultiSearcher
  859. extends Zend_Search_Lucene_MultiSearcher
  860. {
  861. }