123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Elasticsearch\SearchAdapter\Aggregation;
- use Magento\Framework\Search\Dynamic\IntervalInterface;
- use Magento\Elasticsearch\SearchAdapter\ConnectionManager;
- use Magento\Elasticsearch\Model\Adapter\FieldMapperInterface;
- use Magento\Elasticsearch\Model\Config;
- use Magento\Elasticsearch\SearchAdapter\SearchIndexNameResolver;
- use Magento\CatalogSearch\Model\Indexer\Fulltext;
- class Interval implements IntervalInterface
- {
- /**
- * Minimal possible value
- */
- const DELTA = 0.005;
- /**
- * @var ConnectionManager
- */
- protected $connectionManager;
- /**
- * @var FieldMapperInterface
- */
- protected $fieldMapper;
- /**
- * @var Config
- */
- protected $clientConfig;
- /**
- * @var string
- */
- private $fieldName;
- /**
- * @var string
- */
- private $storeId;
- /**
- * @var array
- */
- private $entityIds;
- /**
- * @var SearchIndexNameResolver
- */
- private $searchIndexNameResolver;
- /**
- * @param ConnectionManager $connectionManager
- * @param FieldMapperInterface $fieldMapper
- * @param Config $clientConfig
- * @param SearchIndexNameResolver $searchIndexNameResolver
- * @param string $fieldName
- * @param string $storeId
- * @param array $entityIds
- */
- public function __construct(
- ConnectionManager $connectionManager,
- FieldMapperInterface $fieldMapper,
- Config $clientConfig,
- SearchIndexNameResolver $searchIndexNameResolver,
- $fieldName,
- $storeId,
- $entityIds
- ) {
- $this->connectionManager = $connectionManager;
- $this->fieldMapper = $fieldMapper;
- $this->clientConfig = $clientConfig;
- $this->fieldName = $fieldName;
- $this->storeId = $storeId;
- $this->entityIds = $entityIds;
- $this->searchIndexNameResolver = $searchIndexNameResolver;
- }
- /**
- * {@inheritdoc}
- */
- public function load($limit, $offset = null, $lower = null, $upper = null)
- {
- $from = $to = [];
- if ($lower) {
- $from = ['gte' => $lower - self::DELTA];
- }
- if ($upper) {
- $to = ['lt' => $upper - self::DELTA];
- }
- $requestQuery = [
- 'index' => $this->searchIndexNameResolver->getIndexName($this->storeId, Fulltext::INDEXER_ID),
- 'type' => $this->clientConfig->getEntityType(),
- 'body' => [
- 'fields' => [
- '_id',
- $this->fieldName,
- ],
- 'query' => [
- 'filtered' => [
- 'query' => [
- 'match_all' => [],
- ],
- 'filter' => [
- 'bool' => [
- 'must' => [
- [
- 'terms' => [
- '_id' => $this->entityIds,
- ],
- ],
- [
- 'range' => [
- $this->fieldName => array_merge($from, $to),
- ],
- ],
- ],
- ],
- ],
- ],
- ],
- 'sort' => [
- $this->fieldName,
- ],
- 'size' => $limit,
- ],
- ];
- if ($offset) {
- $requestQuery['body']['from'] = $offset;
- }
- $queryResult = $this->connectionManager->getConnection()
- ->query($requestQuery);
- return $this->arrayValuesToFloat($queryResult['hits']['hits'], $this->fieldName);
- }
- /**
- * {@inheritdoc}
- */
- public function loadPrevious($data, $index, $lower = null)
- {
- if ($lower) {
- $from = ['gte' => $lower - self::DELTA];
- }
- if ($data) {
- $to = ['lt' => $data - self::DELTA];
- }
- $requestQuery = [
- 'index' => $this->searchIndexNameResolver->getIndexName($this->storeId, Fulltext::INDEXER_ID),
- 'type' => $this->clientConfig->getEntityType(),
- 'search_type' => 'count',
- 'body' => [
- 'fields' => [
- '_id'
- ],
- 'query' => [
- 'filtered' => [
- 'query' => [
- 'match_all' => [],
- ],
- 'filter' => [
- 'bool' => [
- 'must' => [
- [
- 'terms' => [
- '_id' => $this->entityIds,
- ],
- ],
- [
- 'range' => [
- $this->fieldName => array_merge($from, $to),
- ],
- ],
- ],
- ],
- ],
- ],
- ],
- 'sort' => [
- $this->fieldName,
- ],
- ],
- ];
- $queryResult = $this->connectionManager->getConnection()
- ->query($requestQuery);
- $offset = $queryResult['hits']['total'];
- if (!$offset) {
- return false;
- }
- return $this->load($index - $offset + 1, $offset - 1, $lower);
- }
- /**
- * {@inheritdoc}
- */
- public function loadNext($data, $rightIndex, $upper = null)
- {
- $from = ['gt' => $data + self::DELTA];
- $to = ['lt' => $data - self::DELTA];
- $requestCountQuery = [
- 'index' => $this->searchIndexNameResolver->getIndexName($this->storeId, Fulltext::INDEXER_ID),
- 'type' => $this->clientConfig->getEntityType(),
- 'search_type' => 'count',
- 'body' => [
- 'fields' => [
- '_id'
- ],
- 'query' => [
- 'filtered' => [
- 'query' => [
- 'match_all' => [],
- ],
- 'filter' => [
- 'bool' => [
- 'must' => [
- [
- 'terms' => [
- '_id' => $this->entityIds,
- ],
- ],
- [
- 'range' => [
- $this->fieldName => array_merge($from, $to),
- ],
- ],
- ],
- ],
- ],
- ],
- ],
- 'sort' => [
- $this->fieldName,
- ],
- ],
- ];
- $queryCountResult = $this->connectionManager->getConnection()
- ->query($requestCountQuery);
- $offset = $queryCountResult['hits']['total'];
- if (!$offset) {
- return false;
- }
- $from = ['gte' => $data - self::DELTA];
- if ($upper !== null) {
- $to = ['lt' => $data - self::DELTA];
- }
- $requestQuery = $requestCountQuery;
- $requestCountQuery['body']['query']['filtered']['filter']['bool']['must']['range'] =
- [$this->fieldName => array_merge($from, $to)];
- $requestCountQuery['body']['from'] = $offset - 1;
- $requestCountQuery['body']['size'] = $rightIndex - $offset + 1;
- $queryResult = $this->connectionManager->getConnection()
- ->query($requestQuery);
- return array_reverse($this->arrayValuesToFloat($queryResult['hits']['hits'], $this->fieldName));
- }
- /**
- * @param array $hits
- * @param string $fieldName
- *
- * @return float[]
- */
- private function arrayValuesToFloat($hits, $fieldName)
- {
- $returnPrices = [];
- foreach ($hits as $hit) {
- $returnPrices[] = (float) $hit['fields'][$fieldName][0];
- }
- return $returnPrices;
- }
- }
|