PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Retrieving documents by unique id

It's a common practice to store some unique document id in the index. Examples include url, path, or database id.

Zend_Search_Lucene provides a termDocs() method for retrieving documents containing specified terms.

This is more efficient than using the find() method:

<?php
// Retrieving documents with find() method using a query string
$query $idFieldName ':' $docId;
$hits  $index->find($query);
foreach (
$hits as $hit) {
    
$title    $hit->title;
    
$contents $hit->contents;
    ...
}
...

// Retrieving documents with find() method using the query API
$term = new Zend_Search_Lucene_Index_Term($docId$idFieldName);
$query = new Zend_Search_Lucene_Search_Query_Term($term);
$hits  $index->find($query);
foreach (
$hits as $hit) {
    
$title    $hit->title;
    
$contents $hit->contents;
    ...
}

...

// Retrieving documents with termDocs() method
$term = new Zend_Search_Lucene_Index_Term($docId$idFieldName);
$docIds  $index->termDocs($term);
foreach (
$docIds as $id) {
    
$doc $index->getDocument($id);
    
$title    $doc->title;
    
$contents $doc->contents;
    ...
}

Zend Framework