Interval.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Elasticsearch\Elasticsearch5\SearchAdapter\Aggregation;
  8. use Magento\Framework\Search\Dynamic\IntervalInterface;
  9. use Magento\Elasticsearch\SearchAdapter\ConnectionManager;
  10. use Magento\Elasticsearch\Model\Adapter\FieldMapperInterface;
  11. use Magento\Elasticsearch\Model\Config;
  12. use Magento\Elasticsearch\SearchAdapter\SearchIndexNameResolver;
  13. use Magento\CatalogSearch\Model\Indexer\Fulltext;
  14. /**
  15. * Aggregate price intervals for search query result.
  16. */
  17. class Interval implements IntervalInterface
  18. {
  19. /**
  20. * Minimal possible value
  21. */
  22. const DELTA = 0.005;
  23. /**
  24. * @var ConnectionManager
  25. */
  26. private $connectionManager;
  27. /**
  28. * @var FieldMapperInterface
  29. */
  30. private $fieldMapper;
  31. /**
  32. * @var Config
  33. */
  34. private $clientConfig;
  35. /**
  36. * @var string
  37. */
  38. private $fieldName;
  39. /**
  40. * @var string
  41. */
  42. private $storeId;
  43. /**
  44. * @var array
  45. */
  46. private $entityIds;
  47. /**
  48. * @var SearchIndexNameResolver
  49. */
  50. private $searchIndexNameResolver;
  51. /**
  52. * @param ConnectionManager $connectionManager
  53. * @param FieldMapperInterface $fieldMapper
  54. * @param Config $clientConfig
  55. * @param SearchIndexNameResolver $searchIndexNameResolver
  56. * @param string $fieldName
  57. * @param string $storeId
  58. * @param array $entityIds
  59. */
  60. public function __construct(
  61. ConnectionManager $connectionManager,
  62. FieldMapperInterface $fieldMapper,
  63. Config $clientConfig,
  64. SearchIndexNameResolver $searchIndexNameResolver,
  65. string $fieldName,
  66. string $storeId,
  67. array $entityIds
  68. ) {
  69. $this->connectionManager = $connectionManager;
  70. $this->fieldMapper = $fieldMapper;
  71. $this->clientConfig = $clientConfig;
  72. $this->fieldName = $fieldName;
  73. $this->storeId = $storeId;
  74. $this->entityIds = $entityIds;
  75. $this->searchIndexNameResolver = $searchIndexNameResolver;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function load($limit, $offset = null, $lower = null, $upper = null)
  81. {
  82. $from = $to = [];
  83. if ($lower) {
  84. $from = ['gte' => $lower - self::DELTA];
  85. }
  86. if ($upper) {
  87. $to = ['lt' => $upper - self::DELTA];
  88. }
  89. $requestQuery = $this->prepareBaseRequestQuery($from, $to);
  90. $requestQuery = array_merge_recursive(
  91. $requestQuery,
  92. ['body' => ['stored_fields' => [$this->fieldName], 'size' => $limit]]
  93. );
  94. if ($offset) {
  95. $requestQuery['body']['from'] = $offset;
  96. }
  97. $queryResult = $this->connectionManager->getConnection()
  98. ->query($requestQuery);
  99. return $this->arrayValuesToFloat($queryResult['hits']['hits'], $this->fieldName);
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function loadPrevious($data, $index, $lower = null)
  105. {
  106. if ($lower) {
  107. $from = ['gte' => $lower - self::DELTA];
  108. }
  109. if ($data) {
  110. $to = ['lt' => $data - self::DELTA];
  111. }
  112. $requestQuery = $this->prepareBaseRequestQuery($from, $to);
  113. $requestQuery = array_merge_recursive(
  114. $requestQuery,
  115. ['size' => 0]
  116. );
  117. $queryResult = $this->connectionManager->getConnection()
  118. ->query($requestQuery);
  119. $offset = $queryResult['hits']['total'];
  120. if (!$offset) {
  121. return false;
  122. }
  123. return $this->load($index - $offset + 1, $offset - 1, $lower);
  124. }
  125. /**
  126. * {@inheritdoc}
  127. */
  128. public function loadNext($data, $rightIndex, $upper = null)
  129. {
  130. $from = ['gt' => $data + self::DELTA];
  131. $to = ['lt' => $data - self::DELTA];
  132. $requestCountQuery = $this->prepareBaseRequestQuery($from, $to);
  133. $requestCountQuery = array_merge_recursive(
  134. $requestCountQuery,
  135. ['size' => 0]
  136. );
  137. $queryCountResult = $this->connectionManager->getConnection()
  138. ->query($requestCountQuery);
  139. $offset = $queryCountResult['hits']['total'];
  140. if (!$offset) {
  141. return false;
  142. }
  143. $from = ['gte' => $data - self::DELTA];
  144. if ($upper !== null) {
  145. $to = ['lt' => $data - self::DELTA];
  146. }
  147. $requestQuery = $requestCountQuery;
  148. $requestCountQuery['body']['query']['bool']['filter']['bool']['must']['range'] =
  149. [$this->fieldName => array_merge($from, $to)];
  150. $requestCountQuery['body']['from'] = $offset - 1;
  151. $requestCountQuery['body']['size'] = $rightIndex - $offset + 1;
  152. $queryResult = $this->connectionManager->getConnection()
  153. ->query($requestQuery);
  154. return array_reverse($this->arrayValuesToFloat($queryResult['hits']['hits'], $this->fieldName));
  155. }
  156. /**
  157. * Conver array values to float type.
  158. *
  159. * @param array $hits
  160. * @param string $fieldName
  161. *
  162. * @return float[]
  163. */
  164. private function arrayValuesToFloat(array $hits, string $fieldName): array
  165. {
  166. $returnPrices = [];
  167. foreach ($hits as $hit) {
  168. $returnPrices[] = (float)$hit['fields'][$fieldName][0];
  169. }
  170. return $returnPrices;
  171. }
  172. /**
  173. * Prepare base query for search.
  174. *
  175. * @param array|null $from
  176. * @param array|null $to
  177. * @return array
  178. */
  179. private function prepareBaseRequestQuery($from = null, $to = null): array
  180. {
  181. $requestQuery = [
  182. 'index' => $this->searchIndexNameResolver->getIndexName($this->storeId, Fulltext::INDEXER_ID),
  183. 'type' => $this->clientConfig->getEntityType(),
  184. 'body' => [
  185. 'stored_fields' => [
  186. '_id',
  187. ],
  188. 'query' => [
  189. 'bool' => [
  190. 'must' => [
  191. 'match_all' => new \stdClass(),
  192. ],
  193. 'filter' => [
  194. 'bool' => [
  195. 'must' => [
  196. [
  197. 'terms' => [
  198. '_id' => $this->entityIds,
  199. ],
  200. ],
  201. [
  202. 'range' => [
  203. $this->fieldName => array_merge($from, $to),
  204. ],
  205. ],
  206. ],
  207. ],
  208. ],
  209. ],
  210. ],
  211. 'sort' => [
  212. $this->fieldName,
  213. ],
  214. ],
  215. ];
  216. return $requestQuery;
  217. }
  218. }