archerzdip/hyperf-elasticsearch

Hyperf Scout 的 Elasticsearch 驱动程序

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

This package is auto-updated.

Last update: 2024-09-22 16:27:21 UTC


README

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

GitHub issues GitHub forks GitHub stars

需求

  • PHP版本 >=7.2.0
  • Hyperf框架版本 >2.0
  • Elasticsearch版本 >=7

安装

composer require archerzdip/hyperf-elasticsearch

配置

发布配置文件

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

索引配置器

所以配置器类用于设置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_'
                ]
            ]    
        ]
    ];
}

更多关于索引设置的详细信息,可以在Elasticsearch文档的索引管理部分找到。

可搜索模型

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

用法

基本搜索用法示例

// 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();

如果你只需要查询的匹配数量,使用count方法

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

如果你需要加载关系,使用with方法

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

除了标准功能外,该软件包还提供了在Elasticsearch中过滤数据而不指定查询字符串的可能性

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

你也可以覆盖模型的搜索规则

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

并使用多种 where 条件

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

并过滤掉得分低于min_score的结果

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

并添加更复杂的排序(如geo_distance)

$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;

最后,如果你想发送自定义请求,可以使用searchRaw方法

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

此查询将返回原始响应。

控制台命令

在命令行中运行php bin/hyperf.php help [command]以获取详细描述和所有可用选项。

搜索规则

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

默认目录为App\ElasticSearchRule\MySearchRule

许可

MIT