somardesignstudios / silverstripe-elastic-search
一个提供使用elasticsearch进行搜索的silverstripe模块
0.4.2
2024-05-08 00:59 UTC
Requires
- php: >=8.2.0
- elasticsearch/elasticsearch: ^7.6
- ramsey/uuid: ^4.7
- silverstripe/segment-field: ^2.0 || ^3.0
- symbiote/silverstripe-queuedjobs: ^4.0 || ^5.0
Requires (Dev)
- phpunit/phpunit: ^10.3
This package is auto-updated.
Last update: 2024-09-08 01:50:24 UTC
README
silverstripe模块,提供使用elasticsearch进行全站内容搜索
要求
- SilverStripe ^4.0
- elasticsearch-php
- ramsey/uuid
- SilverStripe Queued Jobs Module
安装
- 安装模块
composer require somardesignstudios/silverstripe-elastic-search
- 在项目根目录下创建
log
文件夹,并赋予运行网站的用户的写入权限
Elastic连接配置
将您的elasticsearch API密钥添加到.env文件中
# Elastic search
ELASTIC_CLOUD_ID=""
ELASTIC_INDEX=""
ELASTIC_API_ID=""
ELASTIC_API_KEY=""
搜索配置
字段映射配置
页面内容将被展平并存储在此模块的search.yml
中定义的标准字段集中
您可以通过向您的项目添加search.yml
配置来添加更多字段。例如:
--- Name: my_search After: - "#somar_search" --- Somar\Search\ElasticSearchService: mappingProperties: custom_field_one: type: text store: true custom_field_two: type: date store: true
搜索字段
您可以在配置文件中更改默认搜索字段及其权重,您还可以在匹配字段中定义高亮匹配
Somar\Search\ElasticSearchService: searchFields: - title^2 - keywords^2.5 - content highlightFields: - content
搜索页面
在搜索页面中,您将找到一个可由.yml文件配置的Vue组件
Somar\Search\PageType\SearchPage: searchConfig: headingLevel: 1 allowEmptyKeyword: false secondarySearch: documents icons: material # adds material icon into the keyword field & dropdown tag close caretIconClass: "icon-dropdown" # adds <i class="icon-dropdown"> to dropdowns labels: title: Start typing to search the content # all title fields & subtitle can have placeholders and html titleFound: "Search results for <b>“[searchedKeyword]”</b>" titleSearching: "Searching for <b>“[keyword]”</b> ..." subtitle: "[resultsCount] results found" resultLinkText: Read more # uses URL of the result when not defined filtersHint: Refine your search results below by selecting popular filters and/or ordering them by date. filters: type: placeholder: Type of content field: type columns: 4 # width out of 12 columns on desktop, defaults to 6, set to 6 on tablet & 12 on mobile multiple: true # allows multiple values selected showInline: true # display all options inline rather than in dropdown default: news # preselected filter searchable: false iconClass: icon-type # adds <i class="icon-type"> to dropdowns options: news: name: News filter: App\Page\NewsArticle events: name: Events filter: App\Page\Event content: name: Content filter:not: - App\Page\NewsArticle - App\Page\Event - App\Model\Document documents: name: Documents filter: App\Model\Document date: placeholder: By date field: sort_date options: desc: name: Most recent first asc: name: Oldest first range: name: Select dates
搜索类型
当需要具有不同配置的多页搜索时,您可以添加额外的配置集。当定义了额外的搜索类型时,搜索页面将出现下拉菜单。额外的搜索类型的配置将合并到默认配置中。
Somar\Search\PageType\SearchPage: searchTypes: documents: name: Document library presets: type: documents allowEmptyKeyword: true labels: title: Search publications filters: topics: placeholder: Topics field: topics multiple: true tag: App\Model\Topic categories: placeholder: Categories field: categories multiple: true tag: App\Model\Category regions: placeholder: Regions field: regions multiple: true tag: App\Model\Region date: field: published
索引创建
要创建索引,请运行/dev/tasks/Somar-Search-Task-CreateIndexTask
。此任务将创建索引,设置映射并创建处理附件的管道。
当索引创建完成后,使用SearchIndexJob
来索引网站内容(Page类的对象)。您可以通过.yml配置添加其他类来索引。
Somar\Search\Job\SearchIndexJob: IndexedClasses: - App\Model\Document