codedefective/elasticsearch 问题修复 & 功能扩展

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

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

codedefective/elasticsearch

Composer 安装命令:

composer require codedefective/elasticsearch

包简介

An Elasticsearch implementation of Laravel's Eloquent ORM

README 文档

README

Laravel Elasticsearch

Latest Stable Version GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This library is a clone of pdphilip/laravel-elasticsearch

Laravel-Elasticsearch
An Elasticsearch implementation of Laravel's Eloquent ORM

The Power of Elasticsearch with Laravel's Eloquent

This package extends Laravel's Eloquent model and query builder with seamless integration of Elasticsearch functionalities. Designed to feel native to Laravel, this package enables you to work with Eloquent models while leveraging the powerful search and analytics capabilities of Elasticsearch.

The Eloquent you already know:

UserLog::where('created_at','>=',Carbon::now()->subDays(30))->get();
UserLog::create([
    'user_id' => '2936adb0-b10d-11ed-8e03-0b234bda3e12',
    'ip' => '62.182.98.146',
    'location' => [40.7185,-74.0025],
    'country_code' => 'US',
    'status' => 1,
]);
UserLog::where('status', 1)->update(['status' => 4]);
UserLog::where('status', 4)->orderByDesc('created_at')->paginate(50);
UserProfile::whereIn('country_code',['US','CA'])
    ->orderByDesc('last_login')->take(10)->get();
UserProfile::where('state','unsubscribed')
    ->where('updated_at','<=',Carbon::now()->subDays(90))->delete();

Elasticsearch with Eloquent:

UserProfile::searchTerm('Laravel')->orSearchTerm('Elasticsearch')->get();
UserProfile::searchPhrasePrefix('loves espressos and t')->highlight()->get();
UserProfile::whereMatch('bio', 'PHP')->get();
UserLog::whereGeoDistance('location', '10km', [40.7185,-74.0025])->get();
UserProfile::whereFuzzy('description', 'qick brwn fx')->get();

Built in Relationships (even to SQL models):

UserLog::where('status', 1)->orderByDesc('created_at')->with('user')->get();

Read the Documentation

Installation

Maintained versions (Elasticsearch 8.x):

Laravel 10.x, 11.x & 12.x (main):

composer require codedefective/elasticsearch
Laravel Version Command Maintained
Laravel 10/11/12 composer require codedefective/elasticsearch:~5 ✅ Active
Laravel 10/11 (v4) composer require codedefective/elasticsearch:~4 🛠️ LTS
Laravel 9 composer require codedefective/elasticsearch:~3.9 🛠️ LTS
Laravel 8 composer require codedefective/elasticsearch:~3.8 🛠️ LTS

Unmaintained versions (Elasticsearch 8.x):

Laravel Version Command Maintained
Laravel 7.x composer require codedefective/elasticsearch:~2.7 ❌ EOL
Laravel 6.x (5.8) composer require codedefective/elasticsearch:~2.6 ❌ EOL

Unmaintained versions (Elasticsearch 7.x):

Laravel Version Command Maintained
Laravel 9.x composer require codedefective/elasticsearch:~1.9 ❌ EOL
Laravel 8.x composer require codedefective/elasticsearch:~1.8 ❌ EOL
Laravel 7.x composer require codedefective/elasticsearch:~1.7 ❌ EOL
Laravel 6.x (5.8) composer require codedefective/elasticsearch:~1.6 ❌ EOL

Configuration

  1. Set up your .env with the following Elasticsearch settings:
ES_AUTH_TYPE=http
ES_HOSTS="http://localhost:9200"
ES_USERNAME=
ES_PASSWORD=
ES_CLOUD_ID=
ES_API_ID=
ES_API_KEY=
ES_SSL_CA=
ES_INDEX_PREFIX=my_app_
# prefix will be added to all indexes created by the package with an underscore
# ex: my_app_user_logs for UserLog model
ES_SSL_CERT=
ES_SSL_CERT_PASSWORD=
ES_SSL_KEY=
ES_SSL_KEY_PASSWORD=
# Options
ES_OPT_ID_SORTABLE=false
ES_OPT_VERIFY_SSL=true
ES_OPT_RETRIES=
ES_OPT_META_HEADERS=true
ES_ERROR_INDEX=
ES_OPT_BYPASS_MAP_VALIDATION=false
ES_OPT_DEFAULT_LIMIT=1000

For multiple nodes, pass in as comma-separated:

ES_HOSTS="http://es01:9200,http://es02:9200,http://es03:9200"
Example cloud config .env: (Click to expand)
ES_AUTH_TYPE=cloud
ES_HOSTS="https://xxxxx-xxxxxx.es.europe-west1.gcp.cloud.es.io:9243"
ES_USERNAME=elastic
ES_PASSWORD=XXXXXXXXXXXXXXXXXXXX
ES_CLOUD_ID=XXXXX:ZXVyb3BlLXdl.........SQwYzM1YzU5ODI5MTE0NjQ3YmEyNDZlYWUzOGNkN2Q1Yg==
ES_API_ID=
ES_API_KEY=
ES_SSL_CA=
ES_INDEX_PREFIX=my_app_
ES_ERROR_INDEX=
  1. In config/database.php, add the elasticsearch connection:
'elasticsearch' => [
    'driver' => 'elasticsearch',
    'auth_type' => env('ES_AUTH_TYPE', 'http'), //http or cloud
    'hosts' => explode(',', env('ES_HOSTS', 'http://localhost:9200')),
    'username' => env('ES_USERNAME', ''),
    'password' => env('ES_PASSWORD', ''),
    'cloud_id' => env('ES_CLOUD_ID', ''),
    'api_id' => env('ES_API_ID', ''),
    'api_key' => env('ES_API_KEY', ''),
    'ssl_cert' => env('ES_SSL_CA', ''),
    'ssl' => [
        'cert' => env('ES_SSL_CERT', ''),
        'cert_password' => env('ES_SSL_CERT_PASSWORD', ''),
        'key' => env('ES_SSL_KEY', ''),
        'key_password' => env('ES_SSL_KEY_PASSWORD', ''),
    ],
    'index_prefix' => env('ES_INDEX_PREFIX', false),
    'options' => [
        'bypass_map_validation' => env('ES_OPT_BYPASS_MAP_VALIDATION', false),
        'logging' => env('ES_OPT_LOGGING', false),
        'ssl_verification' => env('ES_OPT_VERIFY_SSL', true),
        'retires' => env('ES_OPT_RETRIES', null),
        'meta_header' => env('ES_OPT_META_HEADERS', true),
        'default_limit' => env('ES_OPT_DEFAULT_LIMIT', 1000),
        'allow_id_sort' => env('ES_OPT_ID_SORTABLE', false),
    ],
],

3. If packages are not autoloaded, add the service provider:

For Laravel 11 +:

//bootstrap/providers.php
<?php
return [
    App\Providers\AppServiceProvider::class,
    codedefective\Elasticsearch\ElasticServiceProvider::class,
];

For Laravel 10 and below:

//config/app.php
'providers' => [
    ...
    ...
    codedefective\Elasticsearch\ElasticServiceProvider::class,
    ...

Now, you're all set to use Elasticsearch with Laravel as if it were native to the framework.

Documentation Links

Getting Started

Eloquent

Relationships

Migrations: Schema/Index

Misc

Credits

License

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

Looking to use Elasticsearch for full-text search of your SQL models?

ElasticLens for Laravel

codedefective/elasticsearch 适用场景与选型建议

codedefective/elasticsearch 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 codedefective/elasticsearch 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-29