archerzdip/hyperf-elasticsearch

The Elasticsearch Driver for Hyperf Scout

v2.0.1 2021-06-22 09:23 UTC

This package is auto-updated.

Last update: 2024-10-22 16:43:48 UTC


README

根据 babenkoivan/scout-elasticsearch-driver 改造的基于Hyperf框架的ElasticSearch组件。

GitHub issues GitHub forks GitHub stars

Requirements

  • PHP version >=7.2.0
  • Hyperf Framework version >2.0
  • Elasticsearch version >=7

Installation

composer require archerzdip/hyperf-elasticsearch

Configuration

发布配置文件

php bin/hyperf.php vendor:publish archerzdip/hyperf-elasticsearch
  • 配置文件在config/autoload/scout_elastic.php, 配置参数如下: *

Index Configurator

所以配置器类用于设置ElasticSearch的索引,可使用以下方式创建新的索引配置器:

php bin/hyperf.php make:index-configurator MyIndexConfigurator

默认目录为App\ElasticIndexConfigurator\MyIndexConfigurator

<?php

namespace App\ElasticIndexConfigurator;

use ArcherZdip\ScoutElastic\IndexConfigurator;
use ArcherZdip\ScoutElastic\Traits\Migratable;

class MyIndexConfigurator extends IndexConfigurator
{
    use Migratable;

    protected $name = 'my_index';

    /**
     * @var array
     */
    protected $settings = [
        'analysis' => [
            'analyzer' => [
                'es_std' => [
                    'type' => 'standard',
                    'stopwords' => '_spanish_'
                ]
            ]    
        ]
    ];
}

More about index settings you can find in the index management section of Elasticsearch documentation.

Searchable Model

php bin/hyperf.php make:searchable-model MyModel

Usage

Basic search usage example:

// set query string
App\MyModel::search('phone')
    // specify columns to select
    ->select(['title', 'price'])
    // filter 
    ->where('color', 'red')
    // sort
    ->orderBy('price', 'asc')
    // collapse by field
    ->collapse('brand')
    // set offset
    ->from(0)
    // set limit
    ->take(10)
    // get results
    ->get();

If you only need the number of matches for a query, use the count method:

App\MyModel::search('phone') 
    ->count();

If you need to load relations, use the with method:

App\MyModel::search('phone') 
    ->with('makers')
    ->get();

In addition to standard functionality the package offers you the possibility to filter data in Elasticsearch without specifying a query string:

App\MyModel::search('*')
    ->where('id', 1)
    ->get();

Also you can override model search rules:

App\MyModel::search('Brazil')
    ->rule(App\MySearchRule::class)
    ->get();

And use variety of where conditions:

App\MyModel::search('*')
    ->whereRegexp('name.raw', 'A.+')
    ->where('age', '>=', 30)
    ->whereExists('unemployed')
    ->get();

And filter out results with a score less than min_score:

App\MyModel::search('sales')
    ->minScore(1.0)
    ->get();

And add more complex sorting (geo_distance eg.)

$model = App\MyModel::search('sales')
    ->orderRaw([
       '_geo_distance' =>  [
           'coordinates' => [
               'lat'   =>  51.507351,
               'lon'   =>  -0.127758
           ],
           'order'     =>  'asc',
           'unit'      =>  'm'
       ]
    ])
    ->get();

// To retrieve sort result, use model `sortPayload` attribute:
$model->sortPayload;

At last, if you want to send a custom request, you can use the searchRaw method:

App\MyModel::searchRaw([
    'query' => [
        'bool' => [
            'must' => [
                'match' => [
                    '_all' => 'Brazil'
                ]
            ]
        ]
    ]
]);
## 直接返回ES数据
App\MyModel::search("test")->raw();

This query will return raw response.

Console Commands

For detailed description and all available options run php bin/hyperf.php help [command] in the command line.

Search rules

php bin/hyperf.php make:search-rule MySearchRule

默认目录为App\ElasticSearchRule\MySearchRule

License

MIT