From cc10725c2b10095c98e62abb407efe5bd9fffe35 Mon Sep 17 00:00:00 2001 From: tvdavid <github@tvmaze.com> Date: Mon, 28 Jul 2014 06:31:27 -0400 Subject: [PATCH] Add support for elasticsearch delete by query close #4487 --- extensions/elasticsearch/Command.php | 28 ++++++++++++++++++++++++++++ extensions/elasticsearch/Query.php | 11 +++++------ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/extensions/elasticsearch/Command.php b/extensions/elasticsearch/Command.php index e46210f..0780773 100644 --- a/extensions/elasticsearch/Command.php +++ b/extensions/elasticsearch/Command.php @@ -8,6 +8,8 @@ namespace yii\elasticsearch; use yii\base\Component; +use yii\base\InvalidCallException; +use yii\base\InvalidParamException; use yii\helpers\Json; /** @@ -65,6 +67,32 @@ class Command extends Component } /** + * Sends a request to the delete by query + * @param array $options + * @return mixed + */ + public function deleteByQuery($options = []) + { + if (!isset($this->queryParts['query'])) { + throw new InvalidCallException('Can not call deleteByQuery when no query is given.'); + } + $query = [ + 'query' => $this->queryParts['query'], + ]; + if (isset($this->queryParts['filter'])) { + $query['filter'] = $this->queryParts['filter']; + } + $query = Json::encode($query); + $url = [ + $this->index !== null ? $this->index : '_all', + $this->type !== null ? $this->type : '_all', + '_query' + ]; + + return $this->db->delete($url, array_merge($this->options, $options), $query); + } + + /** * Sends a request to the _suggest API and returns the result * @param string|array $suggester the suggester body * @param array $options diff --git a/extensions/elasticsearch/Query.php b/extensions/elasticsearch/Query.php index cb515e9..d94d15e 100644 --- a/extensions/elasticsearch/Query.php +++ b/extensions/elasticsearch/Query.php @@ -267,17 +267,16 @@ class Query extends Component implements QueryInterface /** * Executes the query and deletes all matching documents. * - * This will not run facet queries. + * Everything except query and filter will be ignored. * * @param Connection $db the database connection used to execute the query. * If this parameter is not given, the `elasticsearch` application component will be used. - * @return array the query results. If the query results in nothing, an empty array will be returned. + * @param array $options The options given with this query. + * @return array the query results. */ - public function delete($db = null) + public function delete($db = null, $options = []) { - // TODO implement http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-delete-by-query.html - // http://www.elasticsearch.org/guide/en/elasticsearch/reference/1.x/_search_requests.html - throw new NotSupportedException('Delete by query is not implemented yet.'); + return $this->createCommand($db)->deleteByQuery($options); } /** -- libgit2 0.27.1