定制 firesphere/elastic-search 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

firesphere/elastic-search

Composer 安装命令:

composer require firesphere/elastic-search

包简介

Search a SilverStripe site with Elastic Enterprise or Elastic search

README 文档

README

Elasticsearch implementation for Silverstripe.

Disclaimer

If this module breaks your website, you get to keep all the pieces.

Acknowledgements

  • Pikselin Ltd., Part of this module has been created thanks to support from Pikselin
  • Marco Sheepy Hermo, the amount of pre-work from the Solr module, done by Marco, has made this work a lot easier

Installation

composer require firesphere/elastic

Requirements

  • PHP8+
  • Elasticsearch 8.10+
  • Silverstripe Framework 4 || 5

Elastic search with Silverstripe

This module provides an API similar to the Solr Search module.

Basic search index

  • Create an API-index in your ElasticSearch instance
    • If you have Elastic Enterprise, this is under Search => API
  • Create an API key
    • You can use a username/password, but API key is recommended

Configuring the service

Configuration is done in YML:

---
Name: MyElastic
---
Firesphere\ElasticSearch\Services\ElasticCoreService:
  config:
    endpoint:
      - host: "https://my-elasticinstance.elastic-cloud.com"
        apiKey: "mybase64apikeyhere==="
        username: "Elastic"
        password: "mysupersecretpassword"
        port: 443

Take special note of the port. When using your own Elastic instance, this might be the standard port 9200. On Elastic Cloud, it's all routed through a reverse proxy on port 443 (https).

NOTE It's obviously never a great idea to use api keys or passwords in YML, but that's okay, to configure it from environment:

ELASTIC_ENDPOINT=host.example.com
ELASTIC_USERNAME=user@example.com
ELASTIC_PASSWORD=examplepassword
ELASTIC_API_KEY=mybase64apikeyhere===
ELASTIC_PORT=443
ELASTIC_PROTOCOL=https

And in your YML:

---
Name: MyElastic
---
Firesphere\ElasticSearch\Services\ElasticCoreService:
  config:
    endpoint: ENVIRONMENT

Creating an index

An index has two parts, the class and the configuration.

The most basic class would by something like the following:

<?php


namespace Firesphere\MyProject\Indexes;

use Firesphere\ElasticSearch\Indexes\ElasticIndex;

class ElasticProjectIndex extends ElasticIndex
{
    public function getIndexName()
    {
        return 'search-indexname';
    }
}

Where search-indexname is the name of the index you've chosen when configuring it in Elastic.

The accompanying YML that configures the fields would potentially look like this:

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

This would at index time add those related fields in to the index, as well as at search runtime ensure all the fields are properly added as filters, where needed.

Further configuration

Please refer to the Solr documentation, and take the YML there as a guideline for configuring Elastic.

The goal is to have a near-identical API, which is largely the case already.

Creating a search

In a controller of choice, here is an example of a search:

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;
    }
}

Permissions

As with the Solr search, all documents are indexed with a ViewStatus field. This field determines who can see the results. At search runtime, the value is calculated based on the current user and as such passed in as an extra, required, filter.

Further functionality

Done(~ish)

  • [x] Basic filtering
  • [x] Pagination
  • [x] Actually, you know... search
  • [x] Highlighting~ish
  • [x] Synonyms
  • [x] Group-access filtering (e.g. all, administrators, specific groups, from access setting in the CMS)
  • [x] Boosting
  • [x] Faceting
  • [x] Spellchecking
  • [x] Unit tests and integration tests

On the to-do list is:

  • [ ] Work out the filtering better
  • [ ] File content searching
  • [ ] Submodules for
    • [ ] Member level permissions
    • [ ] Subsites
    • [ ] Fluent

License

LGPL v3.0 or later

Cow?

Cow!


             /( ,,,,, )\
            _\,;;;;;;;,/_
         .-"; ;;;;;;;;; ;"-.
         '.__/`_ / \ _`\__.'
            | (')| |(') |
            | .--' '--. |
            |/ o     o \|
            |           |
           / \ _..=.._ / \
          /:. '._____.'   \
         ;::'    / \      .;
         |     _|_ _|_   ::|
       .-|     '==o=='    '|-.
      /  |  . /       \    |  \
      |  | ::|         |   | .|
      |  (  ')         (.  )::|
      |: |   |;  U U  ;|:: | `|
      |' |   | \ U U / |'  |  |
      ##V|   |_/`"""`\_|   |V##
         ##V##         ##V##

Sponsors

// @todo Firesphere needs to get some sponsor logos (And maybe some sponsors?)

firesphere/elastic-search 适用场景与选型建议

firesphere/elastic-search 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 235 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 12 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「configuration」 「search」 「elastic」 「silverstripe」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 firesphere/elastic-search 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 firesphere/elastic-search 我们能提供哪些服务?
定制开发 / 二次开发

基于 firesphere/elastic-search 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: LGPL-3.0-or-later
  • 更新时间: 2023-12-01