Skip to content

Commit 6b133c5

Browse files
committed
Several upgrades and bug fixes
1 parent 8b8ccbc commit 6b133c5

File tree

10 files changed

+528
-412
lines changed

10 files changed

+528
-412
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"illuminate/container": "^7.0",
2424
"illuminate/database": "^7.0",
2525
"illuminate/events": "^7.0",
26-
"elasticsearch/elasticsearch": "8.7"
26+
"elasticsearch/elasticsearch": "8.10"
2727
},
2828
"require-dev": {
2929
},

src/Connection.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
namespace PDPhilip\Elasticsearch;
44

5+
use Elastic\Elasticsearch\Client;
56
use PDPhilip\Elasticsearch\DSL\Bridge;
67
use Elastic\Elasticsearch\ClientBuilder;
78
use Illuminate\Database\Connection as BaseConnection;
8-
use Illuminate\Support\Arr;
99
use Illuminate\Support\Str;
10-
use InvalidArgumentException;
1110
use RuntimeException;
1211

1312

@@ -38,18 +37,23 @@ public function __construct(array $config)
3837

3938
}
4039

41-
public function getIndexPrefix()
40+
public function getIndexPrefix(): string
4241
{
4342
return $this->indexPrefix;
4443
}
4544

45+
public function setIndexPrefix($newPrefix): void
46+
{
47+
$this->indexPrefix = $newPrefix;
48+
}
4649

47-
public function getTablePrefix()
50+
51+
public function getTablePrefix(): string
4852
{
4953
return $this->getIndexPrefix();
5054
}
5155

52-
public function setIndex($index)
56+
public function setIndex($index): string
5357
{
5458
$this->index = $index;
5559
if ($this->indexPrefix) {
@@ -66,7 +70,7 @@ public function getSchemaGrammar()
6670
return new Schema\Grammar($this);
6771
}
6872

69-
public function getIndex()
73+
public function getIndex(): string
7074
{
7175
return $this->index;
7276
}
@@ -76,12 +80,14 @@ public function setMaxSize($value)
7680
$this->maxSize = $value;
7781
}
7882

83+
7984
public function table($table, $as = null)
8085
{
81-
return $this->setIndex($table);
86+
$query = new Query\Builder($this, new Query\Processor());
87+
88+
return $query->from($table);
8289
}
8390

84-
8591
/**
8692
* @inheritdoc
8793
*/
@@ -103,7 +109,7 @@ public function disconnect()
103109
/**
104110
* @inheritdoc
105111
*/
106-
public function getDriverName()
112+
public function getDriverName(): string
107113
{
108114
return 'elasticsearch';
109115
}
@@ -137,7 +143,7 @@ protected function getDefaultSchemaGrammar()
137143
// Connection Builder
138144
//----------------------------------------------------------------------
139145

140-
protected function buildConnection()
146+
protected function buildConnection(): Client
141147
{
142148
$type = config('database.connections.elasticsearch.auth_type') ?? null;
143149
$type = strtolower($type);
@@ -149,7 +155,7 @@ protected function buildConnection()
149155

150156
}
151157

152-
protected function _httpConnection()
158+
protected function _httpConnection(): Client
153159
{
154160
$hosts = config('database.connections.elasticsearch.hosts') ?? null;
155161
$username = config('database.connections.elasticsearch.username') ?? null;
@@ -166,7 +172,7 @@ protected function _httpConnection()
166172
return $cb->build();
167173
}
168174

