|
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-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
19 * @version $Id: Proxy.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
20 */ |
|
21 |
|
22 /** Zend_Search_Lucene_Interface */ |
|
23 require_once 'Zend/Search/Lucene/Interface.php'; |
|
24 |
|
25 |
|
26 /** |
|
27 * Proxy class intended to be used in userland. |
|
28 * |
|
29 * It tracks, when index object goes out of scope and forces ndex closing |
|
30 * |
|
31 * @category Zend |
|
32 * @package Zend_Search_Lucene |
|
33 * @copyright Copyright (c) 2005-2010 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_Proxy implements Zend_Search_Lucene_Interface |
|
37 { |
|
38 /** |
|
39 * Index object |
|
40 * |
|
41 * @var Zend_Search_Lucene_Interface |
|
42 */ |
|
43 private $_index; |
|
44 |
|
45 /** |
|
46 * Object constructor |
|
47 * |
|
48 * @param Zend_Search_Lucene_Interface $index |
|
49 */ |
|
50 public function __construct(Zend_Search_Lucene_Interface $index) |
|
51 { |
|
52 $this->_index = $index; |
|
53 $this->_index->addReference(); |
|
54 } |
|
55 |
|
56 /** |
|
57 * Object destructor |
|
58 */ |
|
59 public function __destruct() |
|
60 { |
|
61 if ($this->_index !== null) { |
|
62 // This code is invoked if Zend_Search_Lucene_Interface object constructor throws an exception |
|
63 $this->_index->removeReference(); |
|
64 } |
|
65 $this->_index = null; |
|
66 } |
|
67 |
|
68 /** |
|
69 * Get current generation number |
|
70 * |
|
71 * Returns generation number |
|
72 * 0 means pre-2.1 index format |
|
73 * -1 means there are no segments files. |
|
74 * |
|
75 * @param Zend_Search_Lucene_Storage_Directory $directory |
|
76 * @return integer |
|
77 * @throws Zend_Search_Lucene_Exception |
|
78 */ |
|
79 public static function getActualGeneration(Zend_Search_Lucene_Storage_Directory $directory) |
|
80 { |
|
81 Zend_Search_Lucene::getActualGeneration($directory); |
|
82 } |
|
83 |
|
84 /** |
|
85 * Get segments file name |
|
86 * |
|
87 * @param integer $generation |
|
88 * @return string |
|
89 */ |
|
90 public static function getSegmentFileName($generation) |
|
91 { |
|
92 Zend_Search_Lucene::getSegmentFileName($generation); |
|
93 } |
|
94 |
|
95 /** |
|
96 * Get index format version |
|
97 * |
|
98 * @return integer |
|
99 */ |
|
100 public function getFormatVersion() |
|
101 { |
|
102 return $this->_index->getFormatVersion(); |
|
103 } |
|
104 |
|
105 /** |
|
106 * Set index format version. |
|
107 * Index is converted to this format at the nearest upfdate time |
|
108 * |
|
109 * @param int $formatVersion |
|
110 * @throws Zend_Search_Lucene_Exception |
|
111 */ |
|
112 public function setFormatVersion($formatVersion) |
|
113 { |
|
114 $this->_index->setFormatVersion($formatVersion); |
|
115 } |
|
116 |
|
117 /** |
|
118 * Returns the Zend_Search_Lucene_Storage_Directory instance for this index. |
|
119 * |
|
120 * @return Zend_Search_Lucene_Storage_Directory |
|
121 */ |
|
122 public function getDirectory() |
|
123 { |
|
124 return $this->_index->getDirectory(); |
|
125 } |
|
126 |
|
127 /** |
|
128 * Returns the total number of documents in this index (including deleted documents). |
|
129 * |
|
130 * @return integer |
|
131 */ |
|
132 public function count() |
|
133 { |
|
134 return $this->_index->count(); |
|
135 } |
|
136 |
|
137 /** |
|
138 * Returns one greater than the largest possible document number. |
|
139 * This may be used to, e.g., determine how big to allocate a structure which will have |
|
140 * an element for every document number in an index. |
|
141 * |
|
142 * @return integer |
|
143 */ |
|
144 public function maxDoc() |
|
145 { |
|
146 return $this->_index->maxDoc(); |
|
147 } |
|
148 |
|
149 /** |
|
150 * Returns the total number of non-deleted documents in this index. |
|
151 * |
|
152 * @return integer |
|
153 */ |
|
154 public function numDocs() |
|
155 { |
|
156 return $this->_index->numDocs(); |
|
157 } |
|
158 |
|
159 /** |
|
160 * Checks, that document is deleted |
|
161 * |
|
162 * @param integer $id |
|
163 * @return boolean |
|
164 * @throws Zend_Search_Lucene_Exception Exception is thrown if $id is out of the range |
|
165 */ |
|
166 public function isDeleted($id) |
|
167 { |
|
168 return $this->_index->isDeleted($id); |
|
169 } |
|
170 |
|
171 /** |
|
172 * Set default search field. |
|
173 * |
|
174 * Null means, that search is performed through all fields by default |
|
175 * |
|
176 * Default value is null |
|
177 * |
|
178 * @param string $fieldName |
|
179 */ |
|
180 public static function setDefaultSearchField($fieldName) |
|
181 { |
|
182 Zend_Search_Lucene::setDefaultSearchField($fieldName); |
|
183 } |
|
184 |
|
185 /** |
|
186 * Get default search field. |
|
187 * |
|
188 * Null means, that search is performed through all fields by default |
|
189 * |
|
190 * @return string |
|
191 */ |
|
192 public static function getDefaultSearchField() |
|
193 { |
|
194 return Zend_Search_Lucene::getDefaultSearchField(); |
|
195 } |
|
196 |
|
197 /** |
|
198 * Set result set limit. |
|
199 * |
|
200 * 0 (default) means no limit |
|
201 * |
|
202 * @param integer $limit |
|
203 */ |
|
204 public static function setResultSetLimit($limit) |
|
205 { |
|
206 Zend_Search_Lucene::setResultSetLimit($limit); |
|
207 } |
|
208 |
|
209 /** |
|
210 * Set result set limit. |
|
211 * |
|
212 * 0 means no limit |
|
213 * |
|
214 * @return integer |
|
215 */ |
|
216 public static function getResultSetLimit() |
|
217 { |
|
218 return Zend_Search_Lucene::getResultSetLimit(); |
|
219 } |
|
220 |
|
221 /** |
|
222 * Retrieve index maxBufferedDocs option |
|
223 * |
|
224 * maxBufferedDocs is a minimal number of documents required before |
|
225 * the buffered in-memory documents are written into a new Segment |
|
226 * |
|
227 * Default value is 10 |
|
228 * |
|
229 * @return integer |
|
230 */ |
|
231 public function getMaxBufferedDocs() |
|
232 { |
|
233 return $this->_index->getMaxBufferedDocs(); |
|
234 } |
|
235 |
|
236 /** |
|
237 * Set index maxBufferedDocs option |
|
238 * |
|
239 * maxBufferedDocs is a minimal number of documents required before |
|
240 * the buffered in-memory documents are written into a new Segment |
|
241 * |
|
242 * Default value is 10 |
|
243 * |
|
244 * @param integer $maxBufferedDocs |
|
245 */ |
|
246 public function setMaxBufferedDocs($maxBufferedDocs) |
|
247 { |
|
248 $this->_index->setMaxBufferedDocs($maxBufferedDocs); |
|
249 } |
|
250 |
|
251 |
|
252 /** |
|
253 * Retrieve index maxMergeDocs option |
|
254 * |
|
255 * maxMergeDocs is a largest number of documents ever merged by addDocument(). |
|
256 * Small values (e.g., less than 10,000) are best for interactive indexing, |
|
257 * as this limits the length of pauses while indexing to a few seconds. |
|
258 * Larger values are best for batched indexing and speedier searches. |
|
259 * |
|
260 * Default value is PHP_INT_MAX |
|
261 * |
|
262 * @return integer |
|
263 */ |
|
264 public function getMaxMergeDocs() |
|
265 { |
|
266 return $this->_index->getMaxMergeDocs(); |
|
267 } |
|
268 |
|
269 /** |
|
270 * Set index maxMergeDocs option |
|
271 * |
|
272 * maxMergeDocs is a largest number of documents ever merged by addDocument(). |
|
273 * Small values (e.g., less than 10,000) are best for interactive indexing, |
|
274 * as this limits the length of pauses while indexing to a few seconds. |
|
275 * Larger values are best for batched indexing and speedier searches. |
|
276 * |
|
277 * Default value is PHP_INT_MAX |
|
278 * |
|
279 * @param integer $maxMergeDocs |
|
280 */ |
|
281 public function setMaxMergeDocs($maxMergeDocs) |
|
282 { |
|
283 $this->_index->setMaxMergeDocs($maxMergeDocs); |
|
284 } |
|
285 |
|
286 |
|
287 /** |
|
288 * Retrieve index mergeFactor option |
|
289 * |
|
290 * mergeFactor determines how often segment indices are merged by addDocument(). |
|
291 * With smaller values, less RAM is used while indexing, |
|
292 * and searches on unoptimized indices are faster, |
|
293 * but indexing speed is slower. |
|
294 * With larger values, more RAM is used during indexing, |
|
295 * and while searches on unoptimized indices are slower, |
|
296 * indexing is faster. |
|
297 * Thus larger values (> 10) are best for batch index creation, |
|
298 * and smaller values (< 10) for indices that are interactively maintained. |
|
299 * |
|
300 * Default value is 10 |
|
301 * |
|
302 * @return integer |
|
303 */ |
|
304 public function getMergeFactor() |
|
305 { |
|
306 return $this->_index->getMergeFactor(); |
|
307 } |
|
308 |
|
309 /** |
|
310 * Set index mergeFactor option |
|
311 * |
|
312 * mergeFactor determines how often segment indices are merged by addDocument(). |
|
313 * With smaller values, less RAM is used while indexing, |
|
314 * and searches on unoptimized indices are faster, |
|
315 * but indexing speed is slower. |
|
316 * With larger values, more RAM is used during indexing, |
|
317 * and while searches on unoptimized indices are slower, |
|
318 * indexing is faster. |
|
319 * Thus larger values (> 10) are best for batch index creation, |
|
320 * and smaller values (< 10) for indices that are interactively maintained. |
|
321 * |
|
322 * Default value is 10 |
|
323 * |
|
324 * @param integer $maxMergeDocs |
|
325 */ |
|
326 public function setMergeFactor($mergeFactor) |
|
327 { |
|
328 $this->_index->setMergeFactor($mergeFactor); |
|
329 } |
|
330 |
|
331 /** |
|
332 * Performs a query against the index and returns an array |
|
333 * of Zend_Search_Lucene_Search_QueryHit objects. |
|
334 * Input is a string or Zend_Search_Lucene_Search_Query. |
|
335 * |
|
336 * @param mixed $query |
|
337 * @return array Zend_Search_Lucene_Search_QueryHit |
|
338 * @throws Zend_Search_Lucene_Exception |
|
339 */ |
|
340 public function find($query) |
|
341 { |
|
342 // actual parameter list |
|
343 $parameters = func_get_args(); |
|
344 |
|
345 // invoke $this->_index->find() method with specified parameters |
|
346 return call_user_func_array(array(&$this->_index, 'find'), $parameters); |
|
347 } |
|
348 |
|
349 /** |
|
350 * Returns a list of all unique field names that exist in this index. |
|
351 * |
|
352 * @param boolean $indexed |
|
353 * @return array |
|
354 */ |
|
355 public function getFieldNames($indexed = false) |
|
356 { |
|
357 return $this->_index->getFieldNames($indexed); |
|
358 } |
|
359 |
|
360 /** |
|
361 * Returns a Zend_Search_Lucene_Document object for the document |
|
362 * number $id in this index. |
|
363 * |
|
364 * @param integer|Zend_Search_Lucene_Search_QueryHit $id |
|
365 * @return Zend_Search_Lucene_Document |
|
366 */ |
|
367 public function getDocument($id) |
|
368 { |
|
369 return $this->_index->getDocument($id); |
|
370 } |
|
371 |
|
372 /** |
|
373 * Returns true if index contain documents with specified term. |
|
374 * |
|
375 * Is used for query optimization. |
|
376 * |
|
377 * @param Zend_Search_Lucene_Index_Term $term |
|
378 * @return boolean |
|
379 */ |
|
380 public function hasTerm(Zend_Search_Lucene_Index_Term $term) |
|
381 { |
|
382 return $this->_index->hasTerm($term); |
|
383 } |
|
384 |
|
385 /** |
|
386 * Returns IDs of all the documents containing term. |
|
387 * |
|
388 * @param Zend_Search_Lucene_Index_Term $term |
|
389 * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter |
|
390 * @return array |
|
391 */ |
|
392 public function termDocs(Zend_Search_Lucene_Index_Term $term, $docsFilter = null) |
|
393 { |
|
394 return $this->_index->termDocs($term, $docsFilter); |
|
395 } |
|
396 |
|
397 /** |
|
398 * Returns documents filter for all documents containing term. |
|
399 * |
|
400 * It performs the same operation as termDocs, but return result as |
|
401 * Zend_Search_Lucene_Index_DocsFilter object |
|
402 * |
|
403 * @param Zend_Search_Lucene_Index_Term $term |
|
404 * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter |
|
405 * @return Zend_Search_Lucene_Index_DocsFilter |
|
406 */ |
|
407 public function termDocsFilter(Zend_Search_Lucene_Index_Term $term, $docsFilter = null) |
|
408 { |
|
409 return $this->_index->termDocsFilter($term, $docsFilter); |
|
410 } |
|
411 |
|
412 /** |
|
413 * Returns an array of all term freqs. |
|
414 * Return array structure: array( docId => freq, ...) |
|
415 * |
|
416 * @param Zend_Search_Lucene_Index_Term $term |
|
417 * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter |
|
418 * @return integer |
|
419 */ |
|
420 public function termFreqs(Zend_Search_Lucene_Index_Term $term, $docsFilter = null) |
|
421 { |
|
422 return $this->_index->termFreqs($term, $docsFilter); |
|
423 } |
|
424 |
|
425 /** |
|
426 * Returns an array of all term positions in the documents. |
|
427 * Return array structure: array( docId => array( pos1, pos2, ...), ...) |
|
428 * |
|
429 * @param Zend_Search_Lucene_Index_Term $term |
|
430 * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter |
|
431 * @return array |
|
432 */ |
|
433 public function termPositions(Zend_Search_Lucene_Index_Term $term, $docsFilter = null) |
|
434 { |
|
435 return $this->_index->termPositions($term, $docsFilter); |
|
436 } |
|
437 |
|
438 /** |
|
439 * Returns the number of documents in this index containing the $term. |
|
440 * |
|
441 * @param Zend_Search_Lucene_Index_Term $term |
|
442 * @return integer |
|
443 */ |
|
444 public function docFreq(Zend_Search_Lucene_Index_Term $term) |
|
445 { |
|
446 return $this->_index->docFreq($term); |
|
447 } |
|
448 |
|
449 /** |
|
450 * Retrive similarity used by index reader |
|
451 * |
|
452 * @return Zend_Search_Lucene_Search_Similarity |
|
453 */ |
|
454 public function getSimilarity() |
|
455 { |
|
456 return $this->_index->getSimilarity(); |
|
457 } |
|
458 |
|
459 /** |
|
460 * Returns a normalization factor for "field, document" pair. |
|
461 * |
|
462 * @param integer $id |
|
463 * @param string $fieldName |
|
464 * @return float |
|
465 */ |
|
466 public function norm($id, $fieldName) |
|
467 { |
|
468 return $this->_index->norm($id, $fieldName); |
|
469 } |
|
470 |
|
471 /** |
|
472 * Returns true if any documents have been deleted from this index. |
|
473 * |
|
474 * @return boolean |
|
475 */ |
|
476 public function hasDeletions() |
|
477 { |
|
478 return $this->_index->hasDeletions(); |
|
479 } |
|
480 |
|
481 /** |
|
482 * Deletes a document from the index. |
|
483 * $id is an internal document id |
|
484 * |
|
485 * @param integer|Zend_Search_Lucene_Search_QueryHit $id |
|
486 * @throws Zend_Search_Lucene_Exception |
|
487 */ |
|
488 public function delete($id) |
|
489 { |
|
490 return $this->_index->delete($id); |
|
491 } |
|
492 |
|
493 /** |
|
494 * Adds a document to this index. |
|
495 * |
|
496 * @param Zend_Search_Lucene_Document $document |
|
497 */ |
|
498 public function addDocument(Zend_Search_Lucene_Document $document) |
|
499 { |
|
500 $this->_index->addDocument($document); |
|
501 } |
|
502 |
|
503 /** |
|
504 * Commit changes resulting from delete() or undeleteAll() operations. |
|
505 */ |
|
506 public function commit() |
|
507 { |
|
508 $this->_index->commit(); |
|
509 } |
|
510 |
|
511 /** |
|
512 * Optimize index. |
|
513 * |
|
514 * Merges all segments into one |
|
515 */ |
|
516 public function optimize() |
|
517 { |
|
518 $this->_index->optimize(); |
|
519 } |
|
520 |
|
521 /** |
|
522 * Returns an array of all terms in this index. |
|
523 * |
|
524 * @return array |
|
525 */ |
|
526 public function terms() |
|
527 { |
|
528 return $this->_index->terms(); |
|
529 } |
|
530 |
|
531 |
|
532 /** |
|
533 * Reset terms stream. |
|
534 */ |
|
535 public function resetTermsStream() |
|
536 { |
|
537 $this->_index->resetTermsStream(); |
|
538 } |
|
539 |
|
540 /** |
|
541 * Skip terms stream up to specified term preffix. |
|
542 * |
|
543 * Prefix contains fully specified field info and portion of searched term |
|
544 * |
|
545 * @param Zend_Search_Lucene_Index_Term $prefix |
|
546 */ |
|
547 public function skipTo(Zend_Search_Lucene_Index_Term $prefix) |
|
548 { |
|
549 return $this->_index->skipTo($prefix); |
|
550 } |
|
551 |
|
552 /** |
|
553 * Scans terms dictionary and returns next term |
|
554 * |
|
555 * @return Zend_Search_Lucene_Index_Term|null |
|
556 */ |
|
557 public function nextTerm() |
|
558 { |
|
559 return $this->_index->nextTerm(); |
|
560 } |
|
561 |
|
562 /** |
|
563 * Returns term in current position |
|
564 * |
|
565 * @return Zend_Search_Lucene_Index_Term|null |
|
566 */ |
|
567 public function currentTerm() |
|
568 { |
|
569 return $this->_index->currentTerm(); |
|
570 } |
|
571 |
|
572 /** |
|
573 * Close terms stream |
|
574 * |
|
575 * Should be used for resources clean up if stream is not read up to the end |
|
576 */ |
|
577 public function closeTermsStream() |
|
578 { |
|
579 $this->_index->closeTermsStream(); |
|
580 } |
|
581 |
|
582 |
|
583 /** |
|
584 * Undeletes all documents currently marked as deleted in this index. |
|
585 */ |
|
586 public function undeleteAll() |
|
587 { |
|
588 return $this->_index->undeleteAll(); |
|
589 } |
|
590 |
|
591 /** |
|
592 * Add reference to the index object |
|
593 * |
|
594 * @internal |
|
595 */ |
|
596 public function addReference() |
|
597 { |
|
598 return $this->_index->addReference(); |
|
599 } |
|
600 |
|
601 /** |
|
602 * Remove reference from the index object |
|
603 * |
|
604 * When reference count becomes zero, index is closed and resources are cleaned up |
|
605 * |
|
606 * @internal |
|
607 */ |
|
608 public function removeReference() |
|
609 { |
|
610 return $this->_index->removeReference(); |
|
611 } |
|
612 } |