nailfor/elasticsearch

Eloquent风格的Elasticsearch库

v0.18.8 2024-07-19 14:29 UTC

README

Eloquent ORM的Elasticsearch客户端

安装

推荐通过composer安装此扩展。

运行以下命令之一

composer require nailfor/elasticsearch

或在您的应用程序的composer.json文件的require部分添加以下内容:

"nailfor/elasticsearch" : "*"

to the require section of your application's composer.json file.

使用方法

添加config/app.php

    'providers' => [
        ...
        nailfor\Elasticsearch\ElasticsearchServiceProvider::class,

和config/database.php

    'connections' => [
        ...
        'elasticsearch' => [ //the name of connection in your models(default)
            'driver'    => 'elasticearch',
            'config'    => [
                'BasicAuthentication' => [
                    env('ELASTICSEARCH_LOGIN', ''),
                    env('ELASTICSEARCH_PASSWORD', ''),
                ],
                'hosts'     => [
                    env('ELASTICSEARCH_HOST', 'localhost:9200'),
                ],
                'retries'   => 1,
            ],
        ],

示例模型

namespace App\Models\db\elastic;

use nailfor\Elasticsearch\Eloquent\Model;

class esSearch extends Model
{
    //protected $connection = 'elasticsearch'; //(default)

    //your index name
    protected $table='index';
}

示例选择

esSearch::where('field', 'somedata')
    ->whereBetween('@timestamp', [$startDate, $endDate])
    ->whereIn('data.field', [1,3,4])
    ->whereFiledExist('some.field')
    ->whereFiledNotExist('another.field')
    ->get();

!!!注意!!! 从v0.17.0版本开始,groups返回查询结果!

示例分组

esSearch::where('field.data', 'somedata')
    //group name "group" by field "data.field" without subgroups
    ->groupBy(['group'=>'data.field'])

    //group name "some_field" by field "some_field"
    ->groupBy('some_field')
    //subgroup name "another" by field "another" and subgroup name "diff" and field "diff.name"
    ->groupBy('some_field', ['another', 'diff'=>'field.diff'])

    //another style
    ->groupBy(['grp'=>'field'], ['subgrp'=>'sub.field'])

esSearch::where('field.data', 'somedata')
    //group name "group" by field "data.field" without subgroups with limit 10 items
    ->groupBy(['group' => 'data.field'])
    ->limit(10)

//supported closure
esSearch::groupBy(['aggregation_name' => 'field.data'], fn ($query) => $query
    ->groupBy('some_field', fn ($subQuery) => $subQuery
        ->groupBy(['agg3' => 'another.field'])
    )
)

范围聚合

    //group field "price" by 3 group: <1000, 1000-2000 and >2000
    ->groupByRange('price', ['ranges'=>[['to' => '1000'], ['from' => '1000', 'to' => 2000], ['from'=>2000]]])

日期聚合

    //group "group" by 2 dates: before NOW-1Day and after. This is NOT filter, this is group by condition!
    //all groups with name "group" will be merged
    ->groupByDateRange(['group'=>'date.field'], ['ranges'=>['to' => 'now-1y/d', 'from' => 'now-1y/d']])

间隔聚合

    //group "date.field" by interval "hour"
    ->groupByInterval(['graph'=>'date.field'], ['interval' => 'hour'])

平均值聚合

    //group and calculate average value
    ->groupBy(['groupName' => 'field_for_group'])
    ->groupByAverage('groupName', ['field' => 'field.name'])

最小/最大聚合

    ->groupByMin('groupName', ['field' => 'field.name'])
    ->groupByMax('groupName', ['field' => 'field.name'])

求和聚合

    //group and calculate sum
    ->groupBy(['groupName' => 'field_for_group'])
    ->groupBySum('groupName', ['field' => 'field.name'])

嵌套聚合

//simple group 'nested_field' same of 'nested_field'
esSearch::groupByNested('nested_field')

    //group name 'nested_group' by field 'nested.field'
    ->groupByNested(['nested_group' => 'nested.field']) 

    //By closures
    ->groupByNested(['nested_group' => 'nested.field'], fn ($query) => $query
        ->groupBy('some_field', fn ($subQuery) => $subQuery
            ->groupBy(['agg3' => 'another.field'])
        )
    )

统计聚合

esSearch::groupByStats('some_field')
    // or
    ->groupByStats(['group_name' => 'some_field'])

//You can combine them together
esSearch::groupByNested('nested_field', fn($query) => 
    $query->groupBy(['group' => 'field.id'], fn($query) => $query
        ->groupByStats(['values' => 'properties.value'])
    )
)

示例模糊度

$query = esSearch::query($searchString, [
    'fuzziness' => 1,
]);
$collection = $query->get();

示例滚动API

$query = esSearch::scroll([
        'scroll' => '1m',
    ]);
$collection = $query->get(); //first 10k(max) records
$collection = $query->get(); //next 10k(max) records...

esSearch::scroll([
        'scroll' => '1m',
    ])
    ->chunk(1000, function ($collection) {
        ...
    });

示例建议请求

//clear
esSeartch::where('model', 'short')
    ->suggest('my-suggest')
    ->get();

//closure
esSeartch::suggest('my-clossure', fn ($query) => $query->where('color', 'black'))
    ->get();

//mix
$query = esSeartch::select([
        'brand',
        'name',
    ])
    ->query($searchString, [
        'minimum_should_match'=> '50%',
        'fuzziness' => 'auto',
    ])
    ->suggest('my-1', fn ($query) => $query->where('size', 'xxl'))
    ->suggest('my-2', fn ($query) => $query->where('color', 'black'))
    ->suggest('my-3') //do nothing because there no "where" section
    ;
$collection = $query->get();

示例嵌套请求

$query = esSeartch::select([])
    ->where('category.id', 2)   //Attention!
    ->nested('category')        //Condition category.id = 2 will be removed from the main body
    ->where('price', 100)       //But this condition will be added to the main body

    //also u can combine simple and complicated nested requests
    ->nested('brand', fn ($subQuery) => $subQuery           //$subQuery doesn't consist any conditions from the body
        ->whereIn('brand.id', ['Nike', 'Sony', 'LG'])
        ->nested('color', fn($subSubQuery) => $subSubQuery  //Also $subSubQuery doesn't consist conditions
            ->whereIn('color.group', ['red', 'green'])      //Both of them doesn't affected the body condition 'price = 100'
        )
    )
    ->orNested('property', fn(subQuery) => $subQuery
        ->where('property.value', 'milked')
    )

示例批量插入

$records = [
    [
        '_id' => 1,  //This is field MUST be into recordset, otherwise will be set uniqid()
        'field1' => 'data1',
        'field2' => 'data2',
    ],
    [
        '_id' => 2,
        'field1' => 'some data1',
        'field2' => 'some data2',
    ],

];
esSeartch::insert($records);

示例post_filter

$query = esSearch::groupBy(['group' => 'field.id'])
    ->postFilter(fn($query) => $query
        ->where('field', 'value')
        ->nested('category')
    )

调试

$query = esSearch::query() ->dd(true) //默认为false ;

鸣谢

许可证

GNU许可证(GNU)。请参阅许可证文件获取更多信息。