定制 fareselshinawy/elastic-search 二次开发

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

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

fareselshinawy/elastic-search

Composer 安装命令:

composer require fareselshinawy/elastic-search

包简介

elastic-search package provides a seamless integeration of elastic search into laravel appications, desinged to simplify search and indexing functionalities. It offers an easy-to-use interface for managing elastic search queries, along with full support for multi tenant and standard projects.

README 文档

README

The package provides seamless integration with Elasticsearch, supporting multi-tenant architecture and offering an intuitive query builder interface.

Features

  • Easy integration with Laravel models
  • Built-in multi-tenant support using stancl/tenancy
  • Fluent query builder interface
  • Automated index management
  • Configurable schema mappings
  • Relationship syncing support
  • Artisan commands for index management

Installation

You can install the package via composer:

composer require fareselshinawy/elasticsearch

If you plan to use multi-tenant features: multi tenant can be extend to other packages be making your own tenant adapter implementing TenantAdapterInterface

composer require stancl/tenancy

Configuration

Publish the configuration file:

php artisan vendor:publish --provider="Fareselshinawy\ElasticSearch\Providers\ElasticSearchServiceProvider"

Configure your Elasticsearch credentials in your .env file:

ELASTIC_API_ENDPOINT=your-elasticsearch-endpoint
ELASTIC_API_KEY=your-api-key
ELASTIC_INDEX_PREFIX=optional-prefix
ELASTIC_DEFAULT_SIZE_VARIABLE=10
ELASTIC_INDEX_SYNC_CHUNK_SIZE=500
ENABLE_ELASTIC_FOR_TENANTS=true

Usage

Model Setup

To make your model searchable with Elasticsearch, add the following methods to your model:

use Fareselshinawy\ElasticSearch\Traits\ElasticSearchable;

class User extends Model
{
    use ElasticSearchable;

    // Required: Define the data to be synchronized with Elasticsearch
    public function getElasticSearchSyncAbleData()
    {
        return [
            "name"  => Str::lower($this->name),
            "email" => $this->email,
            "id"    => $this->id
        ];
    }

    // Optional: Define custom index mappings
    public function getIndexableProperties()
    {
        return [
            "name"  => ['type' => 'keyword'],
            "email" => ['type' => 'keyword'],
            "id"    => ['type' => 'integer']
        ];
    }

    // Optional: Define relations to sync as example department:id,name..etc
    public $elasticSyncAbleRelations = [];

    // Optional: Define dependent indexes that should be updated ( relation => class ) users => User::class
    public $dependentIndexesRelations = [];

    // Optional: Custom index name
    public function getElasticIndexKey()
    {
        return $this->indexPrefix() . 'users';
    }
}

Query Builder Usage

The package provides a fluent interface for building Elasticsearch queries:

// Basic queries
User::elasticWhere('name', 'John')
    ->elasticOrWhere('email', 'john@example.com')
    ->elasticGet();

// Wildcard searches
User::elasticWhereLike('name', 'jo')
    ->elasticWhereStartWith('email', 'john')
    ->elasticGet();

// Range queries
User::elasticRange('age', 18, 30)->elasticGet();

// Term queries
User::elasticTermMust('status', 'active')
    ->elasticTermMustNot('role', 'guest')
    ->elasticGet();

// Match queries
User::elasticWhereMatch('description', 'search text')
    ->elasticWhereMatchPhrase('title', 'exact phrase')
    ->elasticGet();

// Nested queries
User::elasticNested('orders', function($query) {
    $query->elasticWhere('status', 'completed');
})->elasticGet();

// Sorting
User::elasticSortBy('created_at', 'desc')->elasticGet();

// Pagination
User::elasticWhere('status', 'active')->elasticPaginate(15);

Available Query Methods

Term Queries

  • elasticTerm(field, value, conditionType = 'must')
  • elasticTermMust(field, value)
  • elasticTermMustNot(field, value)
  • elasticTermShould(field, value)

Terms Queries (Arrays)

  • elasticTerms(field, values, conditionType = 'must')
  • elasticTermMustIn(field, values)
  • elasticTermMustNotIn(field, values)
  • elasticTermShouldIn(field, values)

Match Queries

  • elasticMatch(field, value, conditionType = 'must')
  • elasticWhereMatch(field, value)
  • elasticOrWhereMatch(field, value)
  • elasticMatchMustNot(field, value)

Wildcard Queries

  • elasticWildcard(field, value)
  • elasticWildcardLike(field, value)
  • elasticWildcardStartWith(field, value)
  • elasticWildcardEndWith(field, value)

Range Queries

  • elasticRange(field, from, to)
  • elasticGreaterOrEqual(field, value)
  • elasticLessOrEqual(field, value)

Artisan Commands

The package provides several Artisan commands for index management:

Standard Commands

php artisan elastic:index-create        # Create index
php artisan elastic:index-delete        # Delete index
php artisan elastic:index-sync          # Sync data
php artisan elastic:index-update        # Update index

Multi-tenant Commands

php artisan elastic:tenants-index-create  # Create tenant index
php artisan elastic:tenants-index-delete  # Delete tenant index
php artisan elastic:tenants-index-sync    # Sync tenant data
php artisan elastic:tenants-index-update  # Update tenant index

For tenant-specific operations, use the --tenantReference flag:

php artisan elastic:tenants-index-sync --tenantReference=tenant1

Multi-tenant Support

The package integrates with stancl/tenancy for multi-tenant support. Configure tenant-specific settings in the config file:

'enable_elastic_for_tenants' => true,
'tenants_indexable' => [
    'user' => new App\Models\User
],
'tenant_model' => 'App\Models\Tenant',
'tenancy_facade' => 'Stancl\Tenancy\Facades\Tenancy',

Security

If you discover any security-related issues, please email [faresleshinawy560@gmail.com] instead of using the issue tracker.

Credits

  • [Fares Ashraf Ibrahim Elshinawy]

License

The MIT License (MIT). Please see License File for more information.

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-26