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-2008 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 '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-2008 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
* Get current generation number
70
* Returns generation number
71
* 0 means pre-2.1 index format
72
* -1 means there are no segments files.
74
* @param Zend_Search_Lucene_Storage_Directory $directory
76
* @throws Zend_Search_Lucene_Exception
78
public static function getActualGeneration(Zend_Search_Lucene_Storage_Directory $directory)
80
Zend_Search_Lucene::getActualGeneration($directory);
84
* Get segments file name
86
* @param integer $generation
89
public static function getSegmentFileName($generation)
91
Zend_Search_Lucene::getSegmentFileName($generation);
95
* Get index format version
99
public function getFormatVersion()
101
return $this->_index->getFormatVersion();
105
* Set index format version.
106
* Index is converted to this format at the nearest upfdate time
108
* @param int $formatVersion
109
* @throws Zend_Search_Lucene_Exception
111
public function setFormatVersion($formatVersion)
113
$this->_index->setFormatVersion($formatVersion);
117
* Returns the Zend_Search_Lucene_Storage_Directory instance for this index.
119
* @return Zend_Search_Lucene_Storage_Directory
121
public function getDirectory()
123
return $this->_index->getDirectory();
127
* Returns the total number of documents in this index (including deleted documents).
131
public function count()
133
return $this->_index->count();
137
* Returns one greater than the largest possible document number.
138
* This may be used to, e.g., determine how big to allocate a structure which will have
139
* an element for every document number in an index.
143
public function maxDoc()
145
return $this->_index->maxDoc();
149
* Returns the total number of non-deleted documents in this index.
153
public function numDocs()
155
return $this->_index->numDocs();
159
* Checks, that document is deleted
163
* @throws Zend_Search_Lucene_Exception Exception is thrown if $id is out of the range
165
public function isDeleted($id)
167
return $this->_index->isDeleted($id);
171
* Set default search field.
173
* Null means, that search is performed through all fields by default
175
* Default value is null
177
* @param string $fieldName
179
public static function setDefaultSearchField($fieldName)
181
Zend_Search_Lucene::setDefaultSearchField($fieldName);
185
* Get default search field.
187
* Null means, that search is performed through all fields by default
191
public static function getDefaultSearchField()
193
return Zend_Search_Lucene::getDefaultSearchField();
197
* Set result set limit.
199
* 0 (default) means no limit
201
* @param integer $limit
203
public static function setResultSetLimit($limit)
205
Zend_Search_Lucene::setResultSetLimit($limit);
209
* Set result set limit.
215
public static function getResultSetLimit()
217
return Zend_Search_Lucene::getResultSetLimit();
221
* Retrieve index maxBufferedDocs option
223
* maxBufferedDocs is a minimal number of documents required before
224
* the buffered in-memory documents are written into a new Segment
226
* Default value is 10
230
public function getMaxBufferedDocs()
232
return $this->_index->getMaxBufferedDocs();
236
* Set index maxBufferedDocs option
238
* maxBufferedDocs is a minimal number of documents required before
239
* the buffered in-memory documents are written into a new Segment
241
* Default value is 10
243
* @param integer $maxBufferedDocs
245
public function setMaxBufferedDocs($maxBufferedDocs)
247
$this->_index->setMaxBufferedDocs($maxBufferedDocs);
252
* Retrieve index maxMergeDocs option
254
* maxMergeDocs is a largest number of documents ever merged by addDocument().
255
* Small values (e.g., less than 10,000) are best for interactive indexing,
256
* as this limits the length of pauses while indexing to a few seconds.
257
* Larger values are best for batched indexing and speedier searches.
259
* Default value is PHP_INT_MAX
263
public function getMaxMergeDocs()
265
return $this->_index->getMaxMergeDocs();
269
* Set index maxMergeDocs option
271
* maxMergeDocs is a largest number of documents ever merged by addDocument().
272
* Small values (e.g., less than 10,000) are best for interactive indexing,
273
* as this limits the length of pauses while indexing to a few seconds.
274
* Larger values are best for batched indexing and speedier searches.
276
* Default value is PHP_INT_MAX
278
* @param integer $maxMergeDocs
280
public function setMaxMergeDocs($maxMergeDocs)
282
$this->_index->setMaxMergeDocs($maxMergeDocs);
287
* Retrieve index mergeFactor option
289
* mergeFactor determines how often segment indices are merged by addDocument().
290
* With smaller values, less RAM is used while indexing,
291
* and searches on unoptimized indices are faster,
292
* but indexing speed is slower.
293
* With larger values, more RAM is used during indexing,
294
* and while searches on unoptimized indices are slower,
295
* indexing is faster.
296
* Thus larger values (> 10) are best for batch index creation,
297
* and smaller values (< 10) for indices that are interactively maintained.
299
* Default value is 10
303
public function getMergeFactor()
305
return $this->_index->getMergeFactor();
309
* Set index mergeFactor option
311
* mergeFactor determines how often segment indices are merged by addDocument().
312
* With smaller values, less RAM is used while indexing,
313
* and searches on unoptimized indices are faster,
314
* but indexing speed is slower.
315
* With larger values, more RAM is used during indexing,
316
* and while searches on unoptimized indices are slower,
317
* indexing is faster.
318
* Thus larger values (> 10) are best for batch index creation,
319
* and smaller values (< 10) for indices that are interactively maintained.
321
* Default value is 10
323
* @param integer $maxMergeDocs
325
public function setMergeFactor($mergeFactor)
327
$this->_index->setMergeFactor($mergeFactor);
331
* Performs a query against the index and returns an array
332
* of Zend_Search_Lucene_Search_QueryHit objects.
333
* Input is a string or Zend_Search_Lucene_Search_Query.
335
* @param mixed $query
336
* @return array Zend_Search_Lucene_Search_QueryHit
337
* @throws Zend_Search_Lucene_Exception
339
public function find($query)
341
// actual parameter list
342
$parameters = func_get_args();
344
// invoke $this->_index->find() method with specified parameters
345
return call_user_func_array(array(&$this->_index, 'find'), $parameters);
349
* Returns a list of all unique field names that exist in this index.
351
* @param boolean $indexed
354
public function getFieldNames($indexed = false)
356
return $this->_index->getFieldNames($indexed);
360
* Returns a Zend_Search_Lucene_Document object for the document
361
* number $id in this index.
363
* @param integer|Zend_Search_Lucene_Search_QueryHit $id
364
* @return Zend_Search_Lucene_Document
366
public function getDocument($id)
368
return $this->_index->getDocument($id);
372
* Returns true if index contain documents with specified term.
374
* Is used for query optimization.
376
* @param Zend_Search_Lucene_Index_Term $term
379
public function hasTerm(Zend_Search_Lucene_Index_Term $term)
381
return $this->_index->hasTerm($term);
385
* Returns IDs of all the documents containing term.
387
* @param Zend_Search_Lucene_Index_Term $term
388
* @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
391
public function termDocs(Zend_Search_Lucene_Index_Term $term, $docsFilter = null)
393
return $this->_index->termDocs($term, $docsFilter);
397
* Returns documents filter for all documents containing term.
399
* It performs the same operation as termDocs, but return result as
400
* Zend_Search_Lucene_Index_DocsFilter object
402
* @param Zend_Search_Lucene_Index_Term $term
403
* @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
404
* @return Zend_Search_Lucene_Index_DocsFilter
406
public function termDocsFilter(Zend_Search_Lucene_Index_Term $term, $docsFilter = null)
408
return $this->_index->termDocsFilter($term, $docsFilter);
412
* Returns an array of all term freqs.
413
* Return array structure: array( docId => freq, ...)
415
* @param Zend_Search_Lucene_Index_Term $term
416
* @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
419
public function termFreqs(Zend_Search_Lucene_Index_Term $term, $docsFilter = null)
421
return $this->_index->termFreqs($term, $docsFilter);
425
* Returns an array of all term positions in the documents.
426
* Return array structure: array( docId => array( pos1, pos2, ...), ...)
428
* @param Zend_Search_Lucene_Index_Term $term
429
* @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
432
public function termPositions(Zend_Search_Lucene_Index_Term $term, $docsFilter = null)
434
return $this->_index->termPositions($term, $docsFilter);
438
* Returns the number of documents in this index containing the $term.
440
* @param Zend_Search_Lucene_Index_Term $term
443
public function docFreq(Zend_Search_Lucene_Index_Term $term)
445
return $this->_index->docFreq($term);
449
* Retrive similarity used by index reader
451
* @return Zend_Search_Lucene_Search_Similarity
453
public function getSimilarity()
455
return $this->_index->getSimilarity();
459
* Returns a normalization factor for "field, document" pair.
462
* @param string $fieldName
465
public function norm($id, $fieldName)
467
return $this->_index->norm($id, $fieldName);
471
* Returns true if any documents have been deleted from this index.
475
public function hasDeletions()
477
return $this->_index->hasDeletions();
481
* Deletes a document from the index.
482
* $id is an internal document id
484
* @param integer|Zend_Search_Lucene_Search_QueryHit $id
485
* @throws Zend_Search_Lucene_Exception
487
public function delete($id)
489
return $this->_index->delete($id);
493
* Adds a document to this index.
495
* @param Zend_Search_Lucene_Document $document
497
public function addDocument(Zend_Search_Lucene_Document $document)
499
$this->_index->addDocument($document);
503
* Commit changes resulting from delete() or undeleteAll() operations.
505
public function commit()
507
$this->_index->commit();
513
* Merges all segments into one
515
public function optimize()
517
$this->_index->optimize();
521
* Returns an array of all terms in this index.
525
public function terms()
527
return $this->_index->terms();
532
* Reset terms stream.
534
public function resetTermsStream()
536
$this->_index->resetTermsStream();
540
* Skip terms stream up to specified term preffix.
542
* Prefix contains fully specified field info and portion of searched term
544
* @param Zend_Search_Lucene_Index_Term $prefix
546
public function skipTo(Zend_Search_Lucene_Index_Term $prefix)
548
return $this->_index->skipTo($prefix);
552
* Scans terms dictionary and returns next term
554
* @return Zend_Search_Lucene_Index_Term|null
556
public function nextTerm()
558
return $this->_index->nextTerm();
562
* Returns term in current position
564
* @return Zend_Search_Lucene_Index_Term|null
566
public function currentTerm()
568
return $this->_index->currentTerm();
574
* Should be used for resources clean up if stream is not read up to the end
576
public function closeTermsStream()
578
$this->_index->closeTermsStream();
583
* Undeletes all documents currently marked as deleted in this index.
585
public function undeleteAll()
587
return $this->_index->undeleteAll();
591
* Add reference to the index object
595
public function addReference()
597
return $this->_index->addReference();
601
* Remove reference from the index object
603
* When reference count becomes zero, index is closed and resources are cleaned up
607
public function removeReference()
609
return $this->_index->removeReference();