承接 daalvand/laravel-elasticsearch 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

daalvand/laravel-elasticsearch

Composer 安装命令:

composer require daalvand/laravel-elasticsearch

包简介

Use Elasticsearch as a database in Laravel to retrieve Eloquent models and perform aggregations.

README 文档

README

Use Elasticsearch as a database in Laravel to retrieve Eloquent models and perform aggregations.

Build Elasticsearch queries as you're used to with Eloquent, and get Model instances in return, with some nice extras:

  • Use query, filter and postFilter query types
  • Perform geo searches
  • Build and perform complex aggregations on your data
  • Use the Elasticsearch scroll API to retrieve large numbers of results

Setup

Add elasticsearch connection configuration to database.php

'elasticsearch' => [
    'driver'   => 'elasticsearch',
    'host'     => 'localhost',
    'port'     => 9200,
    'database' => 'your_es_index',
    'username' => 'optional_es_username',
    'password' => 'optional_es_username',
    'suffix'   => 'optional_es_index_suffix',
]

Search

You're now ready to carry out searches on your data. The query will look for an Elasticsearch index with the same name as the database table that your models reside in.

$documents = MyModel::newElasticsearchQuery()
              ->where('date', '>', Carbon\Carbon::now())
              ->get();

Aggregations

Aggregations can be added to a query with an approach that's similar to querying Elasticsearch directly, using nested functions rather than nested arrays. The aggregation() method takes three or four arguments:

  1. A key to be used for the aggregation
  2. The type of aggregation, such as 'filter' or 'terms'
  3. (Optional) A callback or array providing options for the aggregation
  4. (Optional) A function allowing you to provide further sub-aggregations
$myQuery = MyModel::newElasticsearchQuery()
             ->aggregation(
                 // The key of the aggregation (used in the Elasticsearch response)
                 'my_filter_aggregation',

                 // The type of the aggregation
                 'filter',

                 // A callback providing options to the aggregation, in this case adding filter criteria to a query builder
                 function ($query) {
                     $query->where('lost', '!=', true);
                     $query->where('concierge', true);
                 },

                 // A callback specifying a sub-aggregation
                 function ($builder) {
                     // A simpler aggregation, counting terms in the 'status' field
                     $builder->aggregation('my_terms_aggregation', 'terms', ['field' => 'status']);
                 }
             );

$results = $myQuery->get();
$aggregations = $myQuery->getQuery()->getAggregationResults();

Geo queries

You can filter search results by distance from a geo point or include only those results that fall within given bounds, passing arguments in the format you'd use if querying Elasticsearch directly.

$withinDistance = MyModel::newElasticsearchQuery()
                    ->whereGeoDistance('geo_field', [$lat, $lon], $distance);

$withinBounds= MyModel::newElasticsearchQuery()
                 ->whereGeoBoundsIn('geo_field', $boundingBox);

Scroll API

You can use a scroll search to retrieve large numbers of results. Rather than returning a Collection, you'll get a PHP Generator function that you can iterate over, where each value is a Model for a single result from Elasticsearch.

$documents = MyModel::newElasticsearchQuery()
               ->limit(100000)
               ->usingScroll()
               ->get();

// $documents is a Generator
foreach ($documents as $document){
  echo $document->id;
}

Console

This package ships with the following commands to be used as utilities or as part of your deployment process.

Command Arguments Options Description
make:mapping name: Name of the mapping. This name also determines the name of the index and the alias. --update: Whether the mapping should update an existing index. --template: Pass a pre-existing mapping filename to create your new mapping from. Creates a new mapping migration file.
migrate:mappings index-command: (Optional) Name of your local Artisan console command that performs the Elasticsearch indexing. If not given, command will be retrieved from laravel-elasticsearch config file. --index : Automatically index new mapping.--swap: Automatically update the alias after the indexing has finished. Migrates your mapping files and begins to create the index.
index:rollback Rollback to the previous index migration.
index:remove index: (Optional) Name of the index to remove from your Elasticsearch cluster. Removes an index from your Elasticsearch cluster.
index:swap alias: Name of alias to update. index: Name of index to update alias to. old-index: (Optional) Name of old index. --remove-old-index: Remove old index from your Elasticsearch cluster. Swap the index your alias points to.
index:list --alias: List active aliases. Pass "*" to view all. Other values filter the returned aliases. Display a list of all indexes in your Elasticsearch cluster.
index:copy from: index to copy from. to: the index to copy from Populate an index with all documents from another index

Mappings and Aliases

When creating a new index during a migrate:mappings the command will automatically create an alias based on the migration name by removing the date string. For example the migration 2018_08_03_095804_users.json will create the alias users.

During the first migration an index appears in the migrate:mappings command will also switch the alias to the latest index mapping. The above will only happen when the alias does not already exist.

Future migrations will require you to use the --swap option.

daalvand/laravel-elasticsearch 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-04-01