diff --git a/src/Elasticsearch5/SearchAdapter/Aggregation/Interval.php b/src/Elasticsearch5/SearchAdapter/Aggregation/Interval.php new file mode 100644 index 0000000..be9b121 --- /dev/null +++ b/src/Elasticsearch5/SearchAdapter/Aggregation/Interval.php @@ -0,0 +1,236 @@ +connectionManager = $connectionManager; + $this->fieldMapper = $fieldMapper; + $this->clientConfig = $clientConfig; + $this->fieldName = $fieldName; + $this->storeId = $storeId; + $this->entityIds = $entityIds; + $this->searchIndexNameResolver = $searchIndexNameResolver; + + parent::__construct( + $connectionManager, + $fieldMapper, + $clientConfig, + $searchIndexNameResolver, + $fieldName, + $storeId, + $entityIds, + + ); + } + + /** + * @inheritdoc + */ + public function load($limit, $offset = null, $lower = null, $upper = null) + { + + $from = ['gte' => 0]; //Added this because in some situations the $lower is null and $from is not declared + $to = ['lt' => 0]; //Added this because in some situations the $data is null and $to is not declared + + if ($lower) { + $from = ['gte' => $lower - self::DELTA]; + } + + if ($upper) { + $to = ['lt' => $upper - self::DELTA]; + } + + $requestQuery = $this->prepareBaseRequestQuery($from, $to); + $requestQuery = array_merge_recursive( + $requestQuery, + ['body' => ['stored_fields' => [$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) + { + + $from = ['gte' => 0]; //Added this because in some situations the $lower is null and $from is not declared + $to = ['lt' => 0]; //Added this because in some situations the $data is null and $to is not declared + + if ($lower) { + $from = ['gte' => $lower - self::DELTA]; + } + if ($data) { + $to = ['lt' => $data - self::DELTA]; + } + + $requestQuery = $this->prepareBaseRequestQuery($from, $to); + $requestQuery = array_merge_recursive( + $requestQuery, + ['size' => 0] + ); + + $queryResult = $this->connectionManager->getConnection() + ->query($requestQuery); + + $offset = $queryResult['hits']['total']; + if (!$offset) { + return false; + } + + if (is_array($offset)) { + $offset = $offset['value']; + } + + return $this->load($index - $offset + 1, $offset - 1, $lower); + } + + /** + * Conver array values to float type. + * + * @param array $hits + * @param string $fieldName + * + * @return float[] + */ + protected function arrayValuesToFloat(array $hits, string $fieldName): array + { + $returnPrices = []; + foreach ($hits as $hit) { + $returnPrices[] = (float)$hit['fields'][$fieldName][0]; + } + + return $returnPrices; + } + + /** + * Prepare base query for search. + * + * @param array|null $from + * @param array|null $to + * @return array + */ + protected function prepareBaseRequestQuery($from = null, $to = null): array + { + $requestQuery = [ + 'index' => $this->searchIndexNameResolver->getIndexName($this->storeId, Fulltext::INDEXER_ID), + 'type' => $this->clientConfig->getEntityType(), + 'body' => [ + 'stored_fields' => [ + '_id', + ], + 'query' => [ + 'bool' => [ + 'must' => [ + 'match_all' => new \stdClass(), + ], + 'filter' => [ + 'bool' => [ + 'must' => [ + [ + 'terms' => [ + '_id' => $this->entityIds, + ], + ], + [ + 'range' => [ + $this->fieldName => array_merge($from, $to), + ], + ], + ], + ], + ], + ], + ], + 'sort' => [ + $this->fieldName, + ], + ], + ]; + + return $requestQuery; + } +} diff --git a/src/Model/Layer/Filter/Category.php b/src/Model/Layer/Filter/Category.php index 0b7da59..72dbeaf 100644 --- a/src/Model/Layer/Filter/Category.php +++ b/src/Model/Layer/Filter/Category.php @@ -15,6 +15,7 @@ use Magento\CatalogGraphQl\DataProvider\CategoryAttributesMapper; use Magento\CatalogGraphQl\DataProvider\Category\Query\CategoryAttributeQuery; +use Magento\CatalogGraphQl\DataProvider\Product\LayeredNavigation\Builder\Aggregations\Category\IncludeDirectChildrenOnly; use Magento\CatalogGraphQl\DataProvider\Product\LayeredNavigation\RootCategoryProvider; use Magento\Framework\Api\Search\AggregationInterface; use Magento\Framework\App\ResourceConnection; @@ -40,6 +41,7 @@ class Category extends OriginalCategoryBuilder * @param RootCategoryProvider $rootCategoryProvider * @param ResourceConnection $resourceConnection * @param LayerFormatter $layerFormatter + * @param IncludeDirectChildrenOnly $includeDirectChildrenOnly */ public function __construct( CategoryAttributeQuery $categoryAttributeQuery, @@ -47,6 +49,7 @@ public function __construct( RootCategoryProvider $rootCategoryProvider, ResourceConnection $resourceConnection, LayerFormatter $layerFormatter, + IncludeDirectChildrenOnly $includeDirectChildrenOnly, AttributeDataProvider $attributeDataProvider ) { parent::__construct( @@ -54,7 +57,8 @@ public function __construct( $attributesMapper, $rootCategoryProvider, $resourceConnection, - $layerFormatter + $layerFormatter, + $includeDirectChildrenOnly ); $this->attributeDataProvider = $attributeDataProvider; diff --git a/src/Model/Resolver/Category/SortFields.php b/src/Model/Resolver/Category/SortFields.php index d994dd9..21b1c93 100644 --- a/src/Model/Resolver/Category/SortFields.php +++ b/src/Model/Resolver/Category/SortFields.php @@ -115,7 +115,7 @@ private function getDefaultSortOption($context): string { private function getSortOptionsByCategory(int $categoryId): array { $result = []; $category = $this->categoryRepository->get($categoryId); - $sortBy = $category->getAvailableSortBy(); + $sortBy = $category->getAvailableSortBy() ?? []; foreach ($sortBy as $sortItem) { $result[] = [ diff --git a/src/SearchAdapter/Query/Builder/Match.php b/src/SearchAdapter/Query/Builder/MatchQuery.php similarity index 97% rename from src/SearchAdapter/Query/Builder/Match.php rename to src/SearchAdapter/Query/Builder/MatchQuery.php index f603d7f..90a3e64 100644 --- a/src/SearchAdapter/Query/Builder/Match.php +++ b/src/SearchAdapter/Query/Builder/MatchQuery.php @@ -15,13 +15,13 @@ use Magento\Elasticsearch\Model\Config; use Magento\Elasticsearch\SearchAdapter\Query\ValueTransformerPool; use Magento\Elasticsearch\Model\Adapter\FieldMapperInterface; -use Magento\Elasticsearch\SearchAdapter\Query\Builder\Match as CoreMatch; +use Magento\Elasticsearch\SearchAdapter\Query\Builder\MatchQuery as CoreMatch; /** - * Class Match + * Class MatchQuery * @package ScandiPWA\CatalogGraphQl\SearchAdapter\Query\Builder */ -class Match extends CoreMatch +class MatchQuery extends CoreMatch { /** * Define fuzziness level of search query diff --git a/src/etc/di.xml b/src/etc/di.xml index 167217f..ab69c44 100755 --- a/src/etc/di.xml +++ b/src/etc/di.xml @@ -65,8 +65,8 @@ - + @@ -105,4 +105,6 @@ +