Zend Lucene, getting document ID after indexing

Once upon a time I have a problem how to get the ID of the indexed document, I need to delete
the document that was not available on the database, apparently the document unique ID cannot
be use or kept bcoz, it changed after optimization, so then,

the Indexed documents may have two type of id’s, so I put database id on the document;

  1. Internal document id returned by $hit->id and used by

    $index->getDocument(), $index->delete()

and some other methods.

This id _may_ and _will_ be changed while index optimization (or
auto-optimization) and can’t be used to refer indexed document.

This id also can’t be used in search queries.

  1. Some unique (or not unique) value added to document while indexing:

    $doc->addField(Zend_Search_Lucene_Field::Keyword(‘DB_id’, $dbId));

This field can be used for searching document:

<pre lang="php">$hits = $index->find('DB_id:2');

or (better) directly retrieving documents:

<pre lang="php">
$docIDs = $index->TermDocs(new Zend_Search_Lucene_Index_Term('2',
foreach ($docIDs as $docId) {

Khalid Adisendjaja

Internet tech wanderer. Networking enthusiast. Radio Announcer. Casual Gamer. OpenSource Fans. Information Junkies. My tweet is my own.

Jakarta, Indonesia http://khal.web.id