Skip to content

Commit

Permalink
Merge pull request #105 from designmynight/deleteByQuery
Browse files Browse the repository at this point in the history
fix: add refresh options
  • Loading branch information
samc05 authored Nov 4, 2020
2 parents 4ad17ed + 6a491be commit af64a80
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
68 changes: 68 additions & 0 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@
*/
class QueryBuilder extends BaseBuilder
{
/** @var string[] */
public const DELETE_REFRESH = [
'FALSE' => false,
'TRUE' => true,
];

/** @var string[] */
public const DELETE_CONFLICT = [
'ABORT' => 'abort',
'PROCEED' => 'proceed',
];

public $type;

public $filters;
Expand All @@ -37,6 +49,9 @@ class QueryBuilder extends BaseBuilder

protected $routing;

/** @var mixed[] */
protected $options;

/**
* All of the supported clause operators.
*
Expand Down Expand Up @@ -100,6 +115,14 @@ public function getRouting(): ?string
return $this->routing;
}

/**
* @return mixed|null
*/
public function getOption(string $option)
{
return $this->options[$option] ?? null;
}

/**
* Add a where between statement to the query.
*
Expand Down Expand Up @@ -573,6 +596,51 @@ public function withInnerHits(): self
return $this;
}

/**
* Set whether to refresh during delete by query
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/7.x/docs-delete-by-query.html#docs-delete-by-query-api-query-params
* @link https://www.elastic.co/guide/en/elasticsearch/reference/7.x/docs-delete-by-query.html#_refreshing_shards
*
* @param string $option
* @return self
* @throws \Exception
*/
public function withRefresh($option = self::DELETE_REFRESH['FALSE']): self
{
if (in_array($option, self::DELETE_REFRESH)) {
$this->options['delete_refresh'] = $option;

return $this;
}

throw new \Exception(
"$option is an invalid conflict option, valid options are: " . explode(', ', self::DELETE_CONFLICT)
);
}

/**
* Set how to handle conflucts during a delete request
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/7.x/docs-delete-by-query.html#docs-delete-by-query-api-query-params
*
* @param string $option
* @return self
* @throws \Exception
*/
public function onConflicts(string $option = self::DELETE_CONFLICT['ABORT']): self
{
if (in_array($option, self::DELETE_CONFLICT)) {
$this->options['delete_conflicts'] = $option;

return $this;
}

throw new \Exception(
"$option is an invalid conflict option, valid options are: " . explode(', ', self::DELETE_CONFLICT)
);
}

/**
* Adds a function score of any type
*
Expand Down
14 changes: 12 additions & 2 deletions src/QueryGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ protected function getValueForWhere(Builder $builder, array $where)
*/
protected function applyOptionsToClause(array $clause, array $where)
{
if (!isset($where['options'])) {
if (empty($where['options'])) {
return $clause;
}

Expand Down Expand Up @@ -1133,7 +1133,17 @@ public function compileInsert(Builder $builder, array $values): array
*/
public function compileDelete(Builder $builder): array
{
return $this->compileSelect($builder);
$clause = $this->compileSelect($builder);

if ($conflict = $builder->getOption('delete_conflicts')) {
$clause['conflicts'] = $conflict;
}

if ($refresh = $builder->getOption('delete_refresh')) {
$clause['refresh'] = $refresh;
}

return $clause;
}

/**
Expand Down

0 comments on commit af64a80

Please sign in to comment.