项目作者: crcms

项目描述 :
Use SQL statements to query elasticsearch
高级语言: PHP
项目地址: git://github.com/crcms/elasticsearch.git
创建时间: 2017-08-21T07:57:30Z
项目社区:https://github.com/crcms/elasticsearch

开源协议:MIT License

下载


Crcms Elasticsearch

Latest Stable Version
License
StyleCI

Version Matrix

Elasticsearch Version crcms/elasticsearch Branch
>= 7.0 master(beta unstable)
>= 6.0 1.*
>= 5.0, < 6.0 0.*

Install

You can install the package via composer:

  1. composer require crcms/elasticsearch

Please install if you want to use the latest version dev-master

Use

Non-Laravel framework

  1. // select config path
  2. $config = require 'search.php';
  3. $builder = Factory::builder($config);

Laravel

Modify config / app.php If the version is less <= 5.5

  1. 'providers' => [
  2. CrCms\ElasticSearch\LaravelServiceProvider::class,
  3. ]

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

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

Quickstart

Create

  1. $builder->index('index')->type('type')->create([
  2. 'key' => 'value'
  3. ]);
  4. // return a collection
  5. $builder->index('index')->type('type')->createCollection([
  6. 'key' => 'value'
  7. ]);

Update

  1. $builder->index('index')->type('type')->update([
  2. 'key' => 'value1'
  3. ]);

Delete

  1. $builder->index('index')->type('type')->delete($result->_id);

Select

  1. $builder = $builder->index('index')->type('type');
  2. //SQL:select ... where id = 1 limit 1;
  3. $result = $builder->whereTerm('id',1)->first();
  4. //SQL:select ... where (key=1 or key=2) and key1=1
  5. $result = $builder->where(function (Query $inQuery) {
  6. $inQuery->whereTerm('key',1)->orWhereTerm('key',2)
  7. })->whereTerm('key1',1)->get();

More

skip / take

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

term query

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

match query

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

range query

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

where in query

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

logic query

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

nested query

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

Available conditions

  1. public function select($columns): self
  1. public function where($column, $operator = null, $value = null, $leaf = 'term', $boolean = 'and'): self
  1. public function orWhere($field, $operator = null, $value = null, $leaf = 'term'): self
  1. public function whereMatch($field, $value, $boolean = 'and'): self
  1. public function orWhereMatch($field, $value, $boolean = 'and'): self
  1. public function whereTerm($field, $value, $boolean = 'and'): self
  1. public function whereIn($field, array $value)
  1. public function orWhereIn($field, array $value)
  1. public function orWhereTerm($field, $value, $boolean = 'or'): self
  1. public function whereRange($field, $operator = null, $value = null, $boolean = 'and'): self
  1. public function orWhereRange($field, $operator = null, $value = null): self
  1. public function whereBetween($field, array $values, $boolean = 'and'): self
  1. public function whereNotBetween($field, array $values): self
  1. public function orWhereNotBetween(string $field, array $values): self
  1. public function whereExists($field, $boolean = 'and'): self
  1. public function whereNotExists($field, $boolean = 'and'): self
  1. public function orWhereBetween($field, array $values): self
  1. public function orderBy(string $field, $sort): self
  1. public function scroll(string $scroll): self
  1. public function aggBy($field, $type): self
  1. public function select($columns): self

Result Method

  1. public function get(): Collection
  1. public function paginate(int $page, int $perPage = 15): Collection
  1. public function first()
  1. public function byId($id)
  1. public function byIdOrFail($id): stdClass
  1. public function chunk(callable $callback, $limit = 2000, $scroll = '10m')
  1. public function create(array $data, $id = null, $key = 'id'): stdClass
  1. public function update($id, array $data): bool
  1. public function delete($id)
  1. public function count(): int

Log

  1. //open log
  2. $builder->enableQueryLog();
  3. //all query log
  4. dump($build->getQueryLog());
  5. //last query log
  6. dump($build->getLastQueryLog());

Elastisearch object

  1. getElasticSearch() // or search()

If you want to expand more, you can use this method, call https://github.com/elastic/elasticsearch-php

Other

For more examples, please see test cases
https://github.com/crcms/elasticsearch/blob/master/tests/BuildTest.php

License

MIT license