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?

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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');

6 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.