Interval.php 8.6 KB

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