Skip to content

Commit

Permalink
Merge pull request #137 from AzizKHAN030/m244_catalog-graphql
Browse files Browse the repository at this point in the history
Passed the 6th param $includeDirectChildrenOnly class
  • Loading branch information
carinadues authored Jun 10, 2022
2 parents a0521aa + b0aace9 commit 4329227
Show file tree
Hide file tree
Showing 5 changed files with 249 additions and 7 deletions.
236 changes: 236 additions & 0 deletions src/Elasticsearch5/SearchAdapter/Aggregation/Interval.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace ScandiPWA\CatalogGraphQl\Elasticsearch5\SearchAdapter\Aggregation;

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;
use Magento\Elasticsearch\Elasticsearch5\SearchAdapter\Aggregation\Interval as CoreInterval;

/**
* Aggregate price intervals for search query result.
*/
class Interval extends CoreInterval
{
/**
* Minimal possible value
*/
const DELTA = 0.005;

/**
* @var ConnectionManager
*/
protected $connectionManager;

/**
* @var FieldMapperInterface
*/
protected $fieldMapper;

/**
* @var Config
*/
protected $clientConfig;

/**
* @var string
*/
protected $fieldName;

/**
* @var string
*/
protected $storeId;

/**
* @var array
*/
protected $entityIds;

/**
* @var SearchIndexNameResolver
*/
protected $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,
string $fieldName,
string $storeId,
array $entityIds
) {
$this->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;
}
}
6 changes: 5 additions & 1 deletion src/Model/Layer/Filter/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,21 +41,24 @@ class Category extends OriginalCategoryBuilder
* @param RootCategoryProvider $rootCategoryProvider
* @param ResourceConnection $resourceConnection
* @param LayerFormatter $layerFormatter
* @param IncludeDirectChildrenOnly $includeDirectChildrenOnly
*/
public function __construct(
CategoryAttributeQuery $categoryAttributeQuery,
CategoryAttributesMapper $attributesMapper,
RootCategoryProvider $rootCategoryProvider,
ResourceConnection $resourceConnection,
LayerFormatter $layerFormatter,
IncludeDirectChildrenOnly $includeDirectChildrenOnly,
AttributeDataProvider $attributeDataProvider
) {
parent::__construct(
$categoryAttributeQuery,
$attributesMapper,
$rootCategoryProvider,
$resourceConnection,
$layerFormatter
$layerFormatter,
$includeDirectChildrenOnly
);

$this->attributeDataProvider = $attributeDataProvider;
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Resolver/Category/SortFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@
<preference for="Magento\GroupedProductGraphQl\Model\Resolver\GroupedItems"
type="ScandiPWA\CatalogGraphQl\Model\Resolver\GroupedItems" />

<preference for="Magento\Elasticsearch\SearchAdapter\Query\Builder\Match"
type="ScandiPWA\CatalogGraphQl\SearchAdapter\Query\Builder\Match"/>
<preference for="Magento\Elasticsearch\SearchAdapter\Query\Builder\MatchQuery"
type="ScandiPWA\CatalogGraphQl\SearchAdapter\Query\Builder\MatchQuery"/>

<type name="Magento\Framework\GraphQl\Query\QueryComplexityLimiter">
<arguments>
Expand Down Expand Up @@ -105,4 +105,6 @@
<plugin name="aggregation_equalize_products_count_handler"
type="ScandiPWA\CatalogGraphQl\Plugin\SearchAdapter\Aggregation\BuilderPlugin" />
</type>
<preference for="Magento\Elasticsearch\Elasticsearch5\SearchAdapter\Aggregation\Interval"
type="ScandiPWA\CatalogGraphQl\Elasticsearch5\SearchAdapter\Aggregation\Interval" />
</config>

0 comments on commit 4329227

Please sign in to comment.