arillo/silverstripe-simple-search

关于此包最新版本(0.0.7)的许可信息不可用。

简单搜索 Silverstripe

安装: 449

依赖项: 0

建议者: 0

安全: 0

星级: 0

关注者: 4

分支: 1

语言:HTML

类型:silverstripe-vendormodule

0.0.7 2022-03-01 12:09 UTC

This package is auto-updated.

Last update: 2024-08-29 05:52:53 UTC


README

为您的项目添加简单的网站搜索。

要求

SilverStripe CMS ^4.0

简单页面示例

<?php
use SilverStripe\CMS\Model\SiteTree;
use Arillo\SimpleSearch\ISearchIndexable;
use Arillo\SimpleSearch\SearchableExtension;
use Arillo\SimpleSearch\SearchIndexEntry;

class Page extends SiteTree implements ISearchIndexable // you need to implement this interface
{
    private static $extensions = [
        // adds after write/delete hooks to re-index this record
        SearchableExtension::class,
    ];

    // add interface function, it should return the string you want to add to your search index.
    public function forSearchIndex()
    {
        $contents = [
            $this->Title,
            $this->obj('Content')
                ->setProcessShortcodes(true)
                ->RAW()
        ];

        $string = implode($contents, ' ');
        return $string ? SearchIndexEntry::sanitize_string($string) : null;
    }
}

(重新)构建搜索索引

php vendor/silverstripe/framework/cli-script.php dev/tasks/Arillo-SimpleSearch-BuildIndexTask

与 Fluent 集成

search.yml 添加到您的配置文件中

Arillo\SimpleSearch\SearchIndexEntry:
  extensions:
    - TractorCow\Fluent\Extension\FluentExtension
  translate:
    - Title
    - SearchableText

与 arillo/elements 集成

添加扩展,因此更新/发布元素将触发持有页面的重新索引。

Arillo\Elements\ElementBase:
  extensions:
    - Arillo\SimpleSearch\ElementDataExtension

实现 ISearchIndexable,例如这样

<?php
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\View\SSViewer;
use SilverStripe\Core\Config\Config;
use Arillo\SimpleSearch\ISearchIndexable;
use Arillo\SimpleSearch\SearchableExtension;
use Arillo\SimpleSearch\SearchIndexEntry;
use Arillo\Elements\ElementsExtension;

class Page extends SiteTree implements ISearchIndexable // you need to implement this interface
{
	const SECTIONS = 'Sections';

    private static $extensions = [
        // adds after write/delete hooks to re-index this record
        SearchableExtension::class,
    ];

    // add interface function, it should return the string you want to add to your search index.
    public function forSearchIndex()
    {
	 if ($this->isPageWithSections()) {
            $oldThemes = SSViewer::get_themes();
            SSViewer::set_themes(
                Config::inst()->get(SSViewer::class, 'themes')
            );
            try {
                $string = SearchIndexEntry::sanitize_string(
                    $this->customise([
                        'RelationName' => self::SECTIONS
                    ])->renderWith('Elements')
                );
            } finally {
                SSViewer::set_themes($oldThemes);
            }
            return $string;
        }

        $contents = [
            $this->Title,
            $this->obj('Content')
                ->setProcessShortcodes(true)
                ->RAW()
        ];

        $string = implode($contents, ' ');
        return $string ? SearchIndexEntry::sanitize_string($string) : null;
    }
    
    public function isPageWithSections()
    {
        return isset(
            ElementsExtension::page_element_relation_names($this)[
                self::SECTIONS
            ]
        );
    }
}

内容剥离

当使用 SearchIndexEntry::sanitize_string 时,html 标签将从文本中删除。有可能标记 html 中的某些部分也被删除。如果你想要剥离导航元素或面包屑,这可能会很有用。为了完成这项工作,所有在 <!--<SearchExclude>--><!--</SearchExclude>--> 之间的内容都将被删除。

<!--<SearchExclude>-->
    Contents between this comments will be stripped
<!--</SearchExclude>-->