2

I have an implementation of Zend_Search_Lucene within a Symfony2 application. I am using Zend 1.11. The index seems to be being created, I have several files including:

_0.cfs
optimization.lock.file
read-lock-processing.lock.file
read.lock.file
segments_2
segments.gen
write.lock.file

here is the php code which I have inside a controller

$index = \Zend_Search_Lucene::create(__DIR__.'/../../../../data/index');
$doc = new \Zend_Search_Lucene_Document();
$doc->addField(\Zend_Search_Lucene_Field::unIndexed('title', 'Symfony2') );
$doc->addField(\Zend_Search_Lucene_Field::text('contents', 'cat dog') );
$index->addDocument($doc);
$index = \Zend_Search_Lucene::open(__DIR__.'/../../../../data/index');
$term  = new \Zend_Search_Lucene_Index_Term("dog");
$query = new \Zend_Search_Lucene_Search_Query_Term($term);
$results  = $index->find($query);
try {
    $results = $index->find($query);
}
catch (\Zend_Search_Lucene_Exception $ex) {
    $results = array();
    var_dump($ex);
}
foreach ( $results as $result ) {
    echo $result->score, ' :: ', $result->title, "n";
}
var_dump($results);
exit;

When I run the script the index files are created but only an empty array gets returned and is printed out with the last var_dump as

array(0) { } 

Firstly does anyone know how I can check the index is being written correctly? Secondly does anyone know why my query is not returning any results? Has anyone successfully implemented Zend_Search_Lucene with Symfony2? I have tried both Lidaa Search and EWZ bundles but neither seems to work.

I worked all day yesterday trying to resolve this problem with no joy, so any help would be very greatly appreciated.


ok, so I've managed to write to the index file now by encoding as utf-8 like this

$doc->addField(\Zend_Search_Lucene_Field::text('contents', 'dog', 'utf-8') );

However, I can still not retrieve any results. I think it might have something to do with the locale settings but I have explicitly set this like so:

setlocale(LC_ALL, 'en_UK.utf-8');
ini_set('intl.default_locale', 'en-UK');

Also if I try and parse the query string as utf-8 the script hangs and timesout

$queryStr = $_GET['query'];
$query = \Zend_Search_Lucene_Search_QueryParser::parse($queryStr, 'utf-8');

Anyone know why this won't work on my Mac?

j0k
  • 22,600
  • 28
  • 79
  • 90
Patrick
  • 358
  • 6
  • 20

2 Answers2

2

Yeehaa! I reinstalled MAMP to version 2.0.3 and Boom! it works. I have a feeling this was something to do with either the default locale or encoding but not sure what. If anyone know why version 2.0 MAMP had a bug please let us know.

Cheers Patrick

Patrick
  • 358
  • 6
  • 20
0

You seem to be missing a commit.

$doc->addField(\Zend_Search_Lucene_Field::text('contents', 'cat dog') );
$index->addDocument($doc);

$index->commit(); //Try adding this line

$index = \Zend_Search_Lucene::open(__DIR__.'/../../../../data/index');
Ryan Wersal
  • 3,210
  • 1
  • 20
  • 29
mogoman
  • 2,286
  • 24
  • 28
  • I've tried adding `$index->commit();` and it has no affect. According to the docs: "Newly added documents are immediately searchable in the index." Therefore `$index->addDocument($doc);` commits to the index. I think the error must be in getting the results back from the index, although does anyone know how to check an index has been created successfully? – Patrick Sep 26 '11 at 08:44
  • I saw that note too (about being immediately created). I copied and ran your code and got the same result as you - adding in commit() fixed it for me. You could create a standalone PHP script to check the index, but I imagine you will get the same result. – mogoman Sep 26 '11 at 10:10
  • Please could you paste a complete code example? I have tried with `$index->commit();` and it doesn't work for me. I am using Zend 1.11. What does the contents of your .cfs look like when it is created? I have also tried in a Zend application and this also does not work for me. – Patrick Sep 26 '11 at 10:42
  • Also I don't know if this is related but if I try `$index->find('dog');` then the script timesout. So it seems the query is not being converted. – Patrick Sep 26 '11 at 11:08
  • I have just found the answer to my own Question, Almost. Zend_Search_Lucene is not working properly on my local Mac OS 10.6.8, using MAMP. However, it does work when I put it on the Live LAMP box. So now the question is why does it not work on my local Mac environment? – Patrick Sep 26 '11 at 11:58
  • tested on my Mac 10.6.8 (native php, not MAMP) with and without utf-8 and it always works. Also, removing commit() prevents the index from being written (or the results from being found). Finally, I compared the files that were written - I have all the same files as you mentioned above if I don't do a commit. With commit, _0.sti appears... – mogoman Sep 26 '11 at 18:44