lingxiang / laravel-elasticsearch
使用SQL语句查询elasticsearch
1.0.5
2020-03-11 10:08 UTC
Requires
- php: >=7.0
- babenkoivan/scout-elasticsearch-driver: ^4.0
- elasticsearch/elasticsearch: ~7.0
- illuminate/support: ^5.3|^6.0
This package is not auto-updated.
Last update: 2024-10-03 07:55:08 UTC
README
版本对应
安装
您可以通过composer安装此包
composer require lingxiang/laravel-elasticsearch
Laravel
服务提供者,门面配置 config / app.php
'providers' => [
Lingxiang\Elasticsearch\LaravelServiceProvider::class
]
'aliases' => [
'Elasticsearch' => Lingxiang\Elasticsearch\LaravelServiceFacade::class
]
创建config配置search.php
php artisan vendor:publish --provider="Lingxiang\Elasticsearch\LaravelServiceProvider"
简单上手
创建
$result = \Elasticsearch::index('index')->type('type')->create([ 'key' => 'value', 'key2' => 'value2', ]);
更新
$result = \Elasticsearch::index('index')->type('type')->update('id',[ 'key' => 'value2', ]); dump($result);
删除
$result = \Elasticsearch::index('index')->type('type')->delete('id'); dump($result);
选择
//指定索引index和type $builder = \Elasticsearch::index('laisiou_test')->type('user'); //全查询,但因ES聚合查询引擎的原因只能返回10条 $result = $builder->get(); //骚操作全查询 $result = $builder->take($builder->count())->get(); //根据id查询一条 注:查询不到则返回 null $result = $builder->whereTerm('id',1)->first(); //子条件查询 $result = $builder->where(function ($inQuery) { $inQuery->whereTerm('key',1)->orWhereTerm('key',2) })->whereTerm('key1',1)->get();
更多查询
跳过 / 取消
$builder->take(10)->get(); // or limit(10) $builder->offset(10)->take(10)->get(); // or skip(10)
term查询
$builder->whereTerm('key',value)->first();
match查询
$builder->whereMatch('key',value)->first();
range查询
$builder->whereBetween('key',[value1,value2])->first();
where in查询
$builder->whereIn('key',[value1,value2])->first();
逻辑查询
$builder->whereTerm('key',value)->orWhereTerm('key2',value)->first();
嵌套查询
$result = $builder->where(function (Builder $inQuery) { $inQuery->whereTerm('key',1)->orWhereTerm('key',2) })->whereTerm('key1',1)->get();
所有可用条件方法
public function select($columns): self
public function where($column, $operator = null, $value = null, $leaf = 'term', $boolean = 'and'): self
public function orWhere($field, $operator = null, $value = null, $leaf = 'term'): self
public function whereMatch($field, $value, $boolean = 'and'): self
public function orWhereMatch($field, $value, $boolean = 'and'): self
public function whereTerm($field, $value, $boolean = 'and'): self
public function whereIn($field, array $value)
public function orWhereIn($field, array $value)
public function orWhereTerm($field, $value, $boolean = 'or'): self
public function whereRange($field, $operator = null, $value = null, $boolean = 'and'): self
public function orWhereRange($field, $operator = null, $value = null): self
public function whereBetween($field, array $values, $boolean = 'and'): self
public function orWhereBetween($field, array $values): self
public function orderBy(string $field, $sort): self
public function scroll(string $scroll): self
public function aggBy($field, $type): self
public function select($columns): self
获取结果的方法
public function get(): Collection
public function paginate(int $page, int $perPage = 15): Collection
public function first()
public function byId($id)
public function byIdOrFail($id): stdClass
public function chunk(callable $callback, $limit = 2000, $scroll = '10m')
public function create(array $data, $id = null, $key = 'id'): stdClass
public function update($id, array $data): bool
public function delete($id)
public function count(): int
日志
$builder->enableQueryLog();
Elastisearch对象
\Elasticsearch::getElasticSearch() // 或者 \Elasticsearch::search()