--- /dev/null
+<?php\r
+/**\r
+ * Zend Framework\r
+ *\r
+ * LICENSE\r
+ *\r
+ * This source file is subject to the new BSD license that is bundled\r
+ * with this package in the file LICENSE.txt.\r
+ * It is also available through the world-wide-web at this URL:\r
+ * http://framework.zend.com/license/new-bsd\r
+ * If you did not receive a copy of the license and are unable to\r
+ * obtain it through the world-wide-web, please send an email\r
+ * to license@zend.com so we can send you a copy immediately.\r
+ *\r
+ * @category Zend\r
+ * @package Zend_Search_Lucene\r
+ * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)\r
+ * @license http://framework.zend.com/license/new-bsd New BSD License\r
+ * @version $Id: MultiSearcher.php 16971 2009-07-22 18:05:45Z mikaelkael $\r
+ */\r
+\r
+/** Zend_Search_Lucene_TermStreamsPriorityQueue */\r
+require_once 'Zend/Search/Lucene/TermStreamsPriorityQueue.php';\r
+\r
+/** Zend_Search_Lucene_Interface */\r
+require_once 'Zend/Search/Lucene/Interface.php';\r
+\r
+/**\r
+ * Multisearcher allows to search through several independent indexes.\r
+ *\r
+ * @category Zend\r
+ * @package Zend_Search_Lucene\r
+ * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)\r
+ * @license http://framework.zend.com/license/new-bsd New BSD License\r
+ */\r
+class Zend_Search_Lucene_Interface_MultiSearcher implements Zend_Search_Lucene_Interface\r
+{\r
+ /**\r
+ * List of indices for searching.\r
+ * Array of Zend_Search_Lucene_Interface objects\r
+ *\r
+ * @var array\r
+ */\r
+ protected $_indices;\r
+\r
+ /**\r
+ * Object constructor.\r
+ *\r
+ * @param array $indices Arrays of indices for search\r
+ * @throws Zend_Search_Lucene_Exception\r
+ */\r
+ public function __construct($indices = array())\r
+ {\r
+ $this->_indices = $indices;\r
+\r
+ foreach ($this->_indices as $index) {\r
+ if (!$index instanceof Zend_Search_Lucene_Interface) {\r
+ require_once 'Zend/Search/Lucene/Exception.php';\r
+ throw new Zend_Search_Lucene_Exception('sub-index objects have to implement Zend_Search_Lucene_Interface.');\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Add index for searching.\r
+ *\r
+ * @param Zend_Search_Lucene_Interface $index\r
+ */\r
+ public function addIndex(Zend_Search_Lucene_Interface $index)\r
+ {\r
+ $this->_indices[] = $index;\r
+ }\r
+\r
+\r
+ /**\r
+ * Get current generation number\r
+ *\r
+ * Returns generation number\r
+ * 0 means pre-2.1 index format\r
+ * -1 means there are no segments files.\r
+ *\r
+ * @param Zend_Search_Lucene_Storage_Directory $directory\r
+ * @return integer\r
+ * @throws Zend_Search_Lucene_Exception\r
+ */\r
+ public static function getActualGeneration(Zend_Search_Lucene_Storage_Directory $directory)\r
+ {\r
+ require_once 'Zend/Search/Lucene/Exception.php';\r
+ throw new Zend_Search_Lucene_Exception("Generation number can't be retrieved for multi-searcher");\r
+ }\r
+\r
+ /**\r
+ * Get segments file name\r
+ *\r
+ * @param integer $generation\r
+ * @return string\r
+ */\r
+ public static function getSegmentFileName($generation)\r
+ {\r
+ return Zend_Search_Lucene::getSegmentFileName($generation);\r
+ }\r
+\r
+ /**\r
+ * Get index format version\r
+ *\r
+ * @return integer\r
+ * @throws Zend_Search_Lucene_Exception\r
+ */\r
+ public function getFormatVersion()\r
+ {\r
+ require_once 'Zend/Search/Lucene/Exception.php';\r
+ throw new Zend_Search_Lucene_Exception("Format version can't be retrieved for multi-searcher");\r
+ }\r
+\r
+ /**\r
+ * Set index format version.\r
+ * Index is converted to this format at the nearest upfdate time\r
+ *\r
+ * @param int $formatVersion\r
+ */\r
+ public function setFormatVersion($formatVersion)\r
+ {\r
+ foreach ($this->_indices as $index) {\r
+ $index->setFormatVersion($formatVersion);\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Returns the Zend_Search_Lucene_Storage_Directory instance for this index.\r
+ *\r
+ * @return Zend_Search_Lucene_Storage_Directory\r
+ */\r
+ public function getDirectory()\r
+ {\r
+ require_once 'Zend/Search/Lucene/Exception.php';\r
+ throw new Zend_Search_Lucene_Exception("Index directory can't be retrieved for multi-searcher");\r
+ }\r
+\r
+ /**\r
+ * Returns the total number of documents in this index (including deleted documents).\r
+ *\r
+ * @return integer\r
+ */\r
+ public function count()\r
+ {\r
+ $count = 0;\r
+\r
+ foreach ($this->_indices as $index) {\r
+ $count += $this->_indices->count();\r
+ }\r
+\r
+ return $count;\r
+ }\r
+\r
+ /**\r
+ * Returns one greater than the largest possible document number.\r
+ * This may be used to, e.g., determine how big to allocate a structure which will have\r
+ * an element for every document number in an index.\r
+ *\r
+ * @return integer\r
+ */\r
+ public function maxDoc()\r
+ {\r
+ return $this->count();\r
+ }\r
+\r
+ /**\r
+ * Returns the total number of non-deleted documents in this index.\r
+ *\r
+ * @return integer\r
+ */\r
+ public function numDocs()\r
+ {\r
+ $docs = 0;\r
+\r
+ foreach ($this->_indices as $index) {\r
+ $docs += $this->_indices->numDocs();\r
+ }\r
+\r
+ return $docs;\r
+ }\r
+\r
+ /**\r
+ * Checks, that document is deleted\r
+ *\r
+ * @param integer $id\r
+ * @return boolean\r
+ * @throws Zend_Search_Lucene_Exception Exception is thrown if $id is out of the range\r
+ */\r
+ public function isDeleted($id)\r
+ {\r
+ foreach ($this->_indices as $index) {\r
+ $indexCount = $index->count();\r
+\r
+ if ($indexCount > $id) {\r
+ return $index->isDeleted($id);\r
+ }\r
+\r
+ $id -= $indexCount;\r
+ }\r
+\r
+ require_once 'Zend/Search/Lucene/Exception.php';\r
+ throw new Zend_Search_Lucene_Exception('Document id is out of the range.');\r
+ }\r
+\r
+ /**\r
+ * Set default search field.\r
+ *\r
+ * Null means, that search is performed through all fields by default\r
+ *\r
+ * Default value is null\r
+ *\r
+ * @param string $fieldName\r
+ */\r
+ public static function setDefaultSearchField($fieldName)\r
+ {\r
+ foreach ($this->_indices as $index) {\r
+ $index->setDefaultSearchField($fieldName);\r
+ }\r
+ }\r
+\r
+\r
+ /**\r
+ * Get default search field.\r
+ *\r
+ * Null means, that search is performed through all fields by default\r
+ *\r
+ * @return string\r
+ * @throws Zend_Search_Lucene_Exception\r
+ */\r
+ public static function getDefaultSearchField()\r
+ {\r
+ if (count($this->_indices) == 0) {\r
+ require_once 'Zend/Search/Lucene/Exception.php';\r
+ throw new Zend_Search_Lucene_Exception('Indices list is empty');\r
+ }\r
+\r
+ $defaultSearchField = reset($this->_indices)->getDefaultSearchField();\r
+\r
+ foreach ($this->_indices as $index) {\r
+ if ($index->getDefaultSearchField() !== $defaultSearchField) {\r
+ require_once 'Zend/Search/Lucene/Exception.php';\r
+ throw new Zend_Search_Lucene_Exception('Indices have different default search field.');\r
+ }\r
+ }\r
+\r
+ return $defaultSearchField;\r
+ }\r
+\r
+ /**\r
+ * Set result set limit.\r
+ *\r
+ * 0 (default) means no limit\r
+ *\r
+ * @param integer $limit\r
+ */\r
+ public static function setResultSetLimit($limit)\r
+ {\r
+ foreach ($this->_indices as $index) {\r
+ $index->setResultSetLimit($limit);\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Set result set limit.\r
+ *\r
+ * 0 means no limit\r
+ *\r
+ * @return integer\r
+ * @throws Zend_Search_Lucene_Exception\r
+ */\r
+ public static function getResultSetLimit()\r
+ {\r
+ if (count($this->_indices) == 0) {\r
+ require_once 'Zend/Search/Lucene/Exception.php';\r
+ throw new Zend_Search_Lucene_Exception('Indices list is empty');\r
+ }\r
+\r
+ $defaultResultSetLimit = reset($this->_indices)->getResultSetLimit();\r
+\r
+ foreach ($this->_indices as $index) {\r
+ if ($index->getResultSetLimit() !== $defaultResultSetLimit) {\r
+ require_once 'Zend/Search/Lucene/Exception.php';\r
+ throw new Zend_Search_Lucene_Exception('Indices have different default search field.');\r
+ }\r
+ }\r
+\r
+ return $defaultResultSetLimit;\r
+ }\r
+\r
+ /**\r
+ * Retrieve index maxBufferedDocs option\r
+ *\r
+ * maxBufferedDocs is a minimal number of documents required before\r
+ * the buffered in-memory documents are written into a new Segment\r
+ *\r
+ * Default value is 10\r
+ *\r
+ * @return integer\r
+ * @throws Zend_Search_Lucene_Exception\r
+ */\r
+ public function getMaxBufferedDocs()\r
+ {\r
+ if (count($this->_indices) == 0) {\r
+ require_once 'Zend/Search/Lucene/Exception.php';\r
+ throw new Zend_Search_Lucene_Exception('Indices list is empty');\r
+ }\r
+\r
+ $maxBufferedDocs = reset($this->_indices)->getMaxBufferedDocs();\r
+\r
+ foreach ($this->_indices as $index) {\r
+ if ($index->getMaxBufferedDocs() !== $maxBufferedDocs) {\r
+ require_once 'Zend/Search/Lucene/Exception.php';\r
+ throw new Zend_Search_Lucene_Exception('Indices have different default search field.');\r
+ }\r
+ }\r
+\r
+ return $maxBufferedDocs;\r
+ }\r
+\r
+ /**\r
+ * Set index maxBufferedDocs option\r
+ *\r
+ * maxBufferedDocs is a minimal number of documents required before\r
+ * the buffered in-memory documents are written into a new Segment\r
+ *\r
+ * Default value is 10\r
+ *\r
+ * @param integer $maxBufferedDocs\r
+ */\r
+ public function setMaxBufferedDocs($maxBufferedDocs)\r
+ {\r
+ foreach ($this->_indices as $index) {\r
+ $index->setMaxBufferedDocs($maxBufferedDocs);\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Retrieve index maxMergeDocs option\r
+ *\r
+ * maxMergeDocs is a largest number of documents ever merged by addDocument().\r
+ * Small values (e.g., less than 10,000) are best for interactive indexing,\r
+ * as this limits the length of pauses while indexing to a few seconds.\r
+ * Larger values are best for batched indexing and speedier searches.\r
+ *\r
+ * Default value is PHP_INT_MAX\r
+ *\r
+ * @return integer\r
+ * @throws Zend_Search_Lucene_Exception\r
+ */\r
+ public function getMaxMergeDocs()\r
+ {\r
+ if (count($this->_indices) == 0) {\r
+ require_once 'Zend/Search/Lucene/Exception.php';\r
+ throw new Zend_Search_Lucene_Exception('Indices list is empty');\r
+ }\r
+\r
+ $maxMergeDocs = reset($this->_indices)->getMaxMergeDocs();\r
+\r
+ foreach ($this->_indices as $index) {\r
+ if ($index->getMaxMergeDocs() !== $maxMergeDocs) {\r
+ require_once 'Zend/Search/Lucene/Exception.php';\r
+ throw new Zend_Search_Lucene_Exception('Indices have different default search field.');\r
+ }\r
+ }\r
+\r
+ return $maxMergeDocs;\r
+ }\r
+\r
+ /**\r
+ * Set index maxMergeDocs option\r
+ *\r
+ * maxMergeDocs is a largest number of documents ever merged by addDocument().\r
+ * Small values (e.g., less than 10,000) are best for interactive indexing,\r
+ * as this limits the length of pauses while indexing to a few seconds.\r
+ * Larger values are best for batched indexing and speedier searches.\r
+ *\r
+ * Default value is PHP_INT_MAX\r
+ *\r
+ * @param integer $maxMergeDocs\r
+ */\r
+ public function setMaxMergeDocs($maxMergeDocs)\r
+ {\r
+ foreach ($this->_indices as $index) {\r
+ $index->setMaxMergeDocs($maxMergeDocs);\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Retrieve index mergeFactor option\r
+ *\r
+ * mergeFactor determines how often segment indices are merged by addDocument().\r
+ * With smaller values, less RAM is used while indexing,\r
+ * and searches on unoptimized indices are faster,\r
+ * but indexing speed is slower.\r
+ * With larger values, more RAM is used during indexing,\r
+ * and while searches on unoptimized indices are slower,\r
+ * indexing is faster.\r
+ * Thus larger values (> 10) are best for batch index creation,\r
+ * and smaller values (< 10) for indices that are interactively maintained.\r
+ *\r
+ * Default value is 10\r
+ *\r
+ * @return integer\r
+ * @throws Zend_Search_Lucene_Exception\r
+ */\r
+ public function getMergeFactor()\r
+ {\r
+ if (count($this->_indices) == 0) {\r
+ require_once 'Zend/Search/Lucene/Exception.php';\r
+ throw new Zend_Search_Lucene_Exception('Indices list is empty');\r
+ }\r
+\r
+ $mergeFactor = reset($this->_indices)->getMergeFactor();\r
+\r
+ foreach ($this->_indices as $index) {\r
+ if ($index->getMergeFactor() !== $mergeFactor) {\r
+ require_once 'Zend/Search/Lucene/Exception.php';\r
+ throw new Zend_Search_Lucene_Exception('Indices have different default search field.');\r
+ }\r
+ }\r
+\r
+ return $mergeFactor;\r
+ }\r
+\r
+ /**\r
+ * Set index mergeFactor option\r
+ *\r
+ * mergeFactor determines how often segment indices are merged by addDocument().\r
+ * With smaller values, less RAM is used while indexing,\r
+ * and searches on unoptimized indices are faster,\r
+ * but indexing speed is slower.\r
+ * With larger values, more RAM is used during indexing,\r
+ * and while searches on unoptimized indices are slower,\r
+ * indexing is faster.\r
+ * Thus larger values (> 10) are best for batch index creation,\r
+ * and smaller values (< 10) for indices that are interactively maintained.\r
+ *\r
+ * Default value is 10\r
+ *\r
+ * @param integer $maxMergeDocs\r
+ */\r
+ public function setMergeFactor($mergeFactor)\r
+ {\r
+ foreach ($this->_indices as $index) {\r
+ $index->setMaxMergeDocs($maxMergeDocs);\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Performs a query against the index and returns an array\r
+ * of Zend_Search_Lucene_Search_QueryHit objects.\r
+ * Input is a string or Zend_Search_Lucene_Search_Query.\r
+ *\r
+ * @param mixed $query\r
+ * @return array Zend_Search_Lucene_Search_QueryHit\r
+ * @throws Zend_Search_Lucene_Exception\r
+ */\r
+ public function find($query)\r
+ {\r
+ $hitsList = array();\r
+\r
+ $indexShift = 0;\r
+ foreach ($this->_indices as $index) {\r
+ $hits = $index->find($query);\r
+\r
+ if ($indexShift != 0) {\r
+ foreach ($hits as $hit) {\r
+ $hit->id += $indexShift;\r
+ }\r
+ }\r
+\r
+ $indexShift += $index->count();\r
+ $hitsList[] = $hits;\r
+ }\r
+\r
+ /** @todo Implement advanced sorting */\r
+\r
+ return call_user_func_array('array_merge', $hitsList);\r
+ }\r
+\r
+ /**\r
+ * Returns a list of all unique field names that exist in this index.\r
+ *\r
+ * @param boolean $indexed\r
+ * @return array\r
+ */\r
+ public function getFieldNames($indexed = false)\r
+ {\r
+ $fieldNamesList = array();\r
+\r
+ foreach ($this->_indices as $index) {\r
+ $fieldNamesList[] = $index->getFieldNames($indexed);\r
+ }\r
+\r
+ return array_unique(call_user_func_array('array_merge', $fieldNamesList));\r
+ }\r
+\r
+ /**\r
+ * Returns a Zend_Search_Lucene_Document object for the document\r
+ * number $id in this index.\r
+ *\r
+ * @param integer|Zend_Search_Lucene_Search_QueryHit $id\r
+ * @return Zend_Search_Lucene_Document\r
+ * @throws Zend_Search_Lucene_Exception Exception is thrown if $id is out of the range\r
+ */\r
+ public function getDocument($id)\r
+ {\r
+ if ($id instanceof Zend_Search_Lucene_Search_QueryHit) {\r
+ /* @var $id Zend_Search_Lucene_Search_QueryHit */\r
+ $id = $id->id;\r
+ }\r
+\r
+ foreach ($this->_indices as $index) {\r
+ $indexCount = $index->count();\r
+\r
+ if ($indexCount > $id) {\r
+ return $index->getDocument($id);\r
+ }\r
+\r
+ $id -= $indexCount;\r
+ }\r
+\r
+ require_once 'Zend/Search/Lucene/Exception.php';\r
+ throw new Zend_Search_Lucene_Exception('Document id is out of the range.');\r
+ }\r
+\r
+ /**\r
+ * Returns true if index contain documents with specified term.\r
+ *\r
+ * Is used for query optimization.\r
+ *\r
+ * @param Zend_Search_Lucene_Index_Term $term\r
+ * @return boolean\r
+ */\r
+ public function hasTerm(Zend_Search_Lucene_Index_Term $term)\r
+ {\r
+ foreach ($this->_indices as $index) {\r
+ if ($index->hasTerm($term)) {\r
+ return true;\r
+ }\r
+ }\r
+\r
+ return false;\r
+ }\r
+\r
+ /**\r
+ * Returns IDs of all the documents containing term.\r
+ *\r
+ * @param Zend_Search_Lucene_Index_Term $term\r
+ * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter\r
+ * @return array\r
+ * @throws Zend_Search_Lucene_Exception\r
+ */\r
+ public function termDocs(Zend_Search_Lucene_Index_Term $term, $docsFilter = null)\r
+ {\r
+ if ($docsFilter != null) {\r
+ require_once 'Zend/Search/Lucene/Exception.php';\r
+ throw new Zend_Search_Lucene_Exception('Document filters could not used with multi-searcher');\r
+ }\r
+\r
+ $docsList = array();\r
+\r
+ $indexShift = 0;\r
+ foreach ($this->_indices as $index) {\r
+ $docs = $index->termDocs($term);\r
+\r
+ if ($indexShift != 0) {\r
+ foreach ($docs as $id => $docId) {\r
+ $docs[$id] += $indexShift;\r
+ }\r
+ }\r
+\r
+ $indexShift += $index->count();\r
+ $docsList[] = $docs;\r
+ }\r
+\r
+ return call_user_func_array('array_merge', $docsList);\r
+ }\r
+\r
+ /**\r
+ * Returns documents filter for all documents containing term.\r
+ *\r
+ * It performs the same operation as termDocs, but return result as\r
+ * Zend_Search_Lucene_Index_DocsFilter object\r
+ *\r
+ * @param Zend_Search_Lucene_Index_Term $term\r
+ * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter\r
+ * @return Zend_Search_Lucene_Index_DocsFilter\r
+ * @throws Zend_Search_Lucene_Exception\r
+ */\r
+ public function termDocsFilter(Zend_Search_Lucene_Index_Term $term, $docsFilter = null)\r
+ {\r
+ require_once 'Zend/Search/Lucene/Exception.php';\r
+ throw new Zend_Search_Lucene_Exception('Document filters could not used with multi-searcher');\r
+ }\r
+\r
+ /**\r
+ * Returns an array of all term freqs.\r
+ * Return array structure: array( docId => freq, ...)\r
+ *\r
+ * @param Zend_Search_Lucene_Index_Term $term\r
+ * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter\r
+ * @return integer\r
+ * @throws Zend_Search_Lucene_Exception\r
+ */\r
+ public function termFreqs(Zend_Search_Lucene_Index_Term $term, $docsFilter = null)\r
+ {\r
+ if ($docsFilter != null) {\r
+ require_once 'Zend/Search/Lucene/Exception.php';\r
+ throw new Zend_Search_Lucene_Exception('Document filters could not used with multi-searcher');\r
+ }\r
+\r
+ $freqsList = array();\r
+\r
+ $indexShift = 0;\r
+ foreach ($this->_indices as $index) {\r
+ $freqs = $index->termFreqs($term);\r
+\r
+ if ($indexShift != 0) {\r
+ $freqsShifted = array();\r
+\r
+ foreach ($freqs as $docId => $freq) {\r
+ $freqsShifted[$docId + $indexShift] = $freq;\r
+ }\r
+ $freqs = $freqsShifted;\r
+ }\r
+\r
+ $indexShift += $index->count();\r
+ $freqsList[] = $freqs;\r
+ }\r
+\r
+ return call_user_func_array('array_merge', $freqsList);\r
+ }\r
+\r
+ /**\r
+ * Returns an array of all term positions in the documents.\r
+ * Return array structure: array( docId => array( pos1, pos2, ...), ...)\r
+ *\r
+ * @param Zend_Search_Lucene_Index_Term $term\r
+ * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter\r
+ * @return array\r
+ * @throws Zend_Search_Lucene_Exception\r
+ */\r
+ public function termPositions(Zend_Search_Lucene_Index_Term $term, $docsFilter = null)\r
+ {\r
+ if ($docsFilter != null) {\r
+ require_once 'Zend/Search/Lucene/Exception.php';\r
+ throw new Zend_Search_Lucene_Exception('Document filters could not used with multi-searcher');\r
+ }\r
+\r
+ $termPositionsList = array();\r
+\r
+ $indexShift = 0;\r
+ foreach ($this->_indices as $index) {\r
+ $termPositions = $index->termPositions($term);\r
+\r
+ if ($indexShift != 0) {\r
+ $termPositionsShifted = array();\r
+\r
+ foreach ($termPositions as $docId => $positions) {\r
+ $termPositions[$docId + $indexShift] = $positions;\r
+ }\r
+ $termPositions = $termPositionsShifted;\r
+ }\r
+\r
+ $indexShift += $index->count();\r
+ $termPositionsList[] = $termPositions;\r
+ }\r
+\r
+ return call_user_func_array('array_merge', $termPositions);\r
+ }\r
+\r
+ /**\r
+ * Returns the number of documents in this index containing the $term.\r
+ *\r
+ * @param Zend_Search_Lucene_Index_Term $term\r
+ * @return integer\r
+ */\r
+ public function docFreq(Zend_Search_Lucene_Index_Term $term)\r
+ {\r
+ $docFreq = 0;\r
+\r
+ foreach ($this->_indices as $index) {\r
+ $docFreq += $index->docFreq($term);\r
+ }\r
+\r
+ return $docFreq;\r
+ }\r
+\r
+ /**\r
+ * Retrive similarity used by index reader\r
+ *\r
+ * @return Zend_Search_Lucene_Search_Similarity\r
+ * @throws Zend_Search_Lucene_Exception\r
+ */\r
+ public function getSimilarity()\r
+ {\r
+ if (count($this->_indices) == 0) {\r
+ require_once 'Zend/Search/Lucene/Exception.php';\r
+ throw new Zend_Search_Lucene_Exception('Indices list is empty');\r
+ }\r
+\r
+ $similarity = reset($this->_indices)->getSimilarity();\r
+\r
+ foreach ($this->_indices as $index) {\r
+ if ($index->getSimilarity() !== $similarity) {\r
+ require_once 'Zend/Search/Lucene/Exception.php';\r
+ throw new Zend_Search_Lucene_Exception('Indices have different similarity.');\r
+ }\r
+ }\r
+\r
+ return $similarity;\r
+ }\r
+\r
+ /**\r
+ * Returns a normalization factor for "field, document" pair.\r
+ *\r
+ * @param integer $id\r
+ * @param string $fieldName\r
+ * @return float\r
+ */\r
+ public function norm($id, $fieldName)\r
+ {\r
+ foreach ($this->_indices as $index) {\r
+ $indexCount = $index->count();\r
+\r
+ if ($indexCount > $id) {\r
+ return $index->norm($id, $fieldName);\r
+ }\r
+\r
+ $id -= $indexCount;\r
+ }\r
+\r
+ return null;\r
+ }\r
+\r
+ /**\r
+ * Returns true if any documents have been deleted from this index.\r
+ *\r
+ * @return boolean\r
+ */\r
+ public function hasDeletions()\r
+ {\r
+ foreach ($this->_indices as $index) {\r
+ if ($index->hasDeletions()) {\r
+ return true;\r
+ }\r
+ }\r
+\r
+ return false;\r
+ }\r
+\r
+ /**\r
+ * Deletes a document from the index.\r
+ * $id is an internal document id\r
+ *\r
+ * @param integer|Zend_Search_Lucene_Search_QueryHit $id\r
+ * @throws Zend_Search_Lucene_Exception\r
+ */\r
+ public function delete($id)\r
+ {\r
+ foreach ($this->_indices as $index) {\r
+ $indexCount = $index->count();\r
+\r
+ if ($indexCount > $id) {\r
+ $index->delete($id);\r
+ return;\r
+ }\r
+\r
+ $id -= $indexCount;\r
+ }\r
+\r
+ require_once 'Zend/Search/Lucene/Exception.php';\r
+ throw new Zend_Search_Lucene_Exception('Document id is out of the range.');\r
+ }\r
+\r
+\r
+ /**\r
+ * Callback used to choose target index for new documents\r
+ *\r
+ * Function/method signature:\r
+ * Zend_Search_Lucene_Interface callbackFunction(Zend_Search_Lucene_Document $document, array $indices);\r
+ *\r
+ * null means "default documents distributing algorithm"\r
+ *\r
+ * @var callback\r
+ */\r
+ protected $_documentDistributorCallBack = null;\r
+\r
+ /**\r
+ * Set callback for choosing target index.\r
+ *\r
+ * @param callback $callback\r
+ */\r
+ public function setDocumentDistributorCallback($callback)\r
+ {\r
+ if ($callback !== null && !is_callable($callback))\r
+ $this->_documentDistributorCallBack = $callback;\r
+ }\r
+\r
+ /**\r
+ * Get callback for choosing target index.\r
+ *\r
+ * @return callback\r
+ */\r
+ public function getDocumentDistributorCallback()\r
+ {\r
+ return $this->_documentDistributorCallBack;\r
+ }\r
+\r
+ /**\r
+ * Adds a document to this index.\r
+ *\r
+ * @param Zend_Search_Lucene_Document $document\r
+ * @throws Zend_Search_Lucene_Exception\r
+ */\r
+ public function addDocument(Zend_Search_Lucene_Document $document)\r
+ {\r
+ if ($this->_documentDistributorCallBack !== null) {\r
+ $index = call_user_func($this->_documentDistributorCallBack, $document, $this->_indices);\r
+ } else {\r
+ $index = $this->_indices[ array_rand($this->_indices) ];\r
+ }\r
+\r
+ $index->addDocument($document);\r
+ }\r
+\r
+ /**\r
+ * Commit changes resulting from delete() or undeleteAll() operations.\r
+ */\r
+ public function commit()\r
+ {\r
+ foreach ($this->_indices as $index) {\r
+ $index->commit();\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Optimize index.\r
+ *\r
+ * Merges all segments into one\r
+ */\r
+ public function optimize()\r
+ {\r
+ foreach ($this->_indices as $index) {\r
+ $index->_optimise();\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Returns an array of all terms in this index.\r
+ *\r
+ * @return array\r
+ */\r
+ public function terms()\r
+ {\r
+ $termsList = array();\r
+\r
+ foreach ($this->_indices as $index) {\r
+ $termsList[] = $index->terms();\r
+ }\r
+\r
+ return array_unique(call_user_func_array('array_merge', $termsList));\r
+ }\r
+\r
+\r
+ /**\r
+ * Terms stream priority queue object\r
+ *\r
+ * @var Zend_Search_Lucene_TermStreamsPriorityQueue\r
+ */\r
+ private $_termsStream = null;\r
+\r
+ /**\r
+ * Reset terms stream.\r
+ */\r
+ public function resetTermsStream()\r
+ {\r
+ if ($this->_termsStream === null) {\r
+ $this->_termsStream = new Zend_Search_Lucene_TermStreamsPriorityQueue($this->_indices);\r
+ } else {\r
+ $this->_termsStream->resetTermsStream();\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Skip terms stream up to specified term preffix.\r
+ *\r
+ * Prefix contains fully specified field info and portion of searched term\r
+ *\r
+ * @param Zend_Search_Lucene_Index_Term $prefix\r
+ */\r
+ public function skipTo(Zend_Search_Lucene_Index_Term $prefix)\r
+ {\r
+ $this->_termsStream->skipTo($prefix);\r
+ }\r
+\r
+ /**\r
+ * Scans terms dictionary and returns next term\r
+ *\r
+ * @return Zend_Search_Lucene_Index_Term|null\r
+ */\r
+ public function nextTerm()\r
+ {\r
+ return $this->_termsStream->nextTerm();\r
+ }\r
+\r
+ /**\r
+ * Returns term in current position\r
+ *\r
+ * @return Zend_Search_Lucene_Index_Term|null\r
+ */\r
+ public function currentTerm()\r
+ {\r
+ return $this->_termsStream->currentTerm();\r
+ }\r
+\r
+ /**\r
+ * Close terms stream\r
+ *\r
+ * Should be used for resources clean up if stream is not read up to the end\r
+ */\r
+ public function closeTermsStream()\r
+ {\r
+ $this->_termsStream->closeTermsStream();\r
+ $this->_termsStream = null;\r
+ }\r
+\r
+\r
+ /**\r
+ * Undeletes all documents currently marked as deleted in this index.\r
+ */\r
+ public function undeleteAll()\r
+ {\r
+ foreach ($this->_indices as $index) {\r
+ $index->undeleteAll();\r
+ }\r
+ }\r
+\r
+\r
+ /**\r
+ * Add reference to the index object\r
+ *\r
+ * @internal\r
+ */\r
+ public function addReference()\r
+ {\r
+ // Do nothing, since it's never referenced by indices\r
+ }\r
+\r
+ /**\r
+ * Remove reference from the index object\r
+ *\r
+ * When reference count becomes zero, index is closed and resources are cleaned up\r
+ *\r
+ * @internal\r
+ */\r
+ public function removeReference()\r
+ {\r
+ // Do nothing, since it's never referenced by indices\r
+ }\r
+}\r