2016-09-22 167 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 $内容。这不是一个完美的解决方案,但有效。