2016-09-22 160 views
0

我正在使用Silverstripe FullTextSearch。我的審訊是如何在自定義字段中搜索並顯示結果。有一個在index.md代碼寫:Silverstripe全文搜索自定義字段

文件:mysite的/代碼/ MyIndex.php:

<?php 
class MyIndex extends SolrIndex { 
    function init() { 
     $this->addClass('MyPage'); 
     $this->addFulltextField('Description'); 
    } 
} 

在page.php文件

class Page_Controller extends ContentController { 
    private static $allowed_actions = array('search'); 
    public function search($request) { 
     $query = new SearchQuery(); 
     $query->search($request->getVar('q')); 
     return $this->renderWith('array(
      'SearchResult' => singleton('MyIndex')->search($query) 
     )); 
    } 
} 

當我試圖尋找一些話從描述字段,找不到結果...建議?

回答

1

以我的經驗FullTextSearch一直令人沮喪和問題。我願意糾正,但我覺得開發人員正在離開它而不是使用SearchFilters(https://docs.silverstripe.org/en/3.1/developer_guides/model/searchfilters/)。

如果它有幫助,我寫了一個模塊,允許自定義搜索並在自定義控制器中顯示結果。它不如FullTextSearch有效,但效果很好(https://github.com/i-lateral/silverstripe-searchable)。

我最初嘗試使用FullTextSearch編寫此模塊,但我無法使其工作。

+0

感謝您的回覆。你的搜索方法是否像部分匹配? ℅something℅?如果是這樣,它不足以搜索複雜的單詞或錯誤的單詞。我已經使自己的搜索代碼從%數據庫中直接獲取數據,我發現這對我的網站來說還不夠。 – StefGuev

+0

不幸的是,它使用局部match。我想另一種解決方案是研究創建一個利用全文搜索的自定義過濾器,或者使用SQLQuery類手動編寫SQL(更多信息請參見https://docs.silverstripe.org/en/3.4/developer_guides/model/sql_query /) – PsychoMo

+0

Humm ok,如果我找到解決方案,我會發布它,謝謝你試圖幫助我 – StefGuev

0

嗯,我找到了解決方案。 FullTextsearch模塊始終在$ Title,$ MenuTitle和$ Content中搜索。我沒有使用$ Content作爲我的內容,而是使用$ ContentPage變量。所有的DataObjects和其他變量都必須像下圖一樣添加。

例子:

$this->Content = $this->ContentPage." ".$this->Variable1." ".$this->Variable2." ".$dataobject; 

在這個例子中,所有的變量,並在頁面數據對象是搜索throught $內容。這不是一個完美的解決方案,但有效。