jeffleyd/esquery

使用类似 eloquent 的语法,利用 elastic search 服务。

v1.1.6 2022-06-24 14:38 UTC

This package is auto-updated.

Last update: 2024-09-24 19:24:11 UTC


README

此项目旨在与 eloquent 查询类似,便于在 ElasticSearch 的 Lucene 中进行搜索。

composer require jeffleyd/esquery

发布文件配置

php artisan vendor:publish --tag="esquery-provider"

访问配置文件夹并修改 esquery.php 文件的设置。

使用示例

首先为您的索引创建一个映射
有关映射类型的更多信息:https://elastic.ac.cn/guide/en/elasticsearch/reference/current/mapping-types.html
$build = EsQuery::index('my_index');
$response = $build->createIndex([
        'mappings' => [
            'properties' => [
                'parent_id' => [
                    'type' => 'long',
                ],
                'created_at' => [
                    'type' => 'date',
                    'format' => 'yyyy-MM-dd HH:mm:ss||yyyy-MM-dd'
                ]
            ]
        ]
    ]);
如果您想添加新的索引,可以这样做
创建一个迁移文件并扩展 EsScheme 类
    public function up()
    {
        EsScheme::create('bosses', function (EsMapping $table) {
            $table->integer('id');
            $table->string('name');
            $table->string('email');
            $table->nested('json');
            $table->timestamp('created_at');
            $table->timestamp('updated_at');
            
            // If you want to use a simple analyzer, you can add the following line.
            $table->setDefaultAnalyzer();

            // If you want to use a custom analyzer, you can use the following:
            $table->mapping['settings'] = [
                'analysis' => [
                    'default_analyze' => [
                        'type' => 'custom',
                        'tokenizer' => 'whitespace',
                        'char_filter' => [
                            'html_strip'
                        ],
                        'filter' => [
                            'lowercase'
                        ]
                    ]
                ]
            ];
        });
    }
    
    public function down()
    {
        EsScheme::dropIfExists('boss');
    }
现在您可以创建您的第一个文档
$build = EsQuery::index('my_index');
$response = $build->create([
        'parent_id' => 1,
        'created_at' => '2022-02-26 23:44:00',
    ]);
查找您的文档
$build = EsQuery::index('my_index');
$response = $build->where('parent_id', 1)->first(); // Example 1
$response = $build->where('parent_id', '=', 1)->first(); // Example 2
$response = $build->where('parent_id', 1)->get(); // Example 3
$response = $build->where('created_at', '>=' '2022-02-26'))->get(); // Example 4
执行聚合操作
$build = EsQuery::index('my_index');
$response = $build->where('parent_id', 1)->sum('price', 'total_price')->get(); // Use get() for aggregations
删除您的文档
$build = EsQuery::index('my_index');
$response = $build->where('parent_id', 1)->delete(); // Example 1 delete with conditions
$response = $build->delete(5); // Example 2 delete by ID
删除您的索引
$build = EsQuery::index('my_index');
$response = $build->deleteIndex(); 
如何关联关系
$build = EsQuery::index('my_index');
$response = $build->with('category', 'id', 'group_id')->get();

OR

$build = EsQuery::index('my_index');
$response = $build->with('category', 'id', 'group_id', function (QueryBuilder $query) {
    return $query->where('is_active', 1);
})->with('boss', 'id', 'boss_id', function (QueryBuilder $query) {
    return $query->where('is_active', 1);
})->get();



索引

[x] 创建
[x] 删除
[x] 更新映射
[x] 存在
[x] 跳过

文档

[x] 创建
[x] 创建多个
[x] 更新
[x] 通过 ID 删除
[x] 通过查询删除

类型搜索

[x] 第一个(有/无条件)
[x] 获取(有/无条件)
[x] 分页(有/无条件)
[x] 聚合 MAX / MIN / SUM / AVG / COUNT
[x] 限制
[x] 按组
[x] 按日期分组

条件

[x] where
[x] whereIn
[x] whereExists
[x] whereNotExists
[x] whereMissing
[x] between
[x] orderBy

附加

[x] with
[x] 在 get/first/paginate 后重置查询

ELASTIC SEARCH

网站:https://elastic.ac.cn/guide/en/elasticsearch/reference/current/index.html
版本:8.1