diff --git a/extensions/elasticsearch/Command.php b/extensions/elasticsearch/Command.php index e46210fafb..0780773a5d 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; /** @@ -64,6 +66,32 @@ class Command extends Component return $this->db->get($url, array_merge($this->options, $options), $query); } + /** + * 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 diff --git a/extensions/elasticsearch/Query.php b/extensions/elasticsearch/Query.php index cb515e9b14..d94d15e350 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); } /**