Skip to content

Commit a8b384d

Browse files
committed
New connection options as properties with getters
- Properties with getters are inline with other connection based options - Renamed perform_unsafe_queries to bypass_map_validation (sounds less ominous) - Added bypass to other map check queries
1 parent adbec40 commit a8b384d

File tree

4 files changed

+96
-63
lines changed

4 files changed

+96
-63
lines changed

src/Connection.php

Lines changed: 75 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ class Connection extends BaseConnection
7777

7878
protected string $connectionName;
7979

80+
protected bool $byPassMapValidation = false;
81+
82+
protected int $insertChunkSize = 1000;
83+
8084
/**
8185
* @var Query\Processor
8286
*/
@@ -90,6 +94,7 @@ public function __construct(array $config)
9094
$this->config = $config;
9195

9296
$this->_sanitizeConfig();
97+
9398
$this->_validateConnection();
9499

95100
$this->setOptions();
@@ -126,8 +131,34 @@ public function setOptions(): void
126131
: $this->config['error_log_index'];
127132
}
128133

134+
if (! empty($this->config['options']['bypass_map_validation'])) {
135+
$this->byPassMapValidation = $this->config['options']['bypass_map_validation'];
136+
}
137+
138+
if (! empty($this->config['options']['insert_chunk_size'])) {
139+
$this->insertChunkSize = $this->config['options']['insert_chunk_size'];
140+
}
141+
142+
}
143+
144+
/** {@inheritdoc} */
145+
public function table($table, $as = null)
146+
{
147+
$query = new Query\Builder($this, new Query\Processor);
148+
149+
return $query->from($table);
150+
}
151+
152+
/** {@inheritdoc} */
153+
public function disconnect(): void
154+
{
155+
$this->client = null;
129156
}
130157

158+
//----------------------------------------------------------------------
159+
// Getters
160+
//----------------------------------------------------------------------
161+
131162
/** {@inheritdoc} */
132163
public function getTablePrefix(): ?string
133164
{
@@ -176,21 +207,20 @@ public function getIndex(): string
176207
return $this->index;
177208
}
178209

179-
public function setIndex(string $index): string
210+
/** {@inheritdoc} */
211+
public function getDriverName(): string
180212
{
181-
$this->index = $this->indexPrefix && ! str_contains($index, $this->indexPrefix.'_')
182-
? $this->indexPrefix.'_'.$index
183-
: $index;
184-
185-
return $this->getIndex();
213+
return 'elasticsearch';
186214
}
187215

188-
/** {@inheritdoc} */
189-
public function table($table, $as = null)
216+
public function getClient(): ?Client
190217
{
191-
$query = new Query\Builder($this, new Query\Processor);
218+
return $this->client;
219+
}
192220

193-
return $query->from($table);
221+
public function getMaxSize(): int
222+
{
223+
return $this->maxSize;
194224
}
195225

196226
/**
@@ -201,36 +231,55 @@ public function getSchemaBuilder(): Schema\Builder
201231
return new Schema\Builder($this);
202232
}
203233

204-
/** {@inheritdoc} */
205-
public function disconnect(): void
234+
public function getAllowIdSort(): bool
206235
{
207-
$this->client = null;
236+
return $this->allowIdSort;
237+
}
238+
239+
public function getBypassMapValidation(): bool
240+
{
241+
return $this->byPassMapValidation;
242+
}
243+
244+
public function getInsertChunkSize(): int
245+
{
246+
return $this->insertChunkSize;
208247
}
209248

210249
/** {@inheritdoc} */
211-
public function getDriverName(): string
250+
protected function getDefaultPostProcessor(): Query\Processor
212251
{
213-
return 'elasticsearch';
252+
return new Query\Processor;
214253
}
215254

216-
public function getClient(): ?Client
255+
/** {@inheritdoc} */
256+
protected function getDefaultQueryGrammar(): Query\Grammar
217257
{
218-
return $this->client;
258+
return new Query\Grammar;
219259
}
220260

221-
public function getMaxSize(): int
261+
/** {@inheritdoc} */
262+
protected function getDefaultSchemaGrammar(): Schema\Grammar
222263
{
223-
return $this->maxSize;
264+
return new Schema\Grammar;
224265
}
225266

226-
public function setMaxSize($value): void
267+
//----------------------------------------------------------------------
268+
// Setters
269+
//----------------------------------------------------------------------
270+
271+
public function setIndex(string $index): string
227272
{
228-
$this->maxSize = $value;
273+
$this->index = $this->indexPrefix && ! str_contains($index, $this->indexPrefix.'_')
274+
? $this->indexPrefix.'_'.$index
275+
: $index;
276+
277+
return $this->getIndex();
229278
}
230279

231-
public function getAllowIdSort(): bool
280+
public function setMaxSize($value): void
232281
{
233-
return $this->allowIdSort;
282+
$this->maxSize = $value;
234283
}
235284

236285
public function __call($method, $parameters)
@@ -245,33 +294,15 @@ public function __call($method, $parameters)
245294
}
246295
$bridge = new Bridge($this);
247296

248-
$methodName = 'process' . Str::studly($method);
297+
$methodName = 'process'.Str::studly($method);
249298

250299
if (! method_exists($bridge, $methodName)) {
251-
throw new LogicException("{$methodName} does not exist on the bridge.");
300+
throw new LogicException("{$methodName} does not exist on the bridge.");
252301
}
253302

254303
return $bridge->{$methodName}(...$parameters);
255304
}
256305

