esmp/elastic

Webman plugin esmp/elastic

v1.0.0 2025-05-23 08:35 UTC

This package is auto-updated.

Last update: 2025-05-23 08:41:09 UTC


README

默认使用illuminate/database 有其他需求请自行fork后修改 elasticSearch 请自行安装

安装

 composer require esmp/elastic

示例

<?php

namespace app\model;

use Es\Elasticsearch\Exception\ElasticSearchException;
use support\Model;
use Es\Elasticsearch\Support\ElasticSearchSync;

class Test extends Model
{
    // 引入elasticsearch同步trait   注意:需要在boot方法中调用 bootElasticSearchSync方法
    use ElasticSearchSync;

    /**
     * !!!!!!!!!!!!!!!!!! 
     * 索引名称 
     * @var string
     */
    protected static string $elasticIndex = 'test';
    
    /**
     *!!!!!!!!!!!!!!!!!!
     * 字段必须要存在于模型中
     * 搜索字段 Elastic 文档对应的字段,id字段必须存在 作为索引的唯一标识
     * @var array
     */
    protected static array $searchable = [
        'id',
        'name'
    ];
    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'test';
    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * @return void
     * @throws ElasticSearchException
     */
    protected static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub
        //!!!!!!!     注意:需要在boot方法中调用 bootElasticSearchSync方法 根据模型事件进行文档同步
        static::bootElasticSearchSync();
    }

}   

配置文件 /your_project/config/esmp/elastic.php

return [
    'elastic_host' => '', //配置的elasticsearch地址
    'elastic_username' => '',//配置的elasticsearch用户名
    'elastic_password' => ''//配置的elasticsearch密码
    ]

命令

php webman  esmp-elastic:index "app\Model\Test" //创建索引
php webman  esmp-elastic:flush "app\Model\Test" //清空索引及文档

搜索示例

        <?php

namespace app\controller;

use app\model\Test;
use Es\Elasticsearch\ElasticSearchOperations;
use support\Request;

class TestController
{
    /**
     * @param Request $request
     * @return array
     */
    public function search(Request $request): array
    {
      $client = new ElasticSearchOperations();
        $client = $client->getClient();
        $res = $client->search([
            'index' => 'test', //索引名称
            'body' => [
                'query' => [
                    'match' => [
                        'name' => 'ec' //搜索字段
                    ]
                ]
            ]
        ]);

        dd($res->getBody()->getContents());
    }
}
  $params = match ($type) {
            0 => [
                'index' => explode(',', getenv('ELASTIC_INDEX')),
                'body' => [
                    'query' => [
                        'bool' => [
                            'must' => [
                                [
                                    'multi_match' => [
                                        'query' => $keyword,
                                        'fields' => [
                                            "nick_name^2",
                                            "content",
                                            "goods_title",
                                            "keywords",
                                            "comment"
                                        ]
                                    ]
                                ]
                            ],
                            'filter' => [
                                'bool' => [
                                    'should' => [
                                        [
                                            'bool' => [
                                                'must' => [
                                                    [
                                                        'term' => [
                                                            '_index' => 'circles'
                                                        ]
                                                    ], [
                                                        'term' => [
                                                            'status' => 1
                                                        ]
                                                    ]
                                                ]
                                            ]
                                        ],
                                        [
                                            'bool' => [
                                                'must' => [
                                                    [
                                                        'term' => [
                                                            '_index' => 'docs'
                                                        ]
                                                    ],
                                                    [
                                                        'term' => [
                                                            'status' => 1,
                                                        ]
                                                    ]
                                                ] 
                                            ]
                                        ],
                                        [
                                            'bool' => [
                                                'must' => [
                                                    [
                                                        'term' => [
                                                            '_index' => 'users'
                                                        ]
                                                    ],
                                                    [
                                                        'term' => [
                                                            'status' => 0
                                                        ]
                                                    ]
                                                ]
                                            ]
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ],
                    'size' => 10,
                    'from' => 0
                ]
            ],
            1 => [
                'index' => 'posts_docs',
                'body' => [
                    'query' => [
                        'match' => [
                            'goods_title' => $keyword
                        ]
                    ],
                    'size' => 10,
                    'from' => 0
                ]
            ],
            2 => [
                'index' => 'posts_circles',
                'body' => [
                    'query' => [
                        'match' => [
                            'comment' => $keyword
                        ]
                    ],
                    'size' => 10,
                    'from' => 0
                ]
            ],
            default => [
                'index' => 'posts_users',
                'body' => [
                    'query' => [
                        'match' => [
                            'nick_name' => $keyword
                        ]
                    ],
                    'size' => 10,
                    'from' => 0
                ]
            ],
        };

        $result = $client->search($params);
        $docs = $result->asArray()['hits']['hits'];

其他搜索请参考elasticSearch