Skip to content

Commit dbbae32

Browse files
committed
Several upgrades and bug fixes
1 parent 32cba90 commit dbbae32

File tree

12 files changed

+626
-504
lines changed

12 files changed

+626
-504
lines changed

composer.json

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,53 @@
11
{
2-
"name": "pdphilip/elasticsearch",
3-
"description": "An Elasticsearch implementation of Laravel's Eloquent ORM",
4-
"keywords": [
5-
"laravel",
6-
"eloquent",
7-
"elasticsearch",
8-
"elastic",
9-
"database",
10-
"model"
11-
],
12-
"homepage": "https://github.com/pdphilip/laravel-elasticsearch",
13-
"authors": [
14-
{
15-
"name": "David Philip",
16-
"email": "[email protected]",
17-
"homepage": "https://github.com/pdphilip"
18-
}
19-
],
20-
"license": "MIT",
21-
"require": {
22-
"illuminate/support": "^9.0",
23-
"illuminate/container": "^9.0",
24-
"illuminate/database": "^9.0",
25-
"illuminate/events": "^9.0",
26-
"elasticsearch/elasticsearch": "8.7"
27-
},
28-
"require-dev": {
29-
"phpunit/phpunit": "^9.5.8",
30-
"orchestra/testbench": "^7.19",
31-
"mockery/mockery": "^1.3.1",
32-
"doctrine/dbal": "^2.13.3|^3.1.4"
33-
},
34-
"autoload-dev": {
35-
"classmap": [
36-
"tests/TestCase.php",
37-
"tests/models",
38-
"tests/seeds"
39-
]
40-
},
41-
"autoload": {
42-
"psr-4": {
43-
"PDPhilip\\Elasticsearch\\": "src/"
44-
}
45-
},
46-
"extra": {
47-
"laravel": {
48-
"providers": [
49-
"PDPhilip\\Elasticsearch\\ElasticServiceProvider"
50-
]
51-
}
2+
"name": "pdphilip/elasticsearch",
3+
"description": "An Elasticsearch implementation of Laravel's Eloquent ORM",
4+
"keywords": [
5+
"laravel",
6+
"eloquent",
7+
"elasticsearch",
8+
"elastic",
9+
"database",
10+
"model"
11+
],
12+
"homepage": "https://github.com/pdphilip/laravel-elasticsearch",
13+
"authors": [
14+
{
15+
"name": "David Philip",
16+
"email": "[email protected]",
17+
"homepage": "https://github.com/pdphilip"
5218
}
19+
],
20+
"license": "MIT",
21+
"require": {
22+
"illuminate/support": "^9.0",
23+
"illuminate/container": "^9.0",
24+
"illuminate/database": "^9.0",
25+
"illuminate/events": "^9.0",
26+
"elasticsearch/elasticsearch": "8.10"
27+
},
28+
"require-dev": {
29+
"phpunit/phpunit": "^9.5.8",
30+
"orchestra/testbench": "^7.19",
31+
"mockery/mockery": "^1.3.1",
32+
"doctrine/dbal": "^2.13.3|^3.1.4"
33+
},
34+
"autoload-dev": {
35+
"classmap": [
36+
"tests/TestCase.php",
37+
"tests/models",
38+
"tests/seeds"
39+
]
40+
},
41+
"autoload": {
42+
"psr-4": {
43+
"PDPhilip\\Elasticsearch\\": "src/"
44+
}
45+
},
46+
"extra": {
47+
"laravel": {
48+
"providers": [
49+
"PDPhilip\\Elasticsearch\\ElasticServiceProvider"
50+
]
51+
}
52+
}
5353
}

src/Connection.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
use PDPhilip\Elasticsearch\DSL\Bridge;
77
use Elastic\Elasticsearch\ClientBuilder;
88
use Illuminate\Database\Connection as BaseConnection;
9-
use Illuminate\Support\Arr;
109
use Illuminate\Support\Str;
11-
use InvalidArgumentException;
1210
use RuntimeException;
1311

1412

@@ -44,6 +42,11 @@ public function getIndexPrefix(): string
4442
return $this->indexPrefix;
4543
}
4644

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

4851
public function getTablePrefix(): string
4952
{
@@ -77,12 +80,14 @@ public function setMaxSize($value)
7780
$this->maxSize = $value;
7881
}
7982

83+
8084
public function table($table, $as = null)
8185
{
82-
return $this->setIndex($table);
86+
$query = new Query\Builder($this, new Query\Processor());
87+
88+
return $query->from($table);
8389
}
8490

85-
8691
/**
8792
* @inheritdoc
8893
*/
@@ -195,6 +200,10 @@ protected function _cloudConnection(): Client
195200

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

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

src/DSL/Bridge.php

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace PDPhilip\Elasticsearch\DSL;
44

55
use Elastic\Elasticsearch\Exception\ClientResponseException;
6+
use Elastic\Elasticsearch\Exception\MissingParameterException;
67
use Elastic\Elasticsearch\Exception\ServerResponseException;
78
use Exception;
8-
use Elastic\Elasticsearch\ClientBuilder;
99
use Elastic\Elasticsearch\Client;
1010

1111