169-
protected function _cloudConnection()
175+
protected function _cloudConnection(): Client
170176
{
171177
$cloudId = config('database.connections.elasticsearch.cloud_id') ?? null;
172178
$username = config('database.connections.elasticsearch.username') ?? null;
@@ -194,6 +200,10 @@ protected function _cloudConnection()
194200

195201
public function __call($method, $parameters)
196202
{
203+
if (!$this->index) {
204+
$this->index = $this->indexPrefix.'*';
205+
}
206+
197207
$bridge = new Bridge($this->client, $this->index, $this->maxSize);
198208

199209
return $bridge->{'process'.Str::studly($method)}(...$parameters);

src/DSL/Bridge.php

Lines changed: 72 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace PDPhilip\Elasticsearch\DSL;
44

5+
use Elastic\Elasticsearch\Exception\ClientResponseException;
6+
use Elastic\Elasticsearch\Exception\MissingParameterException;
7+
use Elastic\Elasticsearch\Exception\ServerResponseException;
58
use Exception;
69
use Elastic\Elasticsearch\Client;
710

@@ -87,6 +90,7 @@ public function processIndicesDsl($method, $params)
8790
*/
8891
public function processFind($wheres, $options, $columns)
8992
{
93+
9094
$params = $this->buildParams($this->index, $wheres, $options, $columns);
9195

9296
return $this->_returnSearch($params, __FUNCTION__);
@@ -97,9 +101,8 @@ public function processFind($wheres, $options, $columns)
97101
*/
98102
public function processSearch($searchParams, $searchOptions, $wheres, $opts, $fields, $cols)
99103
{
104+
100105
$params = $this->buildSearchParams($this->index, $searchParams, $searchOptions, $wheres, $opts, $fields, $cols);
101-
102-
// dd($params);
103106

104107
return $this->_returnSearch($params, __FUNCTION__);
105108

@@ -114,10 +117,11 @@ protected function _returnSearch($params, $source)
114117
$process = $this->client->search($params);
115118

116119
return $this->_sanitizeSearchResponse($process, $params, $this->_queryTag($source));
120+
117121
} catch (Exception $e) {
118122

119-
$error = $this->_returnError($e->getMessage(), $e->getCode(), $params, $this->_queryTag(__FUNCTION__));
120-
throw new Exception($error->errorMessage);
123+
$result = $this->_returnError($e->getMessage(), $e->getCode(), $params, $this->_queryTag(__FUNCTION__));
124+
throw new Exception($result->errorMessage);
121125
}
122126
}
123127

@@ -147,8 +151,8 @@ public function processDistinct($column, $wheres)
147151
return $this->_return($data, $process, $params, $this->_queryTag(__FUNCTION__));
148152
} catch (Exception $e) {
149153

150-
$error = $this->_returnError($e->getMessage(), $e->getCode(), [], $this->_queryTag(__FUNCTION__));
151-
throw new Exception($error->errorMessage);
154+
$result = $this->_returnError($e->getMessage(), $e->getCode(), [], $this->_queryTag(__FUNCTION__));
155+
throw new Exception($result->errorMessage);
152156
}
153157

154158

@@ -190,15 +194,15 @@ public function processSave($data, $refresh)
190194
if ($refresh) {
191195
$params['refresh'] = $refresh;
192196
}
197+
193198
try {
194199
$response = $this->client->index($params);
195-
196200
$savedData = ['_id' => $response['_id']] + $data;
197201

198202
return $this->_return($savedData, $response, $params, $this->_queryTag(__FUNCTION__));
199203
} catch (Exception $e) {
200-
$error = $this->_returnError($e->getMessage(), $e->getCode(), $params, $this->_queryTag(__FUNCTION__));
201-
throw new Exception($error->errorMessage);
204+
$result = $this->_returnError($e->getMessage(), $e->getCode(), $params, $this->_queryTag(__FUNCTION__));
205+
throw new Exception($result->errorMessage);
202206
}
203207

204208

@@ -346,18 +350,34 @@ public function processScript($id, $script)
346350
// Index administration
347351
//----------------------------------------------------------------------
348352

349-
public function processGetIndices($all)
353+
/**
354+
* @throws ClientResponseException
355+
* @throws ServerResponseException
356+
* @throws MissingParameterException
357+
*/
358+
public function processGetIndices($all): array
350359
{
351-
$response = $this->client->cat()->indices();
360+
$index = $this->index;
361+
if ($all) {
362+
$index = '*';
363+
}
364+
$response = $this->client->indices()->get(['index' => $index]);
352365

353-
return $this->catIndices($response, $all);
366+
return $response->asArray();
354367
}
355368

356-
public function processIndexExists($index)
369+
public function processIndexExists($index): bool
357370
{
358371
$params = ['index' => $index];
359372

360-
return $this->client->indices()->exists($params);
373+
try {
374+
$test = $this->client->indices()->exists($params);
375+
376+
return $test->getStatusCode() == 200;
377+
} catch (Exception $e) {
378+
return false;
379+
}
380+
361381
}
362382

363383
/**
@@ -385,11 +405,10 @@ public function processIndexSettings($index)
385405
{
386406
$params = ['index' => $index];
387407
try {
388-
$responseObject = $this->client->indices()->getSettings($params);
389-
$response = $responseObject->asArray();
408+
$response = $this->client->indices()->getSettings($params);
390409
$result = $this->_return($response, $response, $params, $this->_queryTag(__FUNCTION__));
391410

392-
return $result->data;
411+
return $result->data->asArray();
393412
} catch (Exception $e) {
394413
$result = $this->_returnError($e->getMessage(), $e->getCode(), $params, $this->_queryTag(__FUNCTION__));
395414
throw new Exception($result->errorMessage);
@@ -401,15 +420,15 @@ public function processIndexSettings($index)
401420
*/
402421
public function processIndexCreate($settings)
403422
{
423+
$params = $this->buildIndexMap($this->index, $settings);
404424
try {
405-
$params = $this->buildIndexMap($this->index, $settings);
406425
$response = $this->client->indices()->create($params);
407426

408427
$result = $this->_return(true, $response, $params, $this->_queryTag(__FUNCTION__));
409428

410429
return true;
411430
} catch (Exception $e) {
412-
$result = $this->_returnError($e->getMessage(), $e->getCode(), $params, $this->_queryTag(__FUNCTION__));
431+
$result = $this->_returnError($e->getMessage(), $e->getCode(), [], $this->_queryTag(__FUNCTION__));
413432
throw new Exception($result->errorMessage);
414433
}
415434

@@ -421,7 +440,6 @@ public function processIndexCreate($settings)
421440
public function processIndexDelete()
422441
{
423442
$params = ['index' => $this->index];
424-
425443
try {
426444
$response = $this->client->indices()->delete($params);
427445
$this->_return(true, $response, $params, $this->_queryTag(__FUNCTION__));
@@ -462,15 +480,32 @@ public function processIndexModify($settings)
462480
/**
463481
* @throws Exception
464482
*/
465-
public function processReIndex($newIndex, $oldIndex)
483+
public function processReIndex($oldIndex, $newIndex)
466484
{
467-
$params['source']['index'] = $oldIndex;
468-
$params['dest']['index'] = $newIndex;
485+
$prefix = str_replace('*', '', $this->index);
486+
if ($prefix) {
487+
$oldIndex = $prefix.'_'.$oldIndex;
488+
$newIndex = $prefix.'_'.$newIndex;
489+
}
490+
$params['body']['source']['index'] = $oldIndex;
491+
$params['body']['dest']['index'] = $newIndex;
469492
try {
470493
$response = $this->client->reindex($params);
471-
$result = $this->_return(true, $response, $params, $this->_queryTag(__FUNCTION__));
494+
$result = $response->asArray();
495+
$resultData = [
496+
'took' => $result['took'],
497+
'total' => $result['total'],
498+
'created' => $result['created'],
499+
'updated' => $result['updated'],
500+
'deleted' => $result['deleted'],
501+
'batches' => $result['batches'],
502+
'version_conflicts' => $result['version_conflicts'],
503+
'noops' => $result['noops'],
504+
'retries' => $result['retries'],
505+
];
506+
507+
return $this->_return($resultData, $result, $params, $this->_queryTag(__FUNCTION__));
472508

473-
return true;
474509
} catch (Exception $e) {
475510
$result = $this->_returnError($e->getMessage(), $e->getCode(), $params, $this->_queryTag(__FUNCTION__));
476511
throw new Exception($result->errorMessage);
@@ -525,8 +560,8 @@ public function _countAggregate($wheres, $options, $columns)
525560
return $this->_return($process['count'] ?? 0, $process, $params, $this->_queryTag(__FUNCTION__));
526561
} catch (Exception $e) {
527562

528-
$error = $this->_returnError($e->getMessage(), $e->getCode(), [], $this->_queryTag(__FUNCTION__));
529-
throw new Exception($error->errorMessage);
563+
$result = $this->_returnError($e->getMessage(), $e->getCode(), [], $this->_queryTag(__FUNCTION__));
564+
throw new Exception($result->errorMessage);
530565
}
531566

532567
}
@@ -541,8 +576,8 @@ private function _maxAggregate($wheres, $options, $columns)
541576
return $this->_return($process['aggregations']['max_value']['value'] ?? 0, $process, $params, $this->_queryTag(__FUNCTION__));
542577
} catch (Exception $e) {
543578

544-
$error = $this->_returnError($e->getMessage(), $e->getCode(), [], $this->_queryTag(__FUNCTION__));
545-
throw new Exception($error->errorMessage);
579+
$result = $this->_returnError($e->getMessage(), $e->getCode(), [], $this->_queryTag(__FUNCTION__));
580+
throw new Exception($result->errorMessage);
546581
}
547582
}
548583

@@ -555,8 +590,8 @@ private function _minAggregate($wheres, $options, $columns)
555590

556591
return $this->_return($process['aggregations']['min_value']['value'] ?? 0, $process, $params, $this->_queryTag(__FUNCTION__));
557592
} catch (Exception $e) {
558-
$error = $this->_returnError($e->getMessage(), $e->getCode(), [], $this->_queryTag(__FUNCTION__));
559-
throw new Exception($error->errorMessage);
593+
$result = $this->_returnError($e->getMessage(), $e->getCode(), [], $this->_queryTag(__FUNCTION__));
594+
throw new Exception($result->errorMessage);
560595
}
561596
}
562597

