1

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 are not deleted.

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

Community
  • 1
  • 1
mrN
  • 3,734
  • 15
  • 58
  • 82

2 Answers2

2

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.

DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601
  • Thanks for solving my error. Still, my actual question is unanswered. How to delete all the documents in the index? – mrN Apr 12 '11 at 10:24
  • I already found similar codes to that, but how will `$id` target all the documents. So far as I know, it only target those matching $id, is id field. – mrN Apr 13 '11 at 07:29
  • If id is the unique key to identify a document it matches your record 1:1 – DarkLeafyGreen Apr 13 '11 at 09:29
  • Exactly, How to find all the documents and delete them. – mrN Apr 13 '11 at 10:08
  • How do you index them? The same way you delete them, loop over each element – DarkLeafyGreen Apr 13 '11 at 17:43
  • So, there is no way to delete them, using something like `*` – mrN Apr 17 '11 at 06:49
  • No. Isn't it simple and straight forward? The same way you index your data, e.g. by reading the database, you delete everything. Fetch all database entries that you indexed, loop through them and use the code from above to delete them by id – DarkLeafyGreen Apr 17 '11 at 07:31
1

@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.

jpMig
  • 11
  • 1