crcms / elasticsearch by hiword

crcms repository extends laravel
8,264
103
2
Package Data
Maintainer Username: hiword
Maintainer Contact: simon@crcms.cn (simon)
Package Create Date: 2017-08-21
Package Last Update: 2020-12-26
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-11-23 03:01:05
Package Statistics
Total Downloads: 8,264
Monthly Downloads: 1
Daily Downloads: 0
Total Stars: 103
Total Watchers: 2
Total Forks: 15
Total Open Issues: 0

Crcms Elasticsearch

Latest Stable Version License StyleCI

Version Matrix

| Elasticsearch Version | crcms/elasticsearch Branch | | --------------------- | ------------------------ | | >= 6.0 | 1.* | | >= 5.0, < 6.0 | 0.* |

Install

You can install the package via composer:

composer require crcms/elasticsearch

Laravel

Modify config / app.php

'providers' => [
    CrCms\ElasticSearch\LaravelServiceProvider::class,
]

If you'd like to make configuration changes in the configuration file you can pubish it with the following Aritsan command:

php artisan vendor:publish --provider="CrCms\ElasticSearch\LaravelServiceProvider"

Quickstart

Create


Route::get('test/create',function(\CrCms\ElasticSearch\Builder $builder){
    $result = $builder->index('index')->type('type')->create([
		'key' => 'value',
    ]);
    dump($result);
});

Update


Route::get('test/create',function(\CrCms\ElasticSearch\Builder $builder){
    $result = $builder->index('index')->type('type')->update('id',[
		'key' => 'value2',
    ]);
    dump($result);
});

Delete


Route::get('test/create',function(\CrCms\ElasticSearch\Builder $builder){
    $result = $builder->index('index')->type('type')->delete('id');
    dump($result);
});

Select


Route::get('test/create',function(\CrCms\ElasticSearch\Builder $builder){
    $builder = $builder->index('index')->type('type');
	
	//SQL:select ... where id = 1 limit 1;
	$result = $builder->whereTerm('id',1)->first();
	
	//SQL:select ... where (key=1 or key=2) and key1=1
	$result = $builder->where(function (Builder $inQuery) {
		$inQuery->whereTerm('key',1)->orWhereTerm('key',2)
	})->whereTerm('key1',1)->get();
	
});

More

skip / take

$builder->take(10)->get(); // or limit(10)
$builder->offset(10)->take(10)->get(); // or skip(10)

term query

$builder->whereTerm('key',value)->first();

match query

$builder->whereMatch('key',value)->first();

range query

$builder->whereBetween('key',[value1,value2])->first();

where in query

$builder->whereIn('key',[value1,value2])->first();

logic query

$builder->whereTerm('key',value)->orWhereTerm('key2',value)->first();

nested query

$result = $builder->where(function (Builder $inQuery) {
    $inQuery->whereTerm('key',1)->orWhereTerm('key',2)
})->whereTerm('key1',1)->get();

Available conditions

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

Result Method

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

Log

//open log
$builder->enableQueryLog();

//all query log
dump($build->getQueryLog());

//last query log
dump($build->getLastQueryLog());

Elastisearch object

getElasticSearch() // or search()

License

MIT license