2013-03-29 76 views
0

我使用zend lucene進行搜索。 我形成我的索引與該代碼zend lucene接近字段開頭

$doc = new Zend_Search_Lucene_Document(); 
      $doc->addField(Zend_Search_Lucene_Field::text('word_id', $word['id'])); 

      $boostField = Zend_Search_Lucene_Field::text('priority', $word['priority']); 
      $doc->addField($boostField); 
      $doc->addField(Zend_Search_Lucene_Field::unStored('description', $word['name'], 'UTF-8')); 
      $index->addDocument($doc); 

與代碼解析搜索字符串:

$query= str_replace(array('-','!','@','#','$','%','^','&','*','(',')'), ' ', $query); 
     $query = trim($query); 
     $words = explode(" ", $query); 
     unset($query); 
     $query = ""; 
     foreach ($words as $word) { 
      $query.='(' . $word . '*)'; 
      if ($word != end($words)) { 
       $query.=' AND '; 
      } 
     } 
     return $query; 

有了這樣的代碼搜索:

try { 
      Zend_Search_Lucene::setResultSetLimit($limit); 
      $index = Zend_Search_Lucene::open($this->_indexPath); 
     } catch (Exception $e) { 
      return false; 
     } 
     try { 
      return $index->find($query, 'score', SORT_NUMERIC, SORT_DESC, 'priority', SORT_NUMERIC, SORT_ASC); 
     } catch (Exception $e) { 
      return false; 
     } 

所以,我的問題,我有索引一些領域,如:nootebook黑色包,nootebook藍色包,nootebook,nootebook蘋果。 例如我輸入「筆記本」,我想要「nootebook,nootebook蘋果」的頂部結果。 但我有最好的結果袋!我做錯了什麼?我需要做的是從搜索字段的起始位置獲得最大近距離位置的結果? 有可能嗎?

+0

你能否解釋一下「筆記本蘋果」和「黑色筆記本包」之間應該做什麼邏輯區分,只要與查詢「筆記本」相關? – femtoRgon

+0

在「筆記本蘋果」一詞中「筆記本」最大接近左側。在「筆記本黑包」一詞中,「筆記本」離左側很遠。我想進行搜索,基於左邊是多麼接近的詞是什麼。 –

回答

0

我不知道有任何QuerySimilarity實現,直接處理這種情況。

A SpanQuery可能被用來完成類似的行爲,除了沒有能力匹配查詢的開始。發生在我身上的解決方法是,以這種方式向要搜索的字段的開頭添加關鍵字,然後使用該關鍵字搜索Span。所以,如果我索引:

xxxbeginxxx notebook apple 

你可以用SpanNearQuery像搜索:哈克解決方案的

SpanNearQuery spanNear1 = new SpanNearQuery(new SpanQuery[] { 
new SpanTermQuery(new Term("description", "xxxbeginxxx")), 
new SpanTermQuery(new Term("description", "notebook"))}, 
10, true); 

類。您必須處理確保該術語總是在索引時添加,並在您自己顯示給用戶之前將其移除。

也許有人知道更好的方法,但我相信應該讓你找到你正在尋找的行爲。

+0

SpanQuery使用PHP?或僅在Java上? –

+0

請參閱Zend的[查詢語言規範](http://framework.zend.com/manual/1.12/en/zend.search.lucene.query-language.html)。查詢解析器創建這種查詢,如:「」xxxbeginxxx筆記本「~10」。 – femtoRgon

+0

我試着做:code $ query =「」; $ query。='「'。」xxxbeginxxx $ words [0]「。'''''〜10'; ($ word!= $ words [0]){ $ query。='「'。$ word。'」'; ($ word!= end($ words)){ } $ query。='AND'; } } return $ query;代碼 我不知道如何在代碼中發佈代碼。我嘗試使用「xxxbeginxxx術語」〜10,但我的搜索lucene只是與exeption和所有死亡。如果xxxbeginxxx是abscract,我需要寫 - 我不知道什麼 –