Commit cc10725c by tvdavid Committed by Carsten Brandt

Add support for elasticsearch delete by query

close #4487
parent 259b8c90
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
namespace yii\elasticsearch; namespace yii\elasticsearch;
use yii\base\Component; use yii\base\Component;
use yii\base\InvalidCallException;
use yii\base\InvalidParamException;
use yii\helpers\Json; use yii\helpers\Json;
/** /**
...@@ -65,6 +67,32 @@ class Command extends Component ...@@ -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 * Sends a request to the _suggest API and returns the result
* @param string|array $suggester the suggester body * @param string|array $suggester the suggester body
* @param array $options * @param array $options
......
...@@ -267,17 +267,16 @@ class Query extends Component implements QueryInterface ...@@ -267,17 +267,16 @@ class Query extends Component implements QueryInterface
/** /**
* Executes the query and deletes all matching documents. * 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. * @param Connection $db the database connection used to execute the query.
* If this parameter is not given, the `elasticsearch` application component will be used. * 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 return $this->createCommand($db)->deleteByQuery($options);
// http://www.elasticsearch.org/guide/en/elasticsearch/reference/1.x/_search_requests.html
throw new NotSupportedException('Delete by query is not implemented yet.');
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment