Silverstripe solr 搜索文件、页面和数据对象


Silverstripe solr search files, pages, and dataobjects

如何正确将文件添加到搜索索引...

使用自定义索引,我可以成功搜索页面和数据对象,

但是一旦我尝试在此索引中包含文件,页面就会从结果集中删除,并且我只返回文件和数据对象。

这将按预期返回页面和数据对象。

class EntrySearchIndex extends SolrSearchIndex
{
    public function init()
    {
        $this->addClass('SiteTree');
        $this->addClass('EntryAccordionItem');
        $this->addClass('EntryInformationBoxItem');
        $this->addClass('EntryTabItem');
        $this->addAllFulltextFields();
        $this->addFilterField('ShowInSearch');
        $this->excludeVariantState(array('SearchVariantVersioned' => 'Stage'));
    }
}

和基本的工作搜索功能

public static function keywordSearch($keywords)
{
    $keywords = Convert::raw2sql(trim($keywords));
    $classes[] = array('class' => 'EntryPage', 'includeSubclasses' => true);
    $classes[] = array('class' => 'EntryAccordionItem');
    $classes[] = array('class' => 'EntryInformationBoxItem');
    $classes[] = array('class' => 'EntryTabItem');
    $index = singleton('EntrySearchIndex');
    $engine = SearchQuery::create();
    return $engine->search($keywords, $classes, $index, -1, 0)->getResults();
}

进行以下小修改以允许文件(为简洁起见,仅显示更改)

public function init()
{
    $this->addClass('SiteTree');
    $this->addClass('EntryAccordionItem');
    $this->addClass('EntryInformationBoxItem');
    $this->addClass('EntryTabItem');
    // File specific
    $this->addClass('File');
    $this->addFulltextField('FileContent');
    $this->addAllFulltextFields();
    $this->addFilterField('ShowInSearch');
    $this->excludeVariantState(array('SearchVariantVersioned' => 'Stage'));
}

public static function keywordSearch($keywords)
{
    [...]
    // File specific
    $classes[] = array('class' => 'File', 'includeSubclasses' => true);
    [...]
    return $engine->search($keywords, $classes, $index, -1, 0)->getResults();
}

仅返回文件和数据对象。我是否正确认为$this->addAllFulltextFields();现在只应用于文件?

我在 Solr 索引中包含页面和文件时遇到了类似(但略有不同)的问题,但我弄清楚发生了什么的方式可能会有所帮助。

问题是我们希望文件有一个抽象文本字段,用户可以在其中输入文件的简短描述,但是通用Web平台(CWP)页面上有一个抽象字段,因此Solr将其索引,而不是文件的抽象字段。

对于您面临的问题,您是否尝试过登录Solr服务器并浏览架构以查看Solr实际上包含在索引中的字段?

如果在本地运行Solr(使用silverstripe/fulltextsearch-localsolr模块),您应该能够在此处访问服务器 http://localhost:8983/solr

进入Solr服务器Web界面后,尝试执行以下操作...

  • 从左侧菜单的下拉菜单中选择索引
  • 单击底部的架构浏览器
  • 在右侧窗格中,单击顶部的"请选择.."下拉列表,然后检查索引中的字段是否符合预期。

如果幸运的话,您可能会发现Solr选择错误地索引某些内容(也许比较索引中带有和没有文件的索引字段),这将为您提供有关如何解决此问题的线索。

在这一点上,我认为最好不要使用 $this->addAllFulltextFields(); 因为这会将所有内容都放入索引中。我会指定哪些字段是必需的。对于页面,通常标题,摘要,内容都是真正需要的。

也是给你的另一个提示;我发现如果文件的 IncludeSubclasses 设置为 true,搜索结果将包括 assets 目录中的文件夹和图像。在我们的例子中,我们只想要文档,因此将文件排除图像和文件夹的包含子部分设置为 false。

如果您偶然已经或确实解决了这个问题,那么如果您可以发布原因和解决方案,那就太好了。

干杯道格。