firesphere / elastic-search
使用 Elastic Enterprise 或 Elastic search 搜索 SilverStripe 网站内容
Requires
- php: >=8.0
- ext-json: *
- elasticsearch/elasticsearch: ^8.10
- firesphere/searchbackend: ^1.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.35
- phpunit/phpunit: ^9.5
- silverstripe/recipe-cms: ^4|^5
Suggests
- ext-pcntl: Support for multi-core indexing
Conflicts
This package is auto-updated.
Last update: 2024-09-09 04:02:37 UTC
README
Silverstripe 的 Elasticsearch 实现。
免责声明
如果此模块损坏了您的网站,您将保留所有零件。
致谢
- Pikselin Ltd.,本模块的部分创建得到了 Pikselin 的支持。
- Marco
Sheepy
Hermo,Marco 为 Solr 模块所做的预工作数量使得这项工作变得容易得多。
安装
composer require firesphere/elastic
要求
- PHP8+
- Elasticsearch 8.10+
- Silverstripe 框架 4 || 5
与 Silverstripe 集成的 Elastic search
本模块提供了一个类似于 Solr Search 模块的 API。
基本搜索索引
- 在您的 ElasticSearch 实例中创建一个 API-index
- 如果您有 Elastic Enterprise,这位于“搜索”>“API”
- 创建一个 API 密钥
- 您可以使用用户名/密码,但推荐使用 API 密钥
配置服务
配置是通过 YML 完成的
---
Name: MyElastic
---
Firesphere\ElasticSearch\Services\ElasticCoreService:
config:
endpoint:
- host: "https://my-elasticinstance.elastic-cloud.com"
apiKey: "mybase64apikeyhere==="
username: "Elastic"
password: "mysupersecretpassword"
port: 443
请注意端口。当使用您自己的 Elastic 实例时,这可能是一个标准端口 9200。在 Elastic Cloud 上,它通过端口 443(https)的所有路由都通过反向代理进行。
注意 显然,在 YML 中使用 API 密钥或密码从来不是一个好主意,但没关系,可以从环境中进行配置。
ELASTIC_ENDPOINT=host.example.com
ELASTIC_USERNAME=user@example.com
ELASTIC_PASSWORD=examplepassword
ELASTIC_API_KEY=mybase64apikeyhere===
ELASTIC_PORT=443
ELASTIC_PROTOCOL=https
并且在您的 YML 中
---
Name: MyElastic
---
Firesphere\ElasticSearch\Services\ElasticCoreService:
config:
endpoint: ENVIRONMENT
创建索引
索引有两个部分,类和配置。
最基础的类可能如下所示
<?php
namespace Firesphere\MyProject\Indexes;
use Firesphere\ElasticSearch\Indexes\ElasticIndex;
class ElasticProjectIndex extends ElasticIndex
{
public function getIndexName()
{
return 'search-indexname';
}
}
其中 search-indexname
是您在 Elastic 中配置时选择的索引名称。
配置字段的配套 YML 可能看起来像这样
Firesphere\ElasticSearch\Indexes\ElasticIndex:
search-indexname:
Classes:
- Page
FulltextFields:
- Title
- Content
- Description
- getElementsForSearch
- Impression.Title
FilterFields:
- OwnerID
FacetFields:
Firesphere\MyProject\Models\Tag:
BaseClass: Page
Field: Tags.ID
Title: Tag
这将索引时间将这些相关字段添加到索引中,并在搜索运行时确保所有字段都按需正确添加为过滤器。
进一步配置
请参阅 Solr 文档,并将那里的 YML 作为配置 Elastic 的指南。
目标是拥有几乎相同的 API,目前情况大致如此。
创建搜索
在选择的控制器中,以下是一个搜索的示例
class MyController extends PageController
{
public function search()
{
$query = $this->getRequest()->getVars();
if (isset($query['query'])) {
$baseQuery = new ElasticQuery();
// Add the term
$baseQuery->addTerm($query['query']);
// Ensure to start at 0
$start = isset($query['start']) ? $query['start'] : 0;
$baseQuery->setStart($start);
// Get the index
$index = new ElasticProjectIndex();
// And do the search
$this->Results = $index->doSearch($baseQuery);
}
return $this;
}
}
权限
与 Solr 搜索一样,所有文档都使用 ViewStatus
字段进行索引。该字段确定谁可以看到结果。在搜索运行时,该值基于当前用户计算,因此作为额外必需的过滤器传递。
进一步功能
完成(大致如此)
- [x] 基本过滤
- [x] 分页
- [x] 实际上,你知道...搜索
- [x] 高亮显示(大致如此)
- [x] 同义词
- [x] 组访问过滤(例如,所有、管理员、特定组、来自 CMS 的访问设置)
- [x] 提升度
- [x] 面板
- [x] 拼写检查
- [x] 单元测试和集成测试
待办事项列表包括
- [ ] 优化过滤
- [ ] 文件内容搜索
- [ ] 子模块
- [ ] 成员级别权限
- [ ] 子站点
- [ ] 流畅
许可
牛?
牛!
/( ,,,,, )\
_\,;;;;;;;,/_
.-"; ;;;;;;;;; ;"-.
'.__/`_ / \ _`\__.'
| (')| |(') |
| .--' '--. |
|/ o o \|
| |
/ \ _..=.._ / \
/:. '._____.' \
;::' / \ .;
| _|_ _|_ ::|
.-| '==o==' '|-.
/ | . / \ | \
| | ::| | | .|
| ( ') (. )::|
|: | |; U U ;|:: | `|
|' | | \ U U / |' | |
##V| |_/`"""`\_| |V##
##V## ##V##
赞助商
// @todo Firesphere 需要获得一些赞助商的标志(也许还有一些赞助商?)