forked from yiisoft/yii2-elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActiveDataProvider.php
160 lines (142 loc) · 4.78 KB
/
ActiveDataProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\elasticsearch;
use yii\base\InvalidCallException;
use yii\base\InvalidConfigException;
use yii\db\ActiveQueryInterface;
/**
* 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.
*
* @property array $queryResults the query results.
* @property array $aggregations all aggregations results.
*
* @author Paul Klimov <[email protected]>
* @since 2.0.5
*/
class ActiveDataProvider extends \yii\data\ActiveDataProvider
{
/**
* @var array the full query results.
*/
private $_queryResults;
/**
* @param array $results full query results
*/
public function setQueryResults($results)
{
$this->_queryResults = $results;
}
/**
* @return array full query results
*/
public function getQueryResults()
{
if (!is_array($this->_queryResults)) {
$this->prepare();
}
return $this->_queryResults;
}
/**
* @return array all aggregations results
*/
public function getAggregations()
{
$results = $this->getQueryResults();
return isset($results['aggregations']) ? $results['aggregations'] : [];
}
/**
* Returns results of the specified aggregation.
* @param string $name aggregation name.
* @return array aggregation results.
* @throws 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];
}
/**
* @inheritdoc
*/
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());
}
$results = $query->search($this->db);
$this->setQueryResults($results);
if ($pagination !== false) {
$pagination->totalCount = $this->getTotalCount();
}
return $results['hits']['hits'];
}
/**
* @inheritdoc
*/
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 (int)$results['hits']['total'];
}
/**
* @inheritdoc
*/
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 (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);
}
}
}