Skip to content

Commit 571ad04

Browse files
committed
Several upgrades and bug fixes
1 parent 903beae commit 571ad04

File tree

11 files changed

+647
-516
lines changed

11 files changed

+647
-516
lines changed

src/Connection.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
use PDPhilip\Elasticsearch\DSL\Bridge;
66
use Elasticsearch\ClientBuilder;
77
use Illuminate\Database\Connection as BaseConnection;
8-
use Illuminate\Support\Arr;
98
use Illuminate\Support\Str;
10-
use InvalidArgumentException;
119
use RuntimeException;
1210

1311

@@ -38,6 +36,11 @@ public function __construct(array $config)
3836

3937
}
4038

39+
public function setIndexPrefix($newPrefix)
40+
{
41+
$this->indexPrefix = $newPrefix;
42+
}
43+
4144
public function getIndexPrefix()
4245
{
4346
return $this->indexPrefix;
@@ -78,10 +81,11 @@ public function setMaxSize($value)
7881

7982
public function table($table, $as = null)
8083
{
81-
return $this->setIndex($table);
84+
$query = new Query\Builder($this, new Query\Processor());
85+
86+
return $query->from($table);
8287
}
8388

84-
8589
/**
8690
* @inheritdoc
8791
*/
@@ -208,6 +212,10 @@ protected function _apiConnection()
208212

209213
public function __call($method, $parameters)
210214
{
215+
if (!$this->index) {
216+
$this->index = $this->indexPrefix.'*';
217+
}
218+
211219
$bridge = new Bridge($this->client, $this->index, $this->maxSize);
212220

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

src/DSL/Bridge.php

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ protected function _returnSearch($params, $source)
108108
if (empty($params['size'])) {
109109
$params['size'] = $this->maxSize;
110110
}
111+
// dd($params);
111112
try {
112113
$process = $this->client->search($params);
113114

@@ -344,9 +345,14 @@ public function processScript($id, $script)
344345

345346
public function processGetIndices($all): array
346347
{
347-
$response = $this->client->cat()->indices();
348348

349-
return $this->catIndices($response, $all);
349+
$index = $this->index;
350+
if ($all) {
351+
$index = '*';
352+
}
353+
354+
return $this->client->indices()->get(['index' => $index]);
355+
350356
}
351357

352358
public function processIndexExists($index): bool
@@ -456,15 +462,31 @@ public function processIndexModify($settings): bool
456462
/**
457463
* @throws Exception
458464
*/
459-
public function processReIndex($newIndex, $oldIndex): bool
465+
public function processReIndex($oldIndex, $newIndex): Results
460466
{
461-
$params['source']['index'] = $oldIndex;
462-
$params['dest']['index'] = $newIndex;
467+
$prefix = str_replace('*', '', $this->index);
468+
if ($prefix) {
469+
$oldIndex = $prefix.'_'.$oldIndex;
470+
$newIndex = $prefix.'_'.$newIndex;
471+
}
472+
$params['body']['source']['index'] = $oldIndex;
473+
$params['body']['dest']['index'] = $newIndex;
463474
try {
464475
$response = $this->client->reindex($params);
465-
$result = $this->_return(true, $response, $params, $this->_queryTag(__FUNCTION__));
476+
$result = $response;
477+
$resultData = [
478+
'took' => $result['took'],
479+
'total' => $result['total'],
480+
'created' => $result['created'],
481+
'updated' => $result['updated'],
482+
'deleted' => $result['deleted'],
483+
'batches' => $result['batches'],
484+
'version_conflicts' => $result['version_conflicts'],
485+
'noops' => $result['noops'],
486+
'retries' => $result['retries'],
487+
];
466488

467-
return true;
489+
return $this->_return($resultData, $result, $params, $this->_queryTag(__FUNCTION__));
468490
} catch (Exception $e) {
469491
$result = $this->_returnError($e->getMessage(), $e->getCode(), $params, $this->_queryTag(__FUNCTION__));
470492
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 & 11 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,19 +79,13 @@ 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;
8186
}
8287

8388
/**
84-
* @param $index
85-
* @param $wheres
86-
* @param $options
87-
* @param $columns
88-
* @param $_id
89-
*
90-
* @return array
9189
* @throws Exception
9290
*/
9391
public function buildParams($index, $wheres, $options = [], $columns = [], $_id = null): array
@@ -106,7 +104,6 @@ public function buildParams($index, $wheres, $options = [], $columns = [], $_id
106104
if ($columns && $columns != '*') {
107105
$params['body']['_source'] = $columns;
108106
}
109-
110107
$opts = $this->_buildOptions($options);
111108
if ($opts) {
112109
foreach ($opts as $key => $value) {
@@ -119,6 +116,7 @@ public function buildParams($index, $wheres, $options = [], $columns = [], $_id
119116
}
120117
if (self::$filter) {
121118
$params = $this->_parseFilterParameter($params, self::$filter);
119+
self::$filter = [];
122120
}
123121

124122
return $params;
@@ -366,7 +364,7 @@ public function _parseFilterParameter($params, $filer)
366364
],
367365
],
368366
];
369-
$params['body'] = $filteredBody;
367+
$params['body']['query'] = $filteredBody['query'];
370368
}
371369
if (!empty($body['query']['query_string'])) {
372370
$filteredBody = [
@@ -379,7 +377,7 @@ public function _parseFilterParameter($params, $filer)
379377
],
380378
],
381379
];
382-
$params['body'] = $filteredBody;
380+
$params['body']['query'] = $filteredBody['query'];
383381
}
384382

385383
return $params;

0 commit comments

Comments
 (0)