Skip to content

Commit 20b19bc

Browse files
authored
Merge pull request #57 from cxy1461274124/master
elasticsearch: fix bug and add methods
2 parents bd8473a + 125c625 commit 20b19bc

File tree

9 files changed

+176
-239
lines changed

9 files changed

+176
-239
lines changed

src/elasticsearch/src/Autoloader.php renamed to src/elasticsearch/src/AutoLoader.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
namespace Swoft\Elasticsearch;
44

5+
use Swoft\Elasticsearch\Connection\ConnectionManager;
56
use Swoft\Helper\ComposerJSON;
67
use Swoft\SwoftComponent;
78
use function dirname;
89

910
/**
1011
* class AutoLoader
12+
*
1113
* @since 2.0
1214
*/
1315
final class AutoLoader extends SwoftComponent
@@ -39,7 +41,7 @@ public function getPrefixDirs(): array
3941
*/
4042
public function metadata(): array
4143
{
42-
$jsonFile = dirname(__DIR__) . '/composer.json';
44+
$jsonFile = dirname(__DIR__).'/composer.json';
4345

4446
return ComposerJSON::open($jsonFile)->getMetadata();
4547
}

src/elasticsearch/src/Connection/Connection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* @since 2.0
2020
* @Bean(scope=Bean::PROTOTYPE)
2121
*
22-
* @package Jcsp\Elasticsearch\Connection
22+
* @package Swoft\Elasticsearch\Connection
2323
*/
2424
class Connection extends AbstractConnection
2525
{

src/elasticsearch/src/Connection/ConnectionInstance.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149
* @method callable|array tasksTasksList(array $params = [])
150150
* @method callable|array tasksCancel(array $params = [])
151151
*
152-
* @package Jcsp\Elasticsearch\Connection
152+
* @package Swoft\Elasticsearch\Connection
153153
*/
154154
class ConnectionInstance
155155
{

src/elasticsearch/src/Elasticsearch.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,10 @@ public static function connection(string $pool = Pool::DEFAULT_POOL): Connection
166166
{
167167
try {
168168
/** @var ConnectionManager $manager */
169-
$manager = BeanFactory::getBean(ConnectionManager::class);
169+
$manager = bean(ConnectionManager::class);
170170

171171
/** @var Pool $elasticsearchPool */
172-
$elasticsearchPool = BeanFactory::getBean($pool);
172+
$elasticsearchPool = bean($pool);
173173
/** @var Connection $connection */
174174
$connection = $elasticsearchPool->getConnection();
175175

@@ -200,5 +200,4 @@ public static function __callStatic($name, $arguments)
200200

201201
return $instance->$name(...$arguments);
202202
}
203-
204-
}
203+
}

src/elasticsearch/src/Eloquent/Builder.php

Lines changed: 121 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,27 @@
44

55
use Closure;
66
use Exception;
7+
use Jcsp\Core\Pagination\LengthAwarePaginator;
78
use Swoft\Elasticsearch\Elasticsearch;
89
use Swoft\Elasticsearch\Exception\ElasticsearchException;
10+
use Swoft\Elasticsearch\Pool;
11+
use Swoft\Stdlib\Helper\Arr;
912

1013
/**
1114
* Class Builder
1215
*
1316
* @since 2.0
1417
*
15-
* @package Jcsp\Elasticsearch\Eloquent
18+
* @package Swoft\Elasticsearch\Eloquent
1619
*/
1720
class Builder
1821
{
1922

23+
/**
24+
* @var string
25+
*/
26+
protected $pool = '';
27+
2028
/**
2129
* @var string
2230
*/
@@ -32,6 +40,11 @@ class Builder
3240
*/
3341
protected $model;
3442

43+
/**
44+
* @var array
45+
*/
46+
protected $select = [];
47+
3548
/**
3649
* @var array
3750
*/
@@ -42,6 +55,19 @@ class Builder
4255
*/
4356
private $queryIndices = [0, 0];
4457

58+
/**
59+
* @var array
60+
*/
61+
private $sort = [];
62+
63+
/**
64+
* @var array
65+
*/
66+
private $limit = [
67+
'from' => 0,
68+
'size' => 0,
69+
];
70+
4571
/**
4672
* @var array
4773
*/
@@ -81,15 +107,39 @@ public function __construct(string $modelName)
81107
$this->type = $model->getType();
82108
$this->createTimeField = $model->getCreateTimeField();
83109
$this->updateTimeField = $model->getUpdateTimeField();
110+
$this->pool = $model->getPool();
111+
$this->pool = !empty($this->pool) ? $this->pool : Pool::DEFAULT_POOL;
112+
84113
unset($model);
85114
}
86115

87-
//public function select(array $fields): Builder
88-
//{
89-
// $this->fields = $fields;
90-
//
91-
// return $this;
92-
//}
116+
/**
117+
* select
118+
*
119+
* @param array $fields
120+
*
121+
* @return Builder
122+
*/
123+
public function select(array $fields): Builder
124+
{
125+
$this->fields = $fields;
126+
127+
return $this;
128+
}
129+
130+
/**
131+
* setPool
132+
*
133+
* @param string $pool
134+
*
135+
* @return Builder
136+
*/
137+
public function setPool(string $pool = Pool::DEFAULT_POOL): Builder
138+
{
139+
$this->pool = $pool;
140+
141+
return $this;
142+
}
93143

94144
/**
95145
* index
@@ -325,17 +375,23 @@ public function whereRegex(string $field, string $value): Builder
325375
/**
326376
* get
327377
*
328-
* @return \Jcsp\Elasticsearch\Eloquent\Collection
378+
* @return \Swoft\Elasticsearch\Eloquent\Collection
379+
* @throws ElasticsearchException
329380
*/
330381
public function get(): Collection
331382
{
332-
$data = Elasticsearch::search([
383+
$this->limit['size'] = $this->limit['size'] > 0 ? $this->limit['size'] : $this->count();
384+
385+
$data = Elasticsearch::connection($this->pool)->search([
333386
'index' => $this->index,
334387
'type' => $this->type,
335388
'body' => [
336-
'query' => $this->parseJson($this->query),
337-
'sort' => [$this->createTimeField => ['order' => 'asc']],
389+
'_source' => $this->fields,
390+
'query' => $this->parseJson($this->query),
391+
'sort' => $this->sort,
338392
],
393+
'from' => $this->limit['from'],
394+
'size' => $this->limit['size'],
339395
]);
340396

341397
$result = [];
@@ -357,15 +413,17 @@ public function get(): Collection
357413
* first
358414
*
359415
* @return Model|null
416+
* @throws ElasticsearchException
360417
*/
361418
public function first(): ?Model
362419
{
363-
$data = Elasticsearch::search([
420+
$data = Elasticsearch::connection($this->pool)->search([
364421
'index' => $this->index,
365422
'type' => $this->type,
366423
'body' => [
367-
'query' => $this->parseJson($this->query),
368-
'sort' => [$this->createTimeField => ['order' => 'asc']],
424+
'_source' => $this->fields,
425+
'query' => $this->parseJson($this->query),
426+
'sort' => $this->sort,
369427
],
370428
'from' => 0,
371429
'size' => 1,
@@ -391,19 +449,21 @@ public function first(): ?Model
391449
* @param int $page
392450
* @param int $size
393451
*
394-
* @return Pagination
452+
* @return LengthAwarePaginator
453+
* @throws ElasticsearchException
395454
*/
396-
public function paginate(int $page = 1, int $size = 10): Pagination
455+
public function paginate(int $page = 1, int $size = 10): LengthAwarePaginator
397456
{
398457
$page = $page < 1 ? 1 : intval($page);
399458
$size = $size < 1 ? 1 : intval($size);
400459
$from = ($page - 1) * $size;
401-
$data = Elasticsearch::search([
460+
$data = Elasticsearch::connection($this->pool)->search([
402461
'index' => $this->index,
403462
'type' => $this->type,
404463
'body' => [
405-
'query' => $this->parseJson($this->query),
406-
'sort' => [$this->createTimeField => ['order' => 'asc']],
464+
'_source' => $this->fields,
465+
'query' => $this->parseJson($this->query),
466+
'sort' => $this->sort,
407467
],
408468
'from' => $from,
409469
'size' => $size,
@@ -421,7 +481,7 @@ public function paginate(int $page = 1, int $size = 10): Pagination
421481
array_push($list, $model);
422482
}
423483

424-
return Pagination::create($list, $total, $size, $page);
484+
return LengthAwarePaginator::create($list, $total, $size, $page);
425485
}
426486

427487
/**
@@ -432,7 +492,7 @@ public function paginate(int $page = 1, int $size = 10): Pagination
432492
public function count(): int
433493
{
434494
try {
435-
$data = Elasticsearch::count([
495+
$data = Elasticsearch::connection($this->pool)->count([
436496
'index' => $this->index,
437497
'type' => $this->type,
438498
'body' => [
@@ -447,18 +507,51 @@ public function count(): int
447507
return $count;
448508
}
449509

510+
/**
511+
* orderBy
512+
*
513+
* @param string $field
514+
* @param string $sort
515+
*
516+
* @return Builder
517+
*/
518+
public function orderBy(string $field, string $sort = 'asc'): Builder
519+
{
520+
$sort = in_array(strtolower($sort), ['asc', 'desc']) ? strtolower($sort) : 'asc';
521+
array_push($this->sort, [$field => ['order' => $sort]]);
522+
523+
return $this;
524+
}
525+
526+
/**
527+
* limit
528+
*
529+
* @param int $from
530+
* @param int $size
531+
*
532+
* @return Builder
533+
*/
534+
public function limit(int $from = 0, int $size = 10): Builder
535+
{
536+
$this->limit['from'] = $from > 0 ? $from : 0;
537+
$this->limit['size'] = $size > 0 ? $size : 10;
538+
539+
return $this;
540+
}
541+
450542
/**
451543
* create
452544
*
453545
* @param array $value
454546
*
455547
* @return Model
548+
* @throws ElasticsearchException
456549
*/
457550
public function create(array $value): Model
458551
{
459-
$value[$this->createTimeField] = $value[$this->updateTimeField] = time();
552+
$value[$this->createTimeField] = $value[$this->updateTimeField] = date('Y-m-d H:i:s');
460553

461-
$data = Elasticsearch::index([
554+
$data = Elasticsearch::connection($this->pool)->index([
462555
'index' => $this->index,
463556
'type' => $this->type,
464557
'body' => $value,
@@ -491,12 +584,12 @@ public function insert(array $values): Collection
491584
{
492585
$body = [];
493586
foreach ($values as $value) {
494-
$value[$this->createTimeField] = $value[$this->updateTimeField] = time();
587+
$value[$this->createTimeField] = $value[$this->updateTimeField] = date('Y-m-d H:i:s');
495588
array_push($body, ['index' => ['_index' => $this->index, '_type' => $this->type]]);
496589
array_push($body, $value);
497590
}
498591

499-
$data = Elasticsearch::bulk(['body' => $body]);
592+
$data = Elasticsearch::connection($this->pool)->bulk(['body' => $body]);
500593
if (!isset($data['errors']) || $data['errors'] !== false) {
501594
throw new ElasticsearchException('elasticsearch builder error: insert failed.');
502595
}
@@ -527,9 +620,9 @@ public function insert(array $values): Collection
527620
public function update(string $id, array $value): bool
528621
{
529622
try {
530-
$value[$this->updateTimeField] = time();
623+
$value[$this->updateTimeField] = date('Y-m-d H:i:s');
531624

532-
$data = Elasticsearch::update([
625+
$data = Elasticsearch::connection($this->pool)->update([
533626
'index' => $this->index,
534627
'type' => $this->type,
535628
'id' => $id,
@@ -558,7 +651,7 @@ public function update(string $id, array $value): bool
558651
public function delete(string $id): bool
559652
{
560653
try {
561-
$data = Elasticsearch::delete([
654+
$data = Elasticsearch::connection($this->pool)->delete([
562655
'index' => $this->index,
563656
'type' => $this->type,
564657
'id' => $id,

src/elasticsearch/src/Eloquent/Collection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* Class Collection
99
*
10-
* @package Jcsp\Elasticsearch\Eloquent
10+
* @package Swoft\Elasticsearch\Eloquent
1111
*/
1212
class Collection extends SwoftCollection
1313
{

0 commit comments

Comments
 (0)