ltsecurityinc安防产品在线商城代码
This commit is contained in:
+152
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Model\Search\GetResponse;
|
||||
|
||||
use Amasty\ElasticSearch\Model\Search\DataProviderFactory;
|
||||
use Magento\CatalogSearch\Model\Indexer\Fulltext;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Search\Dynamic\Algorithm\Repository;
|
||||
use Magento\Framework\Search\Dynamic\EntityStorage;
|
||||
use Magento\Framework\Search\Dynamic\EntityStorageFactory;
|
||||
use Magento\Framework\Search\Request\BucketInterface;
|
||||
use Magento\Framework\Search\Request\Dimension;
|
||||
|
||||
class GetAggregations
|
||||
{
|
||||
/**
|
||||
* @var Repository
|
||||
*/
|
||||
private $dynamicAlgorithmRepository;
|
||||
|
||||
/**
|
||||
* @var EntityStorageFactory
|
||||
*/
|
||||
private $dynamicEntityStorageFactory;
|
||||
|
||||
/**
|
||||
* @var DataProviderFactory
|
||||
*/
|
||||
private $dataProviderFactory;
|
||||
|
||||
public function __construct(
|
||||
Repository $dynamicAlgorithmRepository,
|
||||
EntityStorageFactory $dynamicEntityStorageFactory,
|
||||
DataProviderFactory $dataProviderFactory
|
||||
) {
|
||||
$this->dynamicAlgorithmRepository = $dynamicAlgorithmRepository;
|
||||
$this->dynamicEntityStorageFactory = $dynamicEntityStorageFactory;
|
||||
$this->dataProviderFactory = $dataProviderFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Magento\Framework\Search\RequestInterface $request
|
||||
* @param array $elasticResponse
|
||||
* @return array
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function execute(\Magento\Framework\Search\RequestInterface $request, array $elasticResponse)
|
||||
{
|
||||
$aggregations = [];
|
||||
$buckets = $request->getAggregation();
|
||||
|
||||
foreach ($buckets as $bucket) {
|
||||
switch ($bucket->getType()) {
|
||||
case BucketInterface::TYPE_DYNAMIC:
|
||||
$aggregations[$bucket->getName()] = $this
|
||||
->getDynamicBucket($bucket, $request->getDimensions(), $elasticResponse);
|
||||
break;
|
||||
case BucketInterface::TYPE_TERM:
|
||||
$aggregations[$bucket->getName()] = $this->getTermBucket($bucket, $elasticResponse);
|
||||
break;
|
||||
default:
|
||||
throw new LocalizedException(__("Incorrect Bucket type: %1.", $bucket->getType()));
|
||||
}
|
||||
}
|
||||
|
||||
return $aggregations;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param BucketInterface $bucket
|
||||
* @param array $elasticResponse
|
||||
* @return array
|
||||
*/
|
||||
public function getTermBucket(BucketInterface $bucket, array $elasticResponse)
|
||||
{
|
||||
$values = [];
|
||||
foreach ($elasticResponse['aggregations'][$bucket->getName()]['buckets'] as $resultBucket) {
|
||||
$values[$resultBucket['key']] = [
|
||||
'value' => $resultBucket['key'],
|
||||
'count' => $resultBucket['doc_count'],
|
||||
];
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param BucketInterface $bucket
|
||||
* @param Dimension[] $dimensions
|
||||
* @param array $elasticResponse
|
||||
* @return array
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function getDynamicBucket(BucketInterface $bucket, array $dimensions, array $elasticResponse)
|
||||
{
|
||||
$dataProvider = $this->dataProviderFactory->create(
|
||||
Fulltext::INDEXER_ID,
|
||||
$bucket->getField()
|
||||
);
|
||||
$algorithm = $this->dynamicAlgorithmRepository->get(
|
||||
$bucket->getMethod(),
|
||||
['dataProvider' => $dataProvider]
|
||||
);
|
||||
$data = $algorithm->getItems(
|
||||
$bucket,
|
||||
$dimensions,
|
||||
$this->convertQuerytoEntity($elasticResponse)
|
||||
);
|
||||
return $this->processFromToData($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
private function processFromToData($data)
|
||||
{
|
||||
$result = [];
|
||||
foreach ($data as $value) {
|
||||
$from = is_numeric($value['from']) ? $value['from'] : '';
|
||||
$to = is_numeric($value['to']) ? $value['to'] : '';
|
||||
unset($value['from'], $value['to']);
|
||||
$fromToValue = "{$from}_{$to}";
|
||||
|
||||
// phpcs:ignore
|
||||
$result[$fromToValue] = array_merge(['value' => $fromToValue], $value);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $queryResult
|
||||
* @return EntityStorage
|
||||
*/
|
||||
private function convertQuerytoEntity(array $queryResult)
|
||||
{
|
||||
$documentIds = [];
|
||||
if (isset($queryResult['hits']['hits']) && !empty($queryResult['hits']['hits'])) {
|
||||
foreach ($queryResult['hits']['hits'] as $document) {
|
||||
$documentIds[] = $document['_id'];
|
||||
}
|
||||
}
|
||||
|
||||
return $this->dynamicEntityStorageFactory->create($documentIds);
|
||||
}
|
||||
}
|
||||
+287
@@ -0,0 +1,287 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Model\Search\GetResponse\GetAggregations;
|
||||
|
||||
use Amasty\ElasticSearch\Model\Search\Adapter;
|
||||
use Magento\Framework\Search\Dynamic\IntervalInterface;
|
||||
use Amasty\ElasticSearch\Model\Config;
|
||||
use Magento\CatalogSearch\Model\Indexer\Fulltext;
|
||||
use Amasty\ElasticSearch\Model\Client\ClientRepositoryInterface;
|
||||
|
||||
class Interval implements IntervalInterface
|
||||
{
|
||||
/**
|
||||
* Minimal possible value
|
||||
*/
|
||||
public const DELTA = 0.005;
|
||||
|
||||
/**
|
||||
* @var Config
|
||||
*/
|
||||
private $clientConfig;
|
||||
|
||||
/**
|
||||
* @var ClientRepositoryInterface
|
||||
*/
|
||||
private $clientRepository;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Registry
|
||||
*/
|
||||
private $registry;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $fieldName;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $storeId;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $entityIds;
|
||||
|
||||
public function __construct(
|
||||
Config $clientConfig,
|
||||
ClientRepositoryInterface $clientRepository,
|
||||
\Magento\Framework\Registry $registry,
|
||||
$fieldName,
|
||||
$storeId,
|
||||
$entityIds
|
||||
) {
|
||||
$this->clientConfig = $clientConfig;
|
||||
$this->clientRepository = $clientRepository;
|
||||
$this->registry = $registry;
|
||||
$this->fieldName = $fieldName;
|
||||
$this->storeId = $storeId;
|
||||
$this->entityIds = $entityIds;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
private function getFilterMustTerms()
|
||||
{
|
||||
$query = $this->registry->registry(Adapter::REQUEST_QUERY);
|
||||
if ($query && !empty($query['bool']['must'])) {
|
||||
$filterMustTerms = $query['bool']['must'];
|
||||
} else {
|
||||
$filterMustTerms = [
|
||||
[
|
||||
'terms' => [
|
||||
'_id' => $this->entityIds,
|
||||
],
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
return $filterMustTerms;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function load($limit, $offset = null, $lower = null, $upper = null)
|
||||
{
|
||||
$fromValue = ($lower) ? ['gte' => $lower - self::DELTA] : [];
|
||||
$toValue = ($upper) ? ['lt' => $upper - self::DELTA] : [];
|
||||
$client = $this->clientRepository->get();
|
||||
$range = [];
|
||||
|
||||
if ($lower !== null && $upper!== null) {
|
||||
$range = [
|
||||
[
|
||||
'range' => [
|
||||
$this->fieldName => array_merge($fromValue, $toValue),
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
$requestQuery = [
|
||||
'index' => $client->getIndexName(Fulltext::INDEXER_ID, $this->storeId),
|
||||
'type' => $this->clientConfig->getEntityType(),
|
||||
'body' => [
|
||||
'stored_fields' => [
|
||||
'_id'
|
||||
],
|
||||
'query' => [
|
||||
'bool' => [
|
||||
'must' => [
|
||||
'match_all' => ['boost' => 1]
|
||||
],
|
||||
'filter' => [
|
||||
'bool' => [
|
||||
'must' => array_merge($this->getFilterMustTerms(), $range),
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
'sort' => [
|
||||
$this->fieldName,
|
||||
],
|
||||
'size' => $limit,
|
||||
],
|
||||
];
|
||||
|
||||
if ($offset) {
|
||||
$requestQuery['body']['from'] = $offset;
|
||||
}
|
||||
|
||||
$queryResult = $client->search($requestQuery);
|
||||
|
||||
return $this->convertToFloat($queryResult['hits']['hits']);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function loadPrevious($data, $index, $lower = null)
|
||||
{
|
||||
$fromValue = ($lower) ? ['gte' => $lower - self::DELTA] : [];
|
||||
$toValue = ($data) ? ['lt' => $data - self::DELTA] : [];
|
||||
$client = $this->clientRepository->get();
|
||||
$range = [];
|
||||
|
||||
if ($fromValue || $toValue) {
|
||||
$range = [
|
||||
[
|
||||
'range' => [
|
||||
$this->fieldName => array_merge($fromValue, $toValue),
|
||||
],
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
$requestQuery = [
|
||||
'index' => $client->getIndexName(Fulltext::INDEXER_ID, $this->storeId),
|
||||
'type' => $this->clientConfig->getEntityType(),
|
||||
'body' => [
|
||||
'stored_fields' => [
|
||||
'_id'
|
||||
],
|
||||
'query' => [
|
||||
'bool' => [
|
||||
'must' => [
|
||||
'match_all' => ['boost' => 1]
|
||||
],
|
||||
'filter' => [
|
||||
'bool' => [
|
||||
'must' => array_merge($this->getFilterMustTerms(), $range),
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
'sort' => [
|
||||
$this->fieldName,
|
||||
],
|
||||
],
|
||||
];
|
||||
$queryResult = $client->search($requestQuery);
|
||||
|
||||
$offset = $queryResult['hits']['total']['value'] ?? $queryResult['hits']['total'] ?? 0;
|
||||
if (!$offset) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->load($index - $offset + 1, $offset - 1, $lower);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function loadNext($data, $rightIndex, $upper = null)
|
||||
{
|
||||
$fromValue = ($data) ? ['gte' => $data - self::DELTA] : [];
|
||||
$toValue = ($data) ? ['lt' => $data - self::DELTA] : [];
|
||||
$client = $this->clientRepository->get();
|
||||
$range = [];
|
||||
|
||||
if ($fromValue || $toValue) {
|
||||
$range = [
|
||||
'range' => [
|
||||
$this->fieldName => array_merge($fromValue, $toValue),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
$requestCountQuery = [
|
||||
'index' => $client->getIndexName(Fulltext::INDEXER_ID, $this->storeId),
|
||||
'type' => $this->clientConfig->getEntityType(),
|
||||
'body' => [
|
||||
'stored_fields' => [
|
||||
'_id'
|
||||
],
|
||||
'query' => [
|
||||
'bool' => [
|
||||
'must' => [
|
||||
'match_all' => ['boost' => 1]
|
||||
],
|
||||
'filter' => [
|
||||
'bool' => [
|
||||
'must' => [
|
||||
[
|
||||
'terms' => [
|
||||
'_id' => $this->entityIds,
|
||||
],
|
||||
],
|
||||
$range
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
'sort' => [
|
||||
$this->fieldName,
|
||||
],
|
||||
],
|
||||
];
|
||||
$queryCountResult = $client->search($requestCountQuery);
|
||||
|
||||
$offset = $queryCountResult['hits']['total']['value'] ?? $queryCountResult['hits']['total'] ?? 0;
|
||||
if (!$offset) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$fromValue = ['gte' => $data - self::DELTA];
|
||||
if ($upper !== null) {
|
||||
$toValue = ['lt' => $data - self::DELTA];
|
||||
}
|
||||
|
||||
$requestQuery = $requestCountQuery;
|
||||
$requestCountQuery['body']['query']['filtered']['filter']['bool']['must']['range'] =
|
||||
[$this->fieldName => array_merge($fromValue, $toValue)];
|
||||
|
||||
$requestCountQuery['body']['from'] = $offset - 1;
|
||||
$requestCountQuery['body']['size'] = $rightIndex - $offset + 1;
|
||||
|
||||
$queryResult = $this->clientRepository->get()->search($requestQuery);
|
||||
|
||||
return array_reverse($this->convertToFloat($queryResult['hits']['hits']));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $hits
|
||||
* @param string $fieldName
|
||||
*
|
||||
* @return float[]
|
||||
*/
|
||||
private function convertToFloat($hits)
|
||||
{
|
||||
$returnPrices = [];
|
||||
foreach ($hits as $hit) {
|
||||
$returnPrices[] = (float) $hit['sort'][0];
|
||||
}
|
||||
|
||||
return $returnPrices;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user