开发者

Zend_Search_Lucene implementation with Symfony2 returning empty result

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?


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


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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