开发者

Updating indexed documents of Zend Search Lucene indexes

I have already read this question Creating and updating Zend_Search_Lucene indexes.

But it has failed to answer to my problem. This article from zend, tells that updating the document is not possible. To update effectively, every document have to be deleted and re indexed.

$removePath = ...;
$hits = $index->find('path:' . $removePath);
foreach ($hits as $hit) {
    $index->delete($hit->id);
}

Now, this does not work for me. I gave the index Path in the $removePath and tried the code. It didn't work. If I use something relative to my particular index such as $index->find("title:test"); it throws

Fatal error:  Exception thrown without a stack frame in Unknown on line 0

I also tried using

  $query = new Zend_Search_Lucene_Search_Query_Term(new Zend_Search_Lucene_Index_Term('test', 'title'));
  $hits = $this -> index->find($query);

But it gave same result.

I do not even know how to debug that type of error. And even it gets debugged, I will only get the searched items rather than all the documents. So, all documents ar开发者_Go百科e not deleted.

Can anyone, tell me what am i doing wrong. How to update your search indexes?


Fatal error: Exception thrown without a stack frame in Unknown on line 0

Means that you have thrown an exception where an exception cannot be thrown. Usually this occurs when you try to throw an exception in a php destructur or an php exception handler (destructors and exception handlers do not have a stack frame)

This error message is kind of cryptic because it gives you no hint where the error might be.


However this is a know issue: Using the index as static property

So you should call commit() on your index. It will prevent lucene from throwing the exception:

$this->index->commit();

To delete documents you have to interate through the index and delete each document.

$index = Zend_Search_Lucene::open('data/index');

$hits = $index->find('id:'.$id);

  foreach ($hits as $hit) {
     $index->delete($hit->id);
  }
}

So with id or path you identify the field that shoud match with the parameter from the record you want to delete. All documents that are found will be deleted from index.


@mrN , below is a little script to do what you are asking for:

// Function will delete all the docs from the given index 
function delete_all_docs_from_index(Zend_Search_Lucene_Proxy $index) {
    $count = 0;
    $indexDocs = $index->maxDoc();// Get the number of non-deleted docs before running this
    //print "Num of Docs in the index before deletion " . $indexDocs;
    for ($count; $count < $indexDocs; $count++) {
            if (!$index->isDeleted($count)) {
                $index->delete($count);
                $index->commit(); // You have to commit at this point after deleting
        }
    }
    $index->optimize(); // highly recommended
    //print  "Num of Docs in the index after deletion " . $indexDocs;
    return $index;
}

Modify function as you see fit.

I wish their API was friendlier than is currently the case.

Let me know if that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