我正在使用zend框架的網站。我需要使用zend lucene進入我的網站。我們有一個選項供用戶搜索類別,項目等。如何查詢自動填充/多詞搜索的建議?
我們使用zend lucene自動完成選項。假設我有'iphone 3s','iphone 4s','iphone 5s'等項目。如果用戶鍵入'iph',我將獲得搜索結果'iphone 3s','iphone 4s','iphone 5s'。目前,我正在爲單個詞搜索獲得良好的結果。
我曾經爲不同的搜索使用了不同查詢構造API的組合。
對於單詞搜索,我使用的是Zend_Search_Lucene_Search_Query_Wildcard
,我得到的匹配模式結果完美。對於多項術語 搜索,我使用了Zend_Search_Lucene_Search_Query_MultiTerm()
,但僅在精確文本搜索的情況下才能獲得結果。有人請 幫我整合這個功能嗎?
我使用Zend_Search_Lucene_Search_Query_Wildcard
獲得了很好的結果,對於多項搜索我需要同樣的完美。由於Zend_Search_Lucene_Search_Query_MultiTerm()
僅在精確搜索文本的情況下才會返回良好結果,因此我無法使用自動完成選項。
我下面搜索部分代碼:
$searchfield = $searchtype."_name";//Like category_name
$searchfieldid = $searchtype."_id";//Like category_name
$res_table = "t_".$searchtype;
$index = Zend_Search_Lucene::open(Zend_Registry::get('_datapath'));
/* Here we used different API for creating query for diffrent pattern
1) Words < 3 chars(it wont work in wild cart
2) Single word (pattern matching--wildcard)
3) More than one word serach
*/
$res = explode(' ',$searchkey);
if(strlen($searchkey) <3) /*if key >=3 then only wildcard search works*/
{
$term = new Zend_Search_Lucene_Index_Term($searchkey, $searchfield);
$query = new Zend_Search_Lucene_Search_Query_Term($term);
}
else if(sizeof($res)==1) /*write condition for single word -->check sub str count*/
{
$searchkey1 = $searchkey."*";//car*
$pattern = new Zend_Search_Lucene_Index_Term($searchkey1, $searchfield);
$query = new Zend_Search_Lucene_Search_Query_Wildcard($pattern);
}
else
{ /*if more than one word comes use different query rather than wildcard query search*/
$query = new Zend_Search_Lucene_Search_Query_MultiTerm();
foreach($res as $key => $val)
{
/* TRUE is used to define required term.
FALSE is used to define prohibited term.
NULL is used to define a term that is neither required nor prohibited.
(here we used last term as null and others are true*/
if($key == (sizeof($res)-1))
$signs = null;/*Last item is not a mandatory, so use null*/
else
$signs = true;
$query->addTerm(new Zend_Search_Lucene_Index_Term($val,$searchfield), $signs);
}
}
$hits = $index->find($query);
這是很好的解釋。我現在不使用它,但對於我的網站,我將使用Lucene實現搜索。目前,我正在爲我的zend網站開發自動填充功能,並且可能會試試你的方式。感謝分享 –
儘管*我沒有測試過自己*,但我不鼓勵修改'分鐘3個字母'的限制。我想這是爲了防止服務器工作量太大,所以如果你能避免這種情況。我已經加載了一些簡單的數據用於1和2個字母自動完成/建議(因爲我可以提供簡單的燈光樣本在我的情況下),並從3個字母,網頁請求服務器。 – Cedric