6 declare(strict_types=1);
30 private $connectionManager;
40 private $clientConfig;
60 private $searchIndexNameResolver;
80 $this->connectionManager = $connectionManager;
81 $this->fieldMapper = $fieldMapper;
82 $this->clientConfig = $clientConfig;
83 $this->fieldName = $fieldName;
85 $this->entityIds = $entityIds;
86 $this->searchIndexNameResolver = $searchIndexNameResolver;
92 public function load($limit, $offset =
null, $lower =
null, $upper =
null)
102 $requestQuery = $this->prepareBaseRequestQuery($from, $to);
103 $requestQuery = array_merge_recursive(
105 [
'body' => [
'stored_fields' => [$this->fieldName],
'size' => $limit]]
109 $requestQuery[
'body'][
'from'] = $offset;
112 $queryResult = $this->connectionManager->getConnection()
113 ->query($requestQuery);
115 return $this->arrayValuesToFloat($queryResult[
'hits'][
'hits'], $this->fieldName);
130 $requestQuery = $this->prepareBaseRequestQuery($from, $to);
131 $requestQuery = array_merge_recursive(
136 $queryResult = $this->connectionManager->getConnection()
137 ->query($requestQuery);
139 $offset = $queryResult[
'hits'][
'total'];
144 return $this->
load(
$index - $offset + 1, $offset - 1, $lower);
155 $requestCountQuery = $this->prepareBaseRequestQuery($from, $to);
156 $requestCountQuery = array_merge_recursive(
161 $queryCountResult = $this->connectionManager->getConnection()
162 ->query($requestCountQuery);
164 $offset = $queryCountResult[
'hits'][
'total'];
170 if ($upper !==
null) {
174 $requestQuery = $requestCountQuery;
176 $requestCountQuery[
'body'][
'query'][
'bool'][
'filter'][
'bool'][
'must'][
'range'] =
177 [$this->fieldName => array_merge($from, $to)];
178 $requestCountQuery[
'body'][
'from'] = $offset - 1;
179 $requestCountQuery[
'body'][
'size'] = $rightIndex - $offset + 1;
180 $queryResult = $this->connectionManager->getConnection()
181 ->query($requestQuery);
183 return array_reverse($this->arrayValuesToFloat($queryResult[
'hits'][
'hits'], $this->fieldName));
194 private function arrayValuesToFloat(array $hits,
string $fieldName): array
197 foreach ($hits as $hit) {
198 $returnPrices[] = (float)$hit[
'fields'][$fieldName][0];
201 return $returnPrices;
211 private function prepareBaseRequestQuery($from =
null, $to =
null): array
215 'type' => $this->clientConfig->getEntityType(),
223 'match_all' => new \stdClass(),
230 '_id' => $this->entityIds,
235 $this->fieldName => array_merge($from, $to),
249 return $requestQuery;
__construct(ConnectionManager $connectionManager, FieldMapperInterface $fieldMapper, Config $clientConfig, SearchIndexNameResolver $searchIndexNameResolver, string $fieldName, string $storeId, array $entityIds)
loadPrevious($data, $index, $lower=null)
loadNext($data, $rightIndex, $upper=null)
load($limit, $offset=null, $lower=null, $upper=null)