2011-09-23 23 views
2

我在Symfony2應用程序中實現了Zend_Search_Lucene。我正在使用Zend 1.11。該指數似乎正在創建,我有幾個文件,包括:Zend_Search_Lucene使用Symfony2實現返回空結果

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

這裏是PHP代碼,我有一個控制器

$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; 

裏面當我運行在創建索引文件的腳本,但只有一個空數組被返回,並打印出與上次的var_dump作爲

​​

首先沒有人知道我可以檢查索引被正確寫的? 其次有人知道我的查詢爲什麼沒有返回任何結果嗎? 是否有人使用Symfony2成功實現了Zend_Search_Lucene?我試過了Lidaa Search和EWZ捆綁包,但都沒有效果。

我昨天工作了整天,試圖解決這個問題沒有喜悅,所以任何幫助將非常感激。


好了,我已經成功通過編碼寫入索引文件現在爲UTF-8這樣

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

不過,我仍然可以檢索不到任何結果。我想可能有一些做的區域設置,但我已經明確地設置此像這樣:

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

另外,如果我嘗試解析查詢字符串爲UTF-8腳本掛起和timesout

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

任何人都知道爲什麼這不適用於我的Mac?

回答

0

您似乎錯過了一次提交。

$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

據我試着添加文檔 – Patrick

+0

'$首頁 - >提交();',它沒有影響。根據文檔:「新增文檔可以立即在索引中搜索。」因此'$ index->​​ addDocument($ doc);'提交給索引。我認爲錯誤必須從索引中獲得結果,儘管有誰知道如何檢查索引已成功創建? – Patrick

+0

我也看到了這個說明(關於立即創建)。我複製並運行了你的代碼,並得到了和你一樣的結果 - 在commit()中添加了對我的修復。你可以創建一個獨立的PHP腳本來檢查索引,但我想你會得到相同的結果。 – mogoman

2

Yeehaa!我重新安裝了MAMP到版本2.0.3和繁榮!有用。我有一種感覺,這是與默認的語言環境或編碼有關,但不知道是什麼。如果有人知道爲什麼2.0版MAMP有bug,請告訴我們。

乾杯 帕特里克