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