tuanlerepo/statamic-es 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tuanlerepo/statamic-es

最新稳定版本:v1.0.1

Composer 安装命令:

composer require tuanlerepo/statamic-es

包简介

Elasticsearch driver for Statamic

README 文档

README

This addon add's a Elasticsearch driver to Statamics builtin search system ( docs ).

Setup index

Indexes are configured in config/statamic/search.php and uses the standard Statamic index configuration. There's a few extra nice configuration options.

Here's an entry in indexes for the pages collection:

'public' => [
  'driver' => 'elasticsearch',
  'searchables' => 'collection:pages',
  'fields' => ['title', 'description', 'content'],
],

Here's an example entry in the drivers section defining the connection properties for your Elasticsearch server.

'elasticsearch' => [
    'hosts' => [[
       'scheme' => env('ELASTICSEARCH_SCHEME', 'http'),
       'host' => env('ELASTICSEARCH_HOST', '127.0.0.1'),
       'port' => env('ELASTICSEARCH_PORT', 9200),
    ]]
],

Content transformation

Statamic supports transformers for the index data, but the current implementation is not compatible with how Laravel serializes the configuration when running php artisan optimize. Therefore this driver has it's own system that can be configured like this:

'public' => [
  ...
  'transforms' => ['content' => 'bardToText'],
],

Available transformers

  • 'bardToText`, converts the ProseMirror data structure into plain text so it can be indexed.
  • 'handle', converts fields that has a handle (eg. site and collection) into the handle text.

Analyzers

Elasticsearch supports different text analyzers for indexing. The analyzer determines how tokenization, stop-words and stemming is handled. You can set the default analyzer used (for all fields) like this:

'public' => [
  'driver' => 'elasticsearch',
  'searchables' => 'collection:pages',
  'fields' => ['title', 'description', 'content'],
  'analyzer' => 'danish'
],

If not set, the standard analyzer is used. See list of available language analyzers here.

Field boost

You can boost individual fields by defining a boost value.

'boost' => [
    'title' => '2',
    'keywords' => '2'
]

Updating indexes

Whenever you save an item in the Control Panel it will automatically update any appropriate indexes as this is extending the Statamic search. As expected you can update the index via command line.

php please search:update public

Verify indexed data

You can verify that your elaticsearch index looks correct with:

curl 127.0.0.1:9200/_cat/indices/public?v

Or make queries directly with:

curl 127.0.0.1:9200/public/_search?q=test | jq

Templating

You can use the default Statamic search tag like usual:

{{ search:results index="public"}}
    {{ if no_results }}
        <h2>No results.</h2>
    {{ else }}
        <a href="{{ url }}">
            <div>{{ title }}</div>
            <p>{{ description | truncate:180 }}</p>
        </a>
    {{ /if }}
{{ /search:results }}

Livewire componenent with pagination

The included Livewire component supports pagination using Elasticsearch for the pagination. This requires that the fields status are indexed so add them to the config:

'public' => [
  ....
  'fields' => [ ... 'status'],
],

If you have multiple sites and wan't the search page to only return results from a specific site then you must add the site field to the index. The site field is an object so it must be transformed with the handle transformer.

'public' => [
  ....
  'fields' => [ ... 'status', 'site'],
  'transforms' => ['site' => 'handle'],
],     

Then you need to have Livewire support for your antlers templates. The easy way is to install Jonas's addon :

composer require jonassiewertsen/statamic-livewire

Then make your search antlers template including:

{{ livewire:styles }}
{{ livewire:elasticsearch.search index="public" size="10"}}
{{ livewire:scripts }}  

The template can be overridden by publishing it:

php artisan vendor:publish --tag=elasticsearch-views

Planned features

  • Set analyzer based om documents site locale
  • Configurable multi-match query type

统计信息

  • 总下载量: 1.22k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: proprietary
  • 更新时间: 2024-12-11