@@ -90,6 +90,7 @@ public function processIndicesDsl($method, $params): Results
9090
*/
9191
public function processFind($wheres, $options, $columns): Results
9292
{
93+
9394
$params = $this->buildParams($this->index, $wheres, $options, $columns);
9495

9596
return $this->_returnSearch($params, __FUNCTION__);
@@ -100,6 +101,7 @@ public function processFind($wheres, $options, $columns): Results
100101
*/
101102
public function processSearch($searchParams, $searchOptions, $wheres, $opts, $fields, $cols)
102103
{
104+
103105
$params = $this->buildSearchParams($this->index, $searchParams, $searchOptions, $wheres, $opts, $fields, $cols);
104106

105107
return $this->_returnSearch($params, __FUNCTION__);
@@ -115,6 +117,7 @@ protected function _returnSearch($params, $source)
115117
$process = $this->client->search($params);
116118

117119
return $this->_sanitizeSearchResponse($process, $params, $this->_queryTag($source));
120+
118121
} catch (Exception $e) {
119122

120123
$result = $this->_returnError($e->getMessage(), $e->getCode(), $params, $this->_queryTag(__FUNCTION__));
@@ -347,18 +350,34 @@ public function processScript($id, $script)
347350
// Index administration
348351
//----------------------------------------------------------------------
349352

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

354-
return $this->catIndices($response, $all);
366+
return $response->asArray();
355367
}
356368

357369
public function processIndexExists($index): bool
358370
{
359371
$params = ['index' => $index];
360372

361-
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+
362381
}
363382

364383
/**
@@ -401,9 +420,8 @@ public function processIndexSettings($index): mixed
401420
*/
402421
public function processIndexCreate($settings)
403422
{
404-
423+
$params = $this->buildIndexMap($this->index, $settings);
405424
try {
406-
$params = $this->buildIndexMap($this->index, $settings);
407425
$response = $this->client->indices()->create($params);
408426

409427
$result = $this->_return(true, $response, $params, $this->_queryTag(__FUNCTION__));
@@ -462,15 +480,32 @@ public function processIndexModify($settings): bool
462480
/**
463481
* @throws Exception
464482
*/
465-
public function processReIndex($newIndex, $oldIndex): bool
483+
public function processReIndex($oldIndex, $newIndex): Results
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);

src/DSL/IndexInterpreter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
trait IndexInterpreter
99
{
10-
public static function buildIndexMap($index, $raw): array
10+
public function buildIndexMap($index, $raw): array
1111
{
1212
$params = [];
1313
if ($index) {
@@ -45,7 +45,7 @@ public static function buildIndexMap($index, $raw): array
4545
return $params;
4646
}
4747

48-
public static function buildAnalyzerSettings($index, $raw): array
48+
public function buildAnalyzerSettings($index, $raw): array
4949
{
5050
$params = [];
5151
$params['index'] = $index;
@@ -70,7 +70,7 @@ public static function buildAnalyzerSettings($index, $raw): array
7070
}
7171

7272

73-
public static function catIndices($data, $all = false): array
73+
public function catIndices($data, $all = false): array
7474
{
7575
if (!$all && $data) {
7676
$indices = $data;
@@ -85,7 +85,7 @@ public static function catIndices($data, $all = false): array
8585
return $data;
8686
}
8787

88-
public static function cleanData($data): array
88+
public function cleanData($data): array
8989
{
9090
if ($data) {
9191
array_walk_recursive($data, function (&$item) {

src/DSL/QueryBuilder.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ public function buildSearchParams($index, $searchQuery, $searchOptions, $wheres
4949
}
5050
$queryString['fields'][] = $field;
5151
}
52+
if (count($queryString['fields']) > 1) {
53+
$queryString['type'] = 'cross_fields';
54+
}
55+
5256
}
5357
if ($searchOptions) {
5458
foreach ($searchOptions as $searchOption => $searchOptionValue) {
@@ -58,7 +62,7 @@ public function buildSearchParams($index, $searchQuery, $searchOptions, $wheres
5862

5963
$params['body']['query']['query_string'] = $queryString;
6064

61-
if ($columns && $columns != '*') {
65+
if ($columns && $columns != ['*']) {
6266
$params['body']['_source'] = $columns;
6367
}
6468
if ($options) {
@@ -75,6 +79,7 @@ public function buildSearchParams($index, $searchQuery, $searchOptions, $wheres
7579
}
7680
if (self::$filter) {
7781
$params = $this->_parseFilterParameter($params, self::$filter);
82+
self::$filter = [];
7883
}
7984

8085
return $params;
@@ -99,7 +104,6 @@ public function buildParams($index, $wheres, $options = [], $columns = [], $_id
99104
if ($columns && $columns != '*') {
100105
$params['body']['_source'] = $columns;
101106
}
102-
103107
$opts = $this->_buildOptions($options);
104108
if ($opts) {
105109
foreach ($opts as $key => $value) {
@@ -112,6 +116,7 @@ public function buildParams($index, $wheres, $options = [], $columns = [], $_id
112116
}
113117
if (self::$filter) {
114118
$params = $this->_parseFilterParameter($params, self::$filter);
119+
self::$filter = [];
115120
}
116121

117122
return $params;
@@ -359,7 +364,7 @@ public function _parseFilterParameter($params, $filer)
359364
],
360365
],
361366
];
362-
$params['body'] = $filteredBody;
367+
$params['body']['query'] = $filteredBody['query'];
363368
}
364369
if (!empty($body['query']['query_string'])) {
365370
$filteredBody = [
@@ -372,7 +377,7 @@ public function _parseFilterParameter($params, $filer)
372377
],
373378
],
374379
];
375-
$params['body'] = $filteredBody;
380+
$params['body']['query'] = $filteredBody['query'];
376381
}
377382

378383
return $params;

0 commit comments

Comments
 (0)