Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataProvider.php
Go to the documentation of this file.
1 <?php
7 
10 
18 {
23  protected $connectionManager;
24 
29  protected $fieldMapper;
30 
35  protected $range;
36 
41  protected $intervalFactory;
42 
49  protected $clientConfig;
50 
57  protected $storeManager;
58 
66 
73  protected $indexerId;
74 
79  protected $scopeResolver;
80 
84  private $queryContainer;
85 
100  public function __construct(
102  \Magento\Elasticsearch\Model\Adapter\FieldMapperInterface $fieldMapper,
103  \Magento\Catalog\Model\Layer\Filter\Price\Range $range,
104  \Magento\Framework\Search\Dynamic\IntervalFactory $intervalFactory,
106  \Magento\Store\Model\StoreManagerInterface $storeManager,
108  $indexerId,
109  \Magento\Framework\App\ScopeResolverInterface $scopeResolver,
110  QueryContainer $queryContainer = null
111  ) {
112  $this->connectionManager = $connectionManager;
113  $this->fieldMapper = $fieldMapper;
114  $this->range = $range;
115  $this->intervalFactory = $intervalFactory;
116  $this->clientConfig = $clientConfig;
117  $this->storeManager = $storeManager;
118  $this->searchIndexNameResolver = $searchIndexNameResolver;
119  $this->indexerId = $indexerId;
120  $this->scopeResolver = $scopeResolver;
121  $this->queryContainer = $queryContainer;
122  }
123 
128  public function getRange()
129  {
130  return $this->range->getPriceRange();
131  }
132 
137  public function getAggregations(\Magento\Framework\Search\Dynamic\EntityStorage $entityStorage)
138  {
139  $aggregations = [
140  'count' => 0,
141  'max' => 0,
142  'min' => 0,
143  'std' => 0,
144  ];
145 
146  $query = $this->getBasicSearchQuery($entityStorage);
147 
148  $fieldName = $this->fieldMapper->getFieldName('price');
149  $query['body']['aggregations'] = [
150  'prices' => [
151  'extended_stats' => [
152  'field' => $fieldName,
153  ],
154  ],
155  ];
156 
157  $queryResult = $this->connectionManager->getConnection()
158  ->query($query);
159 
160  if (isset($queryResult['aggregations']['prices'])) {
161  $aggregations = [
162  'count' => $queryResult['aggregations']['prices']['count'],
163  'max' => $queryResult['aggregations']['prices']['max'],
164  'min' => $queryResult['aggregations']['prices']['min'],
165  'std' => $queryResult['aggregations']['prices']['std_deviation'],
166  ];
167  }
168 
169  return $aggregations;
170  }
171 
176  public function getInterval(
177  \Magento\Framework\Search\Request\BucketInterface $bucket,
178  array $dimensions,
179  \Magento\Framework\Search\Dynamic\EntityStorage $entityStorage
180  ) {
181  $entityIds = $entityStorage->getSource();
182  $fieldName = $this->fieldMapper->getFieldName('price');
183  $dimension = current($dimensions);
184  $storeId = $this->scopeResolver->getScope($dimension->getValue())->getId();
185 
186  return $this->intervalFactory->create(
187  [
188  'entityIds' => $entityIds,
189  'storeId' => $storeId,
190  'fieldName' => $fieldName,
191  ]
192  );
193  }
194 
199  public function getAggregation(
200  \Magento\Framework\Search\Request\BucketInterface $bucket,
201  array $dimensions,
202  $range,
203  \Magento\Framework\Search\Dynamic\EntityStorage $entityStorage
204  ) {
205  $result = [];
206 
207  $query = $this->getBasicSearchQuery($entityStorage);
208 
209  $fieldName = $this->fieldMapper->getFieldName($bucket->getField());
210  $query['body']['aggregations'] = [
211  'prices' => [
212  'histogram' => [
213  'field' => $fieldName,
214  'interval' => (float)$range,
215  ],
216  ],
217  ];
218 
219  $queryResult = $this->connectionManager->getConnection()
220  ->query($query);
221  foreach ($queryResult['aggregations']['prices']['buckets'] as $bucket) {
222  $key = intval($bucket['key'] / $range + 1);
223  $result[$key] = $bucket['doc_count'];
224  }
225 
226  return $result;
227  }
228 
233  public function prepareData($range, array $dbRanges)
234  {
235  $data = [];
236  if (!empty($dbRanges)) {
237  $lastIndex = array_keys($dbRanges);
238  $lastIndex = $lastIndex[count($lastIndex) - 1];
239  foreach ($dbRanges as $index => $count) {
240  $fromPrice = $index == 1 ? '' : ($index - 1) * $range;
241  $toPrice = $index == $lastIndex ? '' : $index * $range;
242  $data[] = [
243  'from' => $fromPrice,
244  'to' => $toPrice,
245  'count' => $count,
246  ];
247  }
248  }
249 
250  return $data;
251  }
252 
270  private function getBasicSearchQuery(
271  \Magento\Framework\Search\Dynamic\EntityStorage $entityStorage,
272  array $dimensions = []
273  ) {
274  if (null !== $this->queryContainer) {
275  return $this->queryContainer->getQuery();
276  }
277 
278  $entityIds = $entityStorage->getSource();
279 
280  $dimension = current($dimensions);
281  $storeId = false !== $dimension
282  ? $this->scopeResolver->getScope($dimension->getValue())->getId()
283  : $this->storeManager->getStore()->getId();
284 
285  $query = [
286  'index' => $this->searchIndexNameResolver->getIndexName($storeId, $this->indexerId),
287  'type' => $this->clientConfig->getEntityType(),
288  'body' => [
289  'fields' => [
290  '_id',
291  '_score',
292  ],
293  'query' => [
294  'bool' => [
295  'must' => [
296  [
297  'terms' => [
298  '_id' => $entityIds,
299  ],
300  ],
301  ],
302  ],
303  ],
304  ],
305  ];
306 
307  return $query;
308  }
309 }
$count
Definition: recent.phtml:13
getAggregations(\Magento\Framework\Search\Dynamic\EntityStorage $entityStorage)
getInterval(\Magento\Framework\Search\Request\BucketInterface $bucket, array $dimensions, \Magento\Framework\Search\Dynamic\EntityStorage $entityStorage)
getAggregation(\Magento\Framework\Search\Request\BucketInterface $bucket, array $dimensions, $range, \Magento\Framework\Search\Dynamic\EntityStorage $entityStorage)
$index
Definition: list.phtml:44
__construct(\Magento\Elasticsearch\SearchAdapter\ConnectionManager $connectionManager, \Magento\Elasticsearch\Model\Adapter\FieldMapperInterface $fieldMapper, \Magento\Catalog\Model\Layer\Filter\Price\Range $range, \Magento\Framework\Search\Dynamic\IntervalFactory $intervalFactory, \Magento\Elasticsearch\Model\Config $clientConfig, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Elasticsearch\SearchAdapter\SearchIndexNameResolver $searchIndexNameResolver, $indexerId, \Magento\Framework\App\ScopeResolverInterface $scopeResolver, QueryContainer $queryContainer=null)