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.
16 * @package Zend_Search_Lucene
17 * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
21 /** Zend_Search_Lucene_Interface */
22 require_once $CFG->dirroot
.'/search/Zend/Search/Lucene/Interface.php';
26 * Proxy class intended to be used in userland.
28 * It tracks, when index object goes out of scope and forces ndex closing
31 * @package Zend_Search_Lucene
32 * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
33 * @license http://framework.zend.com/license/new-bsd New BSD License
35 class Zend_Search_Lucene_Proxy
implements Zend_Search_Lucene_Interface
40 * @var Zend_Search_Lucene_Interface
47 * @param Zend_Search_Lucene_Interface $index
49 public function __construct(Zend_Search_Lucene_Interface
$index)
51 $this->_index
= $index;
52 $this->_index
->addReference();
58 public function __destruct()
60 if ($this->_index
!== null) {
61 // This code is invoked if Zend_Search_Lucene_Interface object constructor throws an exception
62 $this->_index
->removeReference();
68 * Returns the Zend_Search_Lucene_Storage_Directory instance for this index.
70 * @return Zend_Search_Lucene_Storage_Directory
72 public function getDirectory()
74 return $this->_index
->getDirectory();
78 * Returns the total number of documents in this index (including deleted documents).
82 public function count()
84 return $this->_index
->count();
88 * Returns one greater than the largest possible document number.
89 * This may be used to, e.g., determine how big to allocate a structure which will have
90 * an element for every document number in an index.
94 public function maxDoc()
96 return $this->_index
->maxDoc();
100 * Returns the total number of non-deleted documents in this index.
104 public function numDocs()
106 return $this->_index
->numDocs();
110 * Checks, that document is deleted
114 * @throws Zend_Search_Lucene_Exception Exception is thrown if $id is out of the range
116 public function isDeleted($id)
118 return $this->_index
->isDeleted($id);
122 * Set default search field.
124 * Null means, that search is performed through all fields by default
126 * Default value is null
128 * @param string $fieldName
130 public static function setDefaultSearchField($fieldName)
132 Zend_Search_Lucene
::setDefaultSearchField($fieldName);
136 * Get default search field.
138 * Null means, that search is performed through all fields by default
142 public static function getDefaultSearchField()
144 return Zend_Search_Lucene
::getDefaultSearchField();
148 * Retrieve index maxBufferedDocs option
150 * maxBufferedDocs is a minimal number of documents required before
151 * the buffered in-memory documents are written into a new Segment
153 * Default value is 10
157 public function getMaxBufferedDocs()
159 return $this->_index
->getMaxBufferedDocs();
163 * Set index maxBufferedDocs option
165 * maxBufferedDocs is a minimal number of documents required before
166 * the buffered in-memory documents are written into a new Segment
168 * Default value is 10
170 * @param integer $maxBufferedDocs
172 public function setMaxBufferedDocs($maxBufferedDocs)
174 $this->_index
->setMaxBufferedDocs($maxBufferedDocs);
179 * Retrieve index maxMergeDocs option
181 * maxMergeDocs is a largest number of documents ever merged by addDocument().
182 * Small values (e.g., less than 10,000) are best for interactive indexing,
183 * as this limits the length of pauses while indexing to a few seconds.
184 * Larger values are best for batched indexing and speedier searches.
186 * Default value is PHP_INT_MAX
190 public function getMaxMergeDocs()
192 return $this->_index
->getMaxMergeDocs();
196 * Set index maxMergeDocs option
198 * maxMergeDocs is a largest number of documents ever merged by addDocument().
199 * Small values (e.g., less than 10,000) are best for interactive indexing,
200 * as this limits the length of pauses while indexing to a few seconds.
201 * Larger values are best for batched indexing and speedier searches.
203 * Default value is PHP_INT_MAX
205 * @param integer $maxMergeDocs
207 public function setMaxMergeDocs($maxMergeDocs)
209 $this->_index
->setMaxMergeDocs($maxMergeDocs);
214 * Retrieve index mergeFactor option
216 * mergeFactor determines how often segment indices are merged by addDocument().
217 * With smaller values, less RAM is used while indexing,
218 * and searches on unoptimized indices are faster,
219 * but indexing speed is slower.
220 * With larger values, more RAM is used during indexing,
221 * and while searches on unoptimized indices are slower,
222 * indexing is faster.
223 * Thus larger values (> 10) are best for batch index creation,
224 * and smaller values (< 10) for indices that are interactively maintained.
226 * Default value is 10
230 public function getMergeFactor()
232 return $this->_index
->getMergeFactor();
236 * Set index mergeFactor option
238 * mergeFactor determines how often segment indices are merged by addDocument().
239 * With smaller values, less RAM is used while indexing,
240 * and searches on unoptimized indices are faster,
241 * but indexing speed is slower.
242 * With larger values, more RAM is used during indexing,
243 * and while searches on unoptimized indices are slower,
244 * indexing is faster.
245 * Thus larger values (> 10) are best for batch index creation,
246 * and smaller values (< 10) for indices that are interactively maintained.
248 * Default value is 10
250 * @param integer $maxMergeDocs
252 public function setMergeFactor($mergeFactor)
254 $this->_index
->setMergeFactor($mergeFactor);
258 * Performs a query against the index and returns an array
259 * of Zend_Search_Lucene_Search_QueryHit objects.
260 * Input is a string or Zend_Search_Lucene_Search_Query.
262 * @param mixed $query
263 * @return array Zend_Search_Lucene_Search_QueryHit
264 * @throws Zend_Search_Lucene_Exception
266 public function find($query)
268 // actual parameter list
269 $parameters = func_get_args();
271 // invoke $this->_index->find() method with specified parameters
272 return call_user_func_array(array(&$this->_index
, 'find'), $parameters);
276 * Returns a list of all unique field names that exist in this index.
278 * @param boolean $indexed
281 public function getFieldNames($indexed = false)
283 return $this->_index
->getFieldNames($indexed);
287 * Returns a Zend_Search_Lucene_Document object for the document
288 * number $id in this index.
290 * @param integer|Zend_Search_Lucene_Search_QueryHit $id
291 * @return Zend_Search_Lucene_Document
293 public function getDocument($id)
295 return $this->_index
->getDocument($id);
299 * Returns true if index contain documents with specified term.
301 * Is used for query optimization.
303 * @param Zend_Search_Lucene_Index_Term $term
306 public function hasTerm(Zend_Search_Lucene_Index_Term
$term)
308 return $this->_index
->hasTerm($term);
312 * Returns IDs of all the documents containing term.
314 * @param Zend_Search_Lucene_Index_Term $term
317 public function termDocs(Zend_Search_Lucene_Index_Term
$term)
319 return $this->_index
->termDocs($term);
323 * Returns an array of all term freqs.
324 * Return array structure: array( docId => freq, ...)
326 * @param Zend_Search_Lucene_Index_Term $term
329 public function termFreqs(Zend_Search_Lucene_Index_Term
$term)
331 return $this->_index
->termFreqs($term);
335 * Returns an array of all term positions in the documents.
336 * Return array structure: array( docId => array( pos1, pos2, ...), ...)
338 * @param Zend_Search_Lucene_Index_Term $term
341 public function termPositions(Zend_Search_Lucene_Index_Term
$term)
343 return $this->_index
->termPositions($term);
347 * Returns the number of documents in this index containing the $term.
349 * @param Zend_Search_Lucene_Index_Term $term
352 public function docFreq(Zend_Search_Lucene_Index_Term
$term)
354 return $this->_index
->docFreq($term);
358 * Retrive similarity used by index reader
360 * @return Zend_Search_Lucene_Search_Similarity
362 public function getSimilarity()
364 return $this->_index
->getSimilarity();
368 * Returns a normalization factor for "field, document" pair.
371 * @param string $fieldName
374 public function norm($id, $fieldName)
376 return $this->_index
->norm($id, $fieldName);
380 * Returns true if any documents have been deleted from this index.
384 public function hasDeletions()
386 return $this->_index
->hasDeletions();
390 * Deletes a document from the index.
391 * $id is an internal document id
393 * @param integer|Zend_Search_Lucene_Search_QueryHit $id
394 * @throws Zend_Search_Lucene_Exception
396 public function delete($id)
398 return $this->_index
->delete($id);
402 * Adds a document to this index.
404 * @param Zend_Search_Lucene_Document $document
406 public function addDocument(Zend_Search_Lucene_Document
$document)
408 $this->_index
->addDocument($document);
412 * Commit changes resulting from delete() or undeleteAll() operations.
414 public function commit()
416 $this->_index
->commit();
422 * Merges all segments into one
424 public function optimize()
426 $this->_index
->optimize();
430 * Returns an array of all terms in this index.
434 public function terms()
436 return $this->_index
->terms();
440 * Undeletes all documents currently marked as deleted in this index.
442 public function undeleteAll()
444 return $this->_index
->undeleteAll();
448 * Add reference to the index object
452 public function addReference()
454 return $this->_index
->addReference();
458 * Remove reference from the index object
460 * When reference count becomes zero, index is closed and resources are cleaned up
464 public function removeReference()
466 return $this->_index
->removeReference();