From 6053ebec85e16d3fd99478794e36c216edf21d63 Mon Sep 17 00:00:00 2001 From: Vitezslav Lindovsky Date: Tue, 30 Jun 2020 02:05:03 +0200 Subject: [PATCH] Delete - add options & return result from Client --- src/Indices/WorksWithDocuments.php | 64 +++++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 10 deletions(-) diff --git a/src/Indices/WorksWithDocuments.php b/src/Indices/WorksWithDocuments.php index f830dd0..aa54c94 100644 --- a/src/Indices/WorksWithDocuments.php +++ b/src/Indices/WorksWithDocuments.php @@ -109,7 +109,45 @@ public function createOrUpdateDocument(int $id, array $document, array $options /** * Deletes documents by given query. * + * $options['type'] = DEPRECATED (list) A comma-separated list of document types to search; leave empty to perform the operation on all types + * $options['analyzer'] = (string) The analyzer to use for the query string + * $options['analyze_wildcard'] = (boolean) Specify whether wildcard and prefix queries should be analyzed (default: false) + * $options['default_operator'] = (enum) The default operator for query string query (AND or OR) (Options = AND,OR) (Default = OR) + * $options['df'] = (string) The field to use as default where no field prefix is given in the query string + * $options['from'] = (number) Starting offset (default: 0) + * $options['ignore_unavailable'] = (boolean) Whether specified concrete indices should be ignored when unavailable (missing or closed) + * $options['allow_no_indices'] = (boolean) Whether to ignore if a wildcard indices expression resolves into no concrete indices. (This includes `_all` string or when no indices have been specified) + * $options['conflicts'] = (enum) What to do when the delete by query hits version conflicts? (Options = abort,proceed) (Default = abort) + * $options['expand_wildcards'] = (enum) Whether to expand wildcard expression to concrete indices that are open, closed or both. (Options = open,closed,hidden,none,all) (Default = open) + * $options['lenient'] = (boolean) Specify whether format-based query failures (such as providing text to a numeric field) should be ignored + * $options['preference'] = (string) Specify the node or shard the operation should be performed on (default: random) + * $options['q'] = (string) Query in the Lucene query string syntax + * $options['routing'] = (list) A comma-separated list of specific routing values + * $options['scroll'] = (time) Specify how long a consistent view of the index should be maintained for scrolled search + * $options['search_type'] = (enum) Search operation type (Options = query_then_fetch,dfs_query_then_fetch) + * $options['search_timeout'] = (time) Explicit timeout for each search request. Defaults to no timeout. + * $options['size'] = (number) Deprecated, please use `max_docs` instead + * $options['max_docs'] = (number) Maximum number of documents to process (default: all documents) + * $options['sort'] = (list) A comma-separated list of : pairs + * $options['_source'] = (list) True or false to return the _source field or not, or a list of fields to return + * $options['_source_excludes'] = (list) A list of fields to exclude from the returned _source field + * $options['_source_includes'] = (list) A list of fields to extract and return from the _source field + * $options['terminate_after'] = (number) The maximum number of documents to collect for each shard, upon reaching which the query execution will terminate early. + * $options['stats'] = (list) Specific 'tag' of the request for logging and statistical purposes + * $options['version'] = (boolean) Specify whether to return document version as part of a hit + * $options['request_cache'] = (boolean) Specify if request cache should be used for this request or not, defaults to index level setting + * $options['refresh'] = (boolean) Should the effected indexes be refreshed? + * $options['timeout'] = (time) Time each individual bulk request should wait for shards that are unavailable. (Default = 1m) + * $options['wait_for_active_shards'] = (string) Sets the number of shard copies that must be active before proceeding with the delete by query operation. Defaults to 1, meaning the primary shard only. Set to `all` for all shard copies, otherwise set to any non-negative value less than or equal to the total number of copies for the shard (number of replicas + 1) + * $options['scroll_size'] = (number) Size on the scroll request powering the delete by query (Default = 100) + * $options['wait_for_completion'] = (boolean) Should the request should block until the delete by query is complete. (Default = true) + * $options['requests_per_second'] = (number) The throttle for this request in sub-requests per second. -1 means no throttle. (Default = 0) + * $options['slices'] = (number|string) The number of slices this task should be divided into. Defaults to 1, meaning the task isn't sliced into subtasks. Can be set to `auto`. (Default = 1) + * * @param array $query + * @param array $options Additional params for ES client + * + * @return array Result from Client::deleteByQuery() * * @throws ElasticsearchException * @@ -119,20 +157,26 @@ public function createOrUpdateDocument(int $id, array $document, array $options * ], * ]); */ - public function deleteByQuery(array $query) + public function deleteByQuery(array $query, array $options = []): array { + $params = $options + [ + 'index' => $this->name, + 'body' => [ + 'query' => $query, + ], + ]; + try { - $this->client->deleteByQuery([ - 'index' => $this->name, - 'body' => [ - 'query' => $query, - ], - ]); + return $this->client->deleteByQuery($params); } catch (Missing404Exception $exception) { - // If the index was yet created - ignore - if (false === Str::contains($exception->getMessage(), 'index_not_found_exception')) { - throw $exception; + $indexNotFound = Str::contains($exception->getMessage(), 'index_not_found_exception'); + + // Ignore index not found + if ($indexNotFound) { + return []; } + + throw $exception; } } }