@@ -571,8 +606,8 @@ private function _sumAggregate($wheres, $options, $columns)
571606
return $this->_return($process['aggregations']['sum_value']['value'] ?? 0, $process, $params, $this->_queryTag(__FUNCTION__));
572607
} catch (Exception $e) {
573608

574-
$error = $this->_returnError($e->getMessage(), $e->getCode(), [], $this->_queryTag(__FUNCTION__));
575-
throw new Exception($error->errorMessage);
609+
$result = $this->_returnError($e->getMessage(), $e->getCode(), [], $this->_queryTag(__FUNCTION__));
610+
throw new Exception($result->errorMessage);
576611
}
577612

578613
}
@@ -586,8 +621,8 @@ private function _avgAggregate($wheres, $options, $columns)
586621

587622
return $this->_return($process['aggregations']['avg_value']['value'] ?? 0, $process, $params, $this->_queryTag(__FUNCTION__));
588623
} catch (Exception $e) {
589-
$error = $this->_returnError($e->getMessage(), $e->getCode(), [], $this->_queryTag(__FUNCTION__));
590-
throw new Exception($error->errorMessage);
624+
$result = $this->_returnError($e->getMessage(), $e->getCode(), [], $this->_queryTag(__FUNCTION__));
625+
throw new Exception($result->errorMessage);
591626
}
592627
}
593628

@@ -600,8 +635,8 @@ private function _matrixAggregate($wheres, $options, $columns)
600635

601636
return $this->_return($process['aggregations']['statistics'] ?? [], $process, $params, $this->_queryTag(__FUNCTION__));
602637
} catch (Exception $e) {
603-
$error = $this->_returnError($e->getMessage(), $e->getCode(), [], $this->_queryTag(__FUNCTION__));
604-
throw new Exception($error->errorMessage);
638+
$result = $this->_returnError($e->getMessage(), $e->getCode(), [], $this->_queryTag(__FUNCTION__));
639+
throw new Exception($result->errorMessage);
605640
}
606641

607642
}
@@ -651,7 +686,6 @@ private function _return($data, $meta, $params, $queryTag)
651686
return $results;
652687
}
653688

654-
655689
private function _returnError($errorMsg, $errorCode, $params, $queryTag)
656690
{
657691
$error = new Results([], [], $params, $queryTag);

0 commit comments

Comments
 (0)