257-
/** {@inheritdoc} */
258-
protected function getDefaultPostProcessor(): Query\Processor
259-
{
260-
return new Query\Processor;
261-
}
262-
263-
/** {@inheritdoc} */
264-
protected function getDefaultQueryGrammar(): Query\Grammar
265-
{
266-
return new Query\Grammar;
267-
}
268-
269-
/** {@inheritdoc} */
270-
protected function getDefaultSchemaGrammar(): Schema\Grammar
271-
{
272-
return new Schema\Grammar;
273-
}
274-
275306
/**
276307
* Sanitizes the configuration array by merging it with a predefined array of default configuration settings.
277308
* This ensures that all required configuration keys exist, even if they are set to null or default values.
@@ -291,7 +322,7 @@ private function _sanitizeConfig(): void
291322
'api_id' => null,
292323
'index_prefix' => '',
293324
'options' => [
294-
'perform_unsafe_queries' => false, // This skips the safety checks for Elastic Specific queries.
325+
'bypass_map_validation' => false, // This skips the safety checks for Elastic Specific queries.
295326
'insert_chunk_size' => 1000, // This is the maximum insert chunk size to use when bulk inserting
296327
'logging' => false,
297328
'allow_id_sort' => false,

src/DSL/Bridge.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,19 +1120,13 @@ private function _matrixDistinctAggregate($wheres, $options, $columns)
11201120
*/
11211121
public function parseRequiredKeywordMapping($field): ?string
11221122
{
1123-
if ($this->connection->getConfig('options.perform_unsafe_queries')) {
1124-
return null;
1125-
}
1126-
11271123
if (! $this->cachedKeywordFields instanceof Collection) {
11281124
$mapping = $this->processFieldMapping($this->index, '*');
11291125
$fullMap = new Collection($mapping);
11301126
$keywordFields = $fullMap->filter(fn ($value) => $value == 'keyword');
11311127
$this->cachedKeywordFields = $keywordFields;
1132-
// Log::info('cached');
11331128

11341129
}
1135-
// Log::info('returned');
11361130
$keywordFields = $this->cachedKeywordFields;
11371131

11381132
if ($keywordFields->isEmpty()) {

src/DSL/QueryBuilder.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -337,20 +337,28 @@ private function _parseCondition($condition, $parentField = null): array
337337
$queryPart = ['bool' => ['must_not' => [['match' => [$field => $operand]]]]];
338338
break;
339339
case 'in':
340-
$keywordField = $this->parseRequiredKeywordMapping($field);
341-
if (! $keywordField) {
340+
if ($this->connection->getBypassMapValidation()) {
342341
$queryPart = ['terms' => [$field => $operand]];
343342
} else {
344-
$queryPart = ['terms' => [$keywordField => $operand]];
343+
$keywordField = $this->parseRequiredKeywordMapping($field);
344+
if (! $keywordField) {
345+
$queryPart = ['terms' => [$field => $operand]];
346+
} else {
347+
$queryPart = ['terms' => [$keywordField => $operand]];
348+
}
345349
}
346350

347351
break;
348352
case 'nin':
349-
$keywordField = $this->parseRequiredKeywordMapping($field);
350-
if (! $keywordField) {
353+
if ($this->connection->getBypassMapValidation()) {
351354
$queryPart = ['bool' => ['must_not' => ['terms' => [$field => $operand]]]];
352355
} else {
353-
$queryPart = ['bool' => ['must_not' => ['terms' => [$keywordField => $operand]]]];
356+
$keywordField = $this->parseRequiredKeywordMapping($field);
357+
if (! $keywordField) {
358+
$queryPart = ['bool' => ['must_not' => ['terms' => [$field => $operand]]]];
359+
} else {
360+
$queryPart = ['bool' => ['must_not' => ['terms' => [$keywordField => $operand]]]];
361+
}
354362
}
355363

356364
break;
@@ -368,13 +376,13 @@ private function _parseCondition($condition, $parentField = null): array
368376
break;
369377
case 'exact':
370378

371-
if($this->connection->getConfig('options.perform_unsafe_queries')){
372-
$keywordField = $field;
379+
if ($this->connection->getBypassMapValidation()) {
380+
$keywordField = $field;
373381
} else {
374-
$keywordField = $this->parseRequiredKeywordMapping($field);
375-
if (! $keywordField ) {
376-
throw new ParameterException('Field ['.$field.'] is not a keyword field which is required for the [exact] operator.');
377-
}
382+
$keywordField = $this->parseRequiredKeywordMapping($field);
383+
if (! $keywordField) {
384+
throw new ParameterException('Field ['.$field.'] is not a keyword field which is required for the [exact] operator.');
385+
}
378386
}
379387

380388
$queryPart = ['term' => [$keywordField => $operand]];

src/Query/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1678,7 +1678,7 @@ protected function _processInsert(array $values, bool $returnData, bool $saveWit
16781678
}
16791679
$this->applyBeforeQueryCallbacks();
16801680

1681-
$insertChunkSize = $this->getConnection()->getConfig('options.insert_chunk_size') ?? 1000;
1681+
$insertChunkSize = $this->getConnection()->getInsertChunkSize() ?? 1000;
16821682

16831683
collect($values)->chunk($insertChunkSize)->each(callback: function ($chunk) use (&$response, $returnData) {
16841684
$result = $this->connection->insertBulk($chunk->toArray(), $returnData, $this->refresh);

0 commit comments

Comments
 (0)