定制 torann/laravel-hunt 二次开发

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

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

torann/laravel-hunt

Composer 安装命令:

composer require torann/laravel-hunt

包简介

Unified search for Laravel models using Elasticsearch.

README 文档

README

Latest Stable Version Total Downloads Patreon donate button Donate weekly to this project using Gratipay Donate to this project using Flattr Donate to this project using Paypal

Unified search for Laravel models using Elasticsearch. Laravel Hunt uses the official Elasticsearch PHP API. To get started, you should have a basic knowledge of how Elasticsearch works (indexes, types, mappings, etc).

Elasticsearch Requirements

You must be running Elasticsearch 5.0 or higher.

Installation

Composer

From the command line run:

$ composer require torann/laravel-hunt

Laravel

Once installed you need to register the service provider with the application. Open up config/app.php and find the providers key.

'providers' => [

    LaravelHunt\LaravelHuntServiceProvider::class,

]

Lumen

For Lumen register the service provider in bootstrap/app.php.

$app->register(LaravelHunt\LaravelHuntServiceProvider::class);

Publish the configurations

Run this on the command line from the root of your project:

$ php artisan vendor:publish --provider="LaravelHunt\LaravelHuntServiceProvider" --tag=config

A configuration file will be publish to config/hunt.php.

Indexes and Mapping

While you can definitely build your indexes and mapping through the Elasticsearch API, you can also use some helper methods to build indexes and types right from your models.

For custom analyzer, you can set an settings property in the config/hunt.php file:

[
    'settings' => [
         'number_of_shards' => 1,
         'analysis' => [
             'filter' => [
                 'autocomplete_filter' => [
                     'type' => 'edge_ngram',
                     'min_gram' => 1,
                     'max_gram' => 20,
                 ],
             ],
             'analyzer' => [
                 'autocomplete' => [
                     'type' => 'custom',
                     'tokenizer' => 'standard',
                     'filter' => [
                         'lowercase',
                         'autocomplete_filter',
                     ],
                 ],
             ],
         ],
     ],
]

For mapping, you can set a mappingProperties property in your model and use some mapping functions from there:

protected $mappingProperties = [
   'title' => [
        'type' => 'string',
        'analyzer' => 'standard'
    ]
];

Artisan Commands

hunt:install

Create the Elasticsearch index.

hunt:uninstall

Remove the Elasticsearch index.

hunt:map <model>

Initialize an Eloquent model map.

Arguments:

 model               Name or comma separated names of the model(s) to initialize

hunt:import <model>

Import all the entries in an Eloquent model. This will also initialize the model's map if one is not already set.

Arguments:

 model               Name or comma separated names of the model(s) to index

hunt:flush <model>

Flush all of the model's records from the index.

Arguments:

 model               Name or comma separated names of the model(s) to index

Indexing

Once you have added the LaravelHunt\Huntable trait to a model, all you need to do is save a model instance and it will automatically be added to your index.

$post = new App\Post;

// ...

$post->save();

Note: if the model record is already in your index, it will be updated. If it does not exist in the index, it will be added.

Updating Records

To update an index model, you only need to update the model instance's properties and `save`` the model to your database. Hunt will automatically persist the changes to your search index:

$post = App\Post::find(1);

// Update the post...

$post->save();

Removing Records

To remove a record from your index, simply delete the model from the database. This form of removal is even compatible with soft deleted models:

$post = App\Post::find(1);

$post->delete();

Searching

You may begin searching a model using the search method. The search method accepts a single string that will be used to search your models. You should then chain the get method onto the search query to retrieve the Eloquent models that match the given search query:

$posts = App\Post::search('Kitten fluff')->get();

Since Hunt searches return a collection of Eloquent models, you may even return the results directly from a route or controller and they will automatically be converted to JSON:

use Illuminate\Http\Request;

Route::get('/search', function (Request $request) {
    return App\Post::search($request->search)->get();
});

Pagination

In addition to retrieving a collection of models, you may paginate your search results using the paginate method. This method will return a Paginator instance just as if you had paginated a traditional Eloquent query:

$posts = App\Post::search('Kitten fluff')->paginate();

You may specify how many models to retrieve per page by passing the amount as the first argument to the paginate method:

$posts = App\Post::search('Kitten fluff')->paginate(15);

Once you have retrieved the results, you may display the results and render the page links using Blade just as if you had paginated a traditional Eloquent query:

<div class="container">
    @foreach ($posts as $post)
        {{ $post->title }}
    @endforeach
</div>

{{ $posts->links() }}

Multilingual

This feature is experimental

Laravel Hunt can support multiple languages by appending the language code to the index type, so when the system performs a search it will only look for data that is on in the current system locale suffixed index type. For this to work the model needs to use the LaravelHunt\Localized trait or something similar to it and model needs to have the filed locale.

For more information see the config file for more details.

torann/laravel-hunt 适用场景与选型建议

torann/laravel-hunt 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 375 次下载、GitHub Stars 达 4, 最近一次更新时间为 2016 年 11 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 4
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: BSD
  • 更新时间: 2016-11-09