DataProvider.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Elasticsearch\SearchAdapter\Dynamic;
  7. use Magento\Elasticsearch\SearchAdapter\QueryAwareInterface;
  8. use Magento\Elasticsearch\SearchAdapter\QueryContainer;
  9. /**
  10. * Elastic search data provider
  11. *
  12. * @api
  13. * @since 100.1.0
  14. */
  15. class DataProvider implements \Magento\Framework\Search\Dynamic\DataProviderInterface, QueryAwareInterface
  16. {
  17. /**
  18. * @var \Magento\Elasticsearch\SearchAdapter\ConnectionManager
  19. * @since 100.1.0
  20. */
  21. protected $connectionManager;
  22. /**
  23. * @var \Magento\Elasticsearch\Model\Adapter\FieldMapperInterface
  24. * @since 100.1.0
  25. */
  26. protected $fieldMapper;
  27. /**
  28. * @var \Magento\Catalog\Model\Layer\Filter\Price\Range
  29. * @since 100.1.0
  30. */
  31. protected $range;
  32. /**
  33. * @var \Magento\Framework\Search\Dynamic\IntervalFactory
  34. * @since 100.1.0
  35. */
  36. protected $intervalFactory;
  37. /**
  38. * @var \Magento\Elasticsearch\Model\Config
  39. * @deprecated 100.2.0 as this class shouldn't be responsible for query building
  40. * and should only modify existing query
  41. * @since 100.1.0
  42. */
  43. protected $clientConfig;
  44. /**
  45. * @var \Magento\Store\Model\StoreManagerInterface
  46. * @deprecated 100.2.0 as this class shouldn't be responsible for query building
  47. * and should only modify existing query
  48. * @since 100.1.0
  49. */
  50. protected $storeManager;
  51. /**
  52. * @var \Magento\Elasticsearch\SearchAdapter\SearchIndexNameResolver
  53. * @deprecated 100.2.0 as this class shouldn't be responsible for query building
  54. * and should only modify existing query
  55. * @since 100.1.0
  56. */
  57. protected $searchIndexNameResolver;
  58. /**
  59. * @var string
  60. * @deprecated 100.2.0 as this class shouldn't be responsible for query building
  61. * and should only modify existing query
  62. * @since 100.1.0
  63. */
  64. protected $indexerId;
  65. /**
  66. * @var \Magento\Framework\App\ScopeResolverInterface
  67. * @since 100.1.0
  68. */
  69. protected $scopeResolver;
  70. /**
  71. * @var QueryContainer
  72. */
  73. private $queryContainer;
  74. /**
  75. * @param \Magento\Elasticsearch\SearchAdapter\ConnectionManager $connectionManager
  76. * @param \Magento\Elasticsearch\Model\Adapter\FieldMapperInterface $fieldMapper
  77. * @param \Magento\Catalog\Model\Layer\Filter\Price\Range $range
  78. * @param \Magento\Framework\Search\Dynamic\IntervalFactory $intervalFactory
  79. * @param \Magento\Elasticsearch\Model\Config $clientConfig
  80. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  81. * @param \Magento\Elasticsearch\SearchAdapter\SearchIndexNameResolver $searchIndexNameResolver
  82. * @param string $indexerId
  83. * @param \Magento\Framework\App\ScopeResolverInterface $scopeResolver
  84. * @param QueryContainer|null $queryContainer
  85. *
  86. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  87. */
  88. public function __construct(
  89. \Magento\Elasticsearch\SearchAdapter\ConnectionManager $connectionManager,
  90. \Magento\Elasticsearch\Model\Adapter\FieldMapperInterface $fieldMapper,
  91. \Magento\Catalog\Model\Layer\Filter\Price\Range $range,
  92. \Magento\Framework\Search\Dynamic\IntervalFactory $intervalFactory,
  93. \Magento\Elasticsearch\Model\Config $clientConfig,
  94. \Magento\Store\Model\StoreManagerInterface $storeManager,
  95. \Magento\Elasticsearch\SearchAdapter\SearchIndexNameResolver $searchIndexNameResolver,
  96. $indexerId,
  97. \Magento\Framework\App\ScopeResolverInterface $scopeResolver,
  98. QueryContainer $queryContainer = null
  99. ) {
  100. $this->connectionManager = $connectionManager;
  101. $this->fieldMapper = $fieldMapper;
  102. $this->range = $range;
  103. $this->intervalFactory = $intervalFactory;
  104. $this->clientConfig = $clientConfig;
  105. $this->storeManager = $storeManager;
  106. $this->searchIndexNameResolver = $searchIndexNameResolver;
  107. $this->indexerId = $indexerId;
  108. $this->scopeResolver = $scopeResolver;
  109. $this->queryContainer = $queryContainer;
  110. }
  111. /**
  112. * @inheritdoc
  113. * @since 100.1.0
  114. */
  115. public function getRange()
  116. {
  117. return $this->range->getPriceRange();
  118. }
  119. /**
  120. * @inheritdoc
  121. * @since 100.1.0
  122. */
  123. public function getAggregations(\Magento\Framework\Search\Dynamic\EntityStorage $entityStorage)
  124. {
  125. $aggregations = [
  126. 'count' => 0,
  127. 'max' => 0,
  128. 'min' => 0,
  129. 'std' => 0,
  130. ];
  131. $query = $this->getBasicSearchQuery($entityStorage);
  132. $fieldName = $this->fieldMapper->getFieldName('price');
  133. $query['body']['aggregations'] = [
  134. 'prices' => [
  135. 'extended_stats' => [
  136. 'field' => $fieldName,
  137. ],
  138. ],
  139. ];
  140. $queryResult = $this->connectionManager->getConnection()
  141. ->query($query);
  142. if (isset($queryResult['aggregations']['prices'])) {
  143. $aggregations = [
  144. 'count' => $queryResult['aggregations']['prices']['count'],
  145. 'max' => $queryResult['aggregations']['prices']['max'],
  146. 'min' => $queryResult['aggregations']['prices']['min'],
  147. 'std' => $queryResult['aggregations']['prices']['std_deviation'],
  148. ];
  149. }
  150. return $aggregations;
  151. }
  152. /**
  153. * @inheritdoc
  154. * @since 100.1.0
  155. */
  156. public function getInterval(
  157. \Magento\Framework\Search\Request\BucketInterface $bucket,
  158. array $dimensions,
  159. \Magento\Framework\Search\Dynamic\EntityStorage $entityStorage
  160. ) {
  161. $entityIds = $entityStorage->getSource();
  162. $fieldName = $this->fieldMapper->getFieldName('price');
  163. $dimension = current($dimensions);
  164. $storeId = $this->scopeResolver->getScope($dimension->getValue())->getId();
  165. return $this->intervalFactory->create(
  166. [
  167. 'entityIds' => $entityIds,
  168. 'storeId' => $storeId,
  169. 'fieldName' => $fieldName,
  170. ]
  171. );
  172. }
  173. /**
  174. * @inheritdoc
  175. * @since 100.1.0
  176. */
  177. public function getAggregation(
  178. \Magento\Framework\Search\Request\BucketInterface $bucket,
  179. array $dimensions,
  180. $range,
  181. \Magento\Framework\Search\Dynamic\EntityStorage $entityStorage
  182. ) {
  183. $result = [];
  184. $query = $this->getBasicSearchQuery($entityStorage);
  185. $fieldName = $this->fieldMapper->getFieldName($bucket->getField());
  186. $query['body']['aggregations'] = [
  187. 'prices' => [
  188. 'histogram' => [
  189. 'field' => $fieldName,
  190. 'interval' => (float)$range,
  191. 'min_doc_count' => 1,
  192. ],
  193. ],
  194. ];
  195. $queryResult = $this->connectionManager->getConnection()
  196. ->query($query);
  197. foreach ($queryResult['aggregations']['prices']['buckets'] as $bucket) {
  198. $key = (int)($bucket['key'] / $range + 1);
  199. $result[$key] = $bucket['doc_count'];
  200. }
  201. return $result;
  202. }
  203. /**
  204. * @inheritdoc
  205. * @since 100.1.0
  206. */
  207. public function prepareData($range, array $dbRanges)
  208. {
  209. $data = [];
  210. if (!empty($dbRanges)) {
  211. $lastIndex = array_keys($dbRanges);
  212. $lastIndex = $lastIndex[count($lastIndex) - 1];
  213. foreach ($dbRanges as $index => $count) {
  214. $fromPrice = $index == 1 ? '' : ($index - 1) * $range;
  215. $toPrice = $index == $lastIndex ? '' : $index * $range;
  216. $data[] = [
  217. 'from' => $fromPrice,
  218. 'to' => $toPrice,
  219. 'count' => $count,
  220. ];
  221. }
  222. }
  223. return $data;
  224. }
  225. /**
  226. * Returns a basic search query which can be used for aggregations calculation
  227. *
  228. * The query may be requested from a query container if it has been set
  229. * or may be build by entity storage and dimensions.
  230. *
  231. * Building a query by entity storage is actually deprecated as the query
  232. * built in this way may cause ElasticSearch's TooManyClauses exception.
  233. *
  234. * The code which is responsible for building query in-place should be removed someday,
  235. * but for now it's a question of backward compatibility as this class may be used somewhere else
  236. * by extension developers and we can't guarantee that they'll pass a query into constructor.
  237. *
  238. * @param \Magento\Framework\Search\Dynamic\EntityStorage $entityStorage
  239. * @param array $dimensions
  240. * @return array
  241. */
  242. private function getBasicSearchQuery(
  243. \Magento\Framework\Search\Dynamic\EntityStorage $entityStorage,
  244. array $dimensions = []
  245. ) {
  246. if (null !== $this->queryContainer) {
  247. return $this->queryContainer->getQuery();
  248. }
  249. $entityIds = $entityStorage->getSource();
  250. $dimension = current($dimensions);
  251. $storeId = false !== $dimension
  252. ? $this->scopeResolver->getScope($dimension->getValue())->getId()
  253. : $this->storeManager->getStore()->getId();
  254. $query = [
  255. 'index' => $this->searchIndexNameResolver->getIndexName($storeId, $this->indexerId),
  256. 'type' => $this->clientConfig->getEntityType(),
  257. 'body' => [
  258. 'fields' => [
  259. '_id',
  260. '_score',
  261. ],
  262. 'query' => [
  263. 'bool' => [
  264. 'must' => [
  265. [
  266. 'terms' => [
  267. '_id' => $entityIds,
  268. ],
  269. ],
  270. ],
  271. ],
  272. ],
  273. ],
  274. ];
  275. return $query;
  276. }
  277. }