Class yii\elasticsearch\ActiveDataProvider
Inheritance | yii\elasticsearch\ActiveDataProvider » yii\data\ActiveDataProvider |
---|---|
Available since extension's version | 2.0.5 |
Source Code | https://github.com/yiisoft/yii2-elasticsearch/blob/master/ActiveDataProvider.php |
ActiveDataProvider is an enhanced version of \yii\data\ActiveDataProvider specific to the ElasticSearch.
It allows to fetch not only rows and total rows count, but full query results including aggregations and so on.
Note: this data provider fetches result models and total count using single ElasticSearch query, so results total count will be fetched after pagination limit applying, which eliminates ability to verify if requested page number actually exist. Data provider disables yii\data\Pagination::validatePage automatically because of this.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$aggregations | array | All aggregations results. | yii\elasticsearch\ActiveDataProvider |
$queryResults | array | Full query results. | yii\elasticsearch\ActiveDataProvider |
Public Methods
Method | Description | Defined By |
---|---|---|
getAggregation() | Returns results of the specified aggregation. | yii\elasticsearch\ActiveDataProvider |
getAggregations() | yii\elasticsearch\ActiveDataProvider | |
getQueryResults() | yii\elasticsearch\ActiveDataProvider | |
refresh() | yii\elasticsearch\ActiveDataProvider | |
setQueryResults() | yii\elasticsearch\ActiveDataProvider |
Protected Methods
Method | Description | Defined By |
---|---|---|
prepareKeys() | yii\elasticsearch\ActiveDataProvider | |
prepareModels() | yii\elasticsearch\ActiveDataProvider | |
prepareTotalCount() | yii\elasticsearch\ActiveDataProvider |
Property Details
All aggregations results. This property is read-only.
Method Details
Returns results of the specified aggregation.
public array getAggregation ( $name ) | ||
$name | string |
Aggregation name. |
return | array |
Aggregation results. |
---|---|---|
throws | \yii\base\InvalidCallException |
if requested aggregation does not present in query results. |
public function getAggregation($name)
{
$aggregations = $this->getAggregations();
if (!isset($aggregations[$name])) {
throw new InvalidCallException("Aggregation '{$name}' does not present.");
}
return $aggregations[$name];
}
public array getAggregations ( ) | ||
return | array |
All aggregations results |
---|
public function getAggregations()
{
$results = $this->getQueryResults();
return isset($results['aggregations']) ? $results['aggregations'] : [];
}
public array getQueryResults ( ) | ||
return | array |
Full query results |
---|
public function getQueryResults()
{
if (!is_array($this->_queryResults)) {
$this->prepare();
}
return $this->_queryResults;
}
protected void prepareKeys ( $models ) | ||
$models |
protected function prepareKeys($models)
{
$keys = [];
if ($this->key !== null) {
foreach ($models as $model) {
if (is_string($this->key)) {
$keys[] = $model[$this->key];
} else {
$keys[] = call_user_func($this->key, $model);
}
}
return $keys;
} elseif ($this->query instanceof ActiveQueryInterface) {
/* @var $class \yii\db\ActiveRecord */
$class = $this->query->modelClass;
$pks = $class::primaryKey();
if (!is_array($pks) || count($pks) === 1) {
foreach ($models as $model) {
$keys[] = $model->primaryKey;
}
} else {
foreach ($models as $model) {
$kk = [];
foreach ($pks as $pk) {
$kk[$pk] = $model[$pk];
}
$keys[] = $kk;
}
}
return $keys;
} else {
return array_keys($models);
}
}
protected void prepareModels ( ) |
protected function prepareModels()
{
if (!$this->query instanceof Query) {
throw new InvalidConfigException('The "query" property must be an instance "' . Query::className() . '" or its subclasses.');
}
$query = clone $this->query;
if (($pagination = $this->getPagination()) !== false) {
// pagination fails to validate page number, because total count is unknown at this stage
$pagination->validatePage = false;
$query->limit($pagination->getLimit())->offset($pagination->getOffset());
}
if (($sort = $this->getSort()) !== false) {
$query->addOrderBy($sort->getOrders());
}
if (is_array(($results = $query->search($this->db)))) {
$this->setQueryResults($results);
if ($pagination !== false) {
$pagination->totalCount = $this->getTotalCount();
}
return $results['hits']['hits'];
}
$this->setQueryResults([]);
return [];
}
protected void prepareTotalCount ( ) |
protected function prepareTotalCount()
{
if (!$this->query instanceof Query) {
throw new InvalidConfigException('The "query" property must be an instance "' . Query::className() . '" or its subclasses.');
}
$results = $this->getQueryResults();
return isset($results['hits']['total']) ? (int)$results['hits']['total'] : 0;
}
public void refresh ( ) |
public function refresh()
{
parent::refresh();
$this->_queryResults = null;
}
public void setQueryResults ( $results ) | ||
$results | array |
Full query results |
public function setQueryResults($results)
{
$this->_queryResults = $results;
}