babenkoivan/elastic-scout-driver 问题修复 & 功能扩展

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

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

babenkoivan/elastic-scout-driver

Composer 安装命令:

composer require babenkoivan/elastic-scout-driver

包简介

Elasticsearch driver for Laravel Scout

README 文档

README

Support the project!

Elasticsearch driver for Laravel Scout.

Contents

Compatibility

The current version of Elastic Scout Driver has been tested with the following configuration:

  • PHP 8.3
  • Elasticsearch 9.x
  • Laravel 13.x
  • Laravel Scout 11.x

If your project uses older Elasticsearch, Laravel, or PHP version check the previous major version of the package.

Installation

The library can be installed via Composer:

composer require babenkoivan/elastic-scout-driver

Note, that this library is just a driver for Laravel Scout, don't forget to install it beforehand:

composer require laravel/scout

After Scout has been installed, publish its configuration file using:

php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"

Then, change the driver option in the config/scout.php file to elastic:

// config/scout.php

'driver' => env('SCOUT_DRIVER', 'elastic'),

If you want to use Elastic Scout Driver with Lumen framework check this guide.

Configuration

Elastic Scout Driver uses babenkoivan/elastic-client as a dependency. To change the client settings you need to publish the configuration file first:

php artisan vendor:publish --provider="Elastic\Client\ServiceProvider"

In the newly created config/elastic.client.php file you can define the default connection name using configuration hashes. Please, refer to the elastic-client documentation for more details.

Elastic Scout Driver itself has only one configuration option at the moment - refresh_documents. If it's set to true (false by default) documents are indexed immediately, which might be handy for testing.

You can configure refresh_documents in the config/elastic.scout_driver.php file after publishing it with the following command:

php artisan vendor:publish --provider="Elastic\ScoutDriver\ServiceProvider"

At last, do not forget, that with Scout you can configure the searchable data, the model id and the index name. Check the official Scout documentation for more details.

Note, that the _id field can't be part of the searchable data, so make sure the field is excluded or renamed in the toSearchableArray method in case you are using MongoDB as the database.

Basic usage

Elastic driver uses Elasticsearch query string wrapped in a bool query under the hood. It means that you can use mini-language syntax when searching a model:

$orders = App\Order::search('title:(Star OR Trek)')->get();

When the query string is omitted, the match all query is used:

$orders = App\Order::search()->where('user_id', 1)->get();

Please refer to the official Laravel Scout documentation for more details and usage examples.

Advanced Search

In case the basic search doesn't cover your project needs check Elastic Scout Driver Plus, which extends standard Scout search capabilities by introducing advanced query builders. These builders give you possibility to use compound queries, custom filters and sorting, highlights and more.

Migrations

If you are looking for a way to control Elasticsearch index schema programmatically check Elastic Migrations. Elastic Migrations allow you to modify application's index schema and share it across multiple environments with the same ease, that gives you Laravel database migrations.

Pitfalls

There are few things, which are slightly different from other Scout drivers:

  • As you probably know, Scout only indexes fields, which are returned by the toSearchableArray method. Elastic driver indexes a model even when toSearchableArray returns an empty array. You can change this behaviour by overwriting the shouldBeSearchable method of your model:
public function shouldBeSearchable()
{
    return count($this->toSearchableArray()) > 0;
}
  • Raw search returns an instance of SearchResult class (see Elastic Adapter):
$searchResult = App\Order::search('Star Trek')->raw();
  • To be compatible with other drivers and to not expose internal implementation of the engine, Elastic driver ignores callback parameter of the search method:
App\Order::search('Star Trek', function () {
    // this will not be triggered
})->get()

babenkoivan/elastic-scout-driver 适用场景与选型建议

babenkoivan/elastic-scout-driver 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.1M 次下载、GitHub Stars 达 277, 最近一次更新时间为 2020 年 02 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 4.1M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 282
  • 点击次数: 32
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 277
  • Watchers: 4
  • Forks: 36
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-02-15