Class yii\elasticsearch\ActiveDataProvider

Inheritanceyii\elasticsearch\ActiveDataProvider » yii\data\ActiveDataProvider
Available since extension's version2.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

Hide inherited properties

Property Type Description Defined By
$aggregations array All aggregations results. yii\elasticsearch\ActiveDataProvider
$queryResults array Full query results. yii\elasticsearch\ActiveDataProvider
$suggestions array All suggestions results. yii\elasticsearch\ActiveDataProvider

Property Details

Hide inherited properties

$aggregations public property

All aggregations results.

public array $aggregations null
$queryResults public property

Full query results.

public array $queryResults null
$suggestions public property

All suggestions results.

public array $suggestions null

Method Details

Hide inherited methods

getAggregation() public method

Returns results of the specified aggregation.

public array getAggregation ( $name )
$name string

Aggregation name.

return array

Aggregation results.

throws \yii\base\InvalidCallException

if query results do not contain the requested aggregation.

                public function getAggregation($name)
{
    $aggregations = $this->getAggregations();
    if (!isset($aggregations[$name])) {
        throw new InvalidCallException("Aggregation '{$name}' not found.");
    }
    return $aggregations[$name];
}

            
getAggregations() public method

public array getAggregations ( )
return array

All aggregations results

                public function getAggregations()
{
    $results = $this->getQueryResults();
    return isset($results['aggregations']) ? $results['aggregations'] : [];
}

            
getQueryResults() public method

public array getQueryResults ( )
return array

Full query results

                public function getQueryResults()
{
    if (!is_array($this->_queryResults)) {
        $this->prepare();
    }
    return $this->_queryResults;
}

            
getSuggestion() public method

Returns results of the specified suggestion.

public array getSuggestion ( $name )
$name string

Suggestion name.

return array

Suggestion results.

throws \yii\base\InvalidCallException

if query results do not contain the requested suggestion.

                public function getSuggestion($name)
{
    $suggestions = $this->getSuggestions();
    if (!isset($suggestions[$name])) {
        throw new InvalidCallException("Suggestion '{$name}' not found.");
    }
    return $suggestions[$name];
}

            
getSuggestions() public method

public array getSuggestions ( )
return array

All suggestions results

                public function getSuggestions()
{
    $results = $this->getQueryResults();
    return isset($results['suggest']) ? $results['suggest'] : [];
}

            
prepareKeys() protected method

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\elasticsearch\ActiveRecord */
        $class = $this->query->modelClass;
        foreach ($models as $model) {
            $keys[] = $model->primaryKey;
        }
        return $keys;
    } else {
        return array_keys($models);
    }
}

            
prepareModels() protected method

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 [];
}

            
prepareTotalCount() protected method

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();
    if (isset($results['hits']['total'])) {
        return is_array($results['hits']['total']) ? (int) $results['hits']['total']['value'] : (int) $results['hits']['total'];
    }
    return 0;
}

            
refresh() public method

public void refresh ( )

                public function refresh()
{
    parent::refresh();
    $this->_queryResults = null;
}

            
setQueryResults() public method

public void setQueryResults ( $results )
$results array

Full query results

                public function setQueryResults($results)
{
    $this->_queryResults = $results;
}