toastnz/opensearch
Composer 安装命令:
composer require toastnz/opensearch
包简介
A simple OpenSearch wrapper for SilverStripe, built on top of the official OpenSearch PHP client.
README 文档
README
A wrapper around the OpenSearch PHP client.
Requirements
See the root composer.json.
Installation
composer require toastnz/opensearch
Helper Configuration
Configure the helper through Injector properties:
SilverStripe\Core\Injector\Injector: Toast\OpenSearch\Helpers\OpenSearch: properties: client_host: '`OPENSEARCH_HOST`' client_username: '`OPENSEARCH_USERNAME`' client_password: '`OPENSEARCH_PASSWORD`' client_verify: false index_class: App\Search\SiteSearchIndex record_operation_fail_silently: true
Supported helper properties:
client_hostclient_usernameclient_passwordclient_verifyindex_classdefault_search_indexrecord_operation_fail_silently
record_operation_fail_silently defaults to true. It only affects automatic record-driven indexing through Toast\OpenSearch\Extensions\DataObjectExtension, where insert, update, delete, publish, and unpublish operations fail quietly if the index is missing, not configured, or unreachable. It does not apply to direct index operations such as OpenSearchManagerTask, OpenSearchReindexTask, initIndex(), clearIndex(), or deleteIndex().
Defining an Index
Create a subclass of Toast\OpenSearch\Search\OpenSearchIndex and override protected properties before calling parent::__construct().
use SilverStripe\Assets\File; use SilverStripe\CMS\Model\RedirectorPage; use SilverStripe\CMS\Model\SiteTree; use SilverStripe\CMS\Model\VirtualPage; use SilverStripe\ErrorPage\ErrorPage; use Toast\OpenSearch\Search\OpenSearchIndex; class SiteSearchIndex extends OpenSearchIndex { public function __construct(?string $indexName = null) { $this->indexName = $indexName ?? 'default_index'; $this->fields = [ 'Title' => ['type' => 'text'], 'Content' => ['type' => 'text'], 'PublishDate' => 'Datetime', ]; $this->searchFields = [ 'Title^2', 'Content', 'SearchContent', 'ElementalArea.Elements.Title', ]; $this->filters = [ 'SubsiteID' => ['type' => 'integer'], 'PublishDate' => 'Datetime', ]; $this->includedClasses = [SiteTree::class, File::class]; $this->excludedClasses = [ ErrorPage::class, RedirectorPage::class, VirtualPage::class, ]; $this->filterField = 'ShowInSearch'; parent::__construct($indexName); } }
Important: the base constructor only accepts an optional index name. It does not accept named arguments such as fields, searchFields, or filters.
Querying
use Toast\OpenSearch\Helpers\OpenSearch; $results = OpenSearch::singleton()->search('annual report', null, [ 'filters' => [ 'SubsiteID' => 2, 'PublishDate' => ['gte' => '2026-01-01'], ], ]);
The second argument can be:
- an
OpenSearchIndexinstance - an index-definition class name
- a literal OpenSearch index name
nullfor the configured default
SiteConfig Search Tuning
The bundled SiteConfigExtension adds four CMS tabs under Root.OpenSearch:
Weightsfor per-field search weightsFineTunefor search-time query behaviourSynonymsfor runtime query expansion rulesMorefor running a search and reviewing per-result OpenSearch explanations
When silverstripe/subsites is installed and the active index declares SubsiteID as a filter, the More explanation search automatically scopes results to the current CMS subsite from SubsiteState, while still including records with SubsiteID 0 or no SubsiteID.
FineTune stores non-default values in OpenSearchFineTuneSettings and applies them to generated searches at runtime. The available controls are:
type(multi_match.type)operatorminimum_should_matchfuzzinessmin_score
Defaults preserve the module's existing behaviour:
type: omitted, which matches the currentmulti_matchdefault behaviouroperator: omitted, which matches the currentmulti_matchdefault behaviourminimum_should_match: omittedfuzziness: omittedmin_score: omitted
When type is cross_fields, phrase, or phrase_prefix, configured fuzziness is ignored because OpenSearch does not allow that combination.
min_score is only added to generated request bodies when a search term is present or an explicit query option is supplied, so empty filter-only searches continue to behave as they do today.
Synonyms stores rules as related OpenSearchSynonym records on the current SiteConfig and applies them to generated searches at runtime, so changes take effect without reindexing or recreating the index.
In the CMS, editors manage synonyms with a GridField under Root.OpenSearch.Synonyms.
Each row has two simple lists:
When searching for theseAlso search for these
Search Form
The module ships with:
Toast\OpenSearch\Forms\SearchFormToast\OpenSearch\Extensions\ContentControllerExtension
The controller extension adds SearchForm() and searchResults() to ContentController.
The form reads the Search query parameter, runs the helper query, and hydrates matches back into DataObject records when possible.
Search form pagination config:
Toast\OpenSearch\Forms\SearchForm: enable_pagination: true results_per_page: 10
Tasks
Index manager:
/dev/tasks/OpenSearchManagerTask?action=status
Supported actions:
initresetcleardeletestatus
Full reindex:
/dev/tasks/OpenSearchReindexTask
Notes
searchFieldsentries not declared infieldsare mapped astextfiltersare both mapped and included in indexed documentsIDandClassNameare always indexed- versioned classes are reindexed from
Live - undeclared runtime filters are ignored
Toast\OpenSearch\Extensions\DataObjectExtensiononly performs automatic indexing for records included by the activeOpenSearchIndex, while still reindexing dependent parent records for declared relation chains and respectingexcludedClasses
toastnz/opensearch 适用场景与选型建议
toastnz/opensearch 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 265 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「search」 「silverstripe」 「fulltext」 「ranking」 「similarity」 「fuzzy」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 toastnz/opensearch 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 toastnz/opensearch 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 toastnz/opensearch 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel package to retrieve data from Google Search Console
Laravel and Lumen ElasticSearch query builder to build complex queries using an elegant syntax
Indexed Search Autocomplete - Extends the TYPO3 Core Extension Indexed_Search searchform with an autocomplete feature.
Abstraction Layer to index and search entities
Analytics chooser extensions for site settings.
统计信息
- 总下载量: 265
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2026-04-16