diff --git a/vendor/magento/module-catalog-graph-ql/Model/Resolver/Products/Query/Search.php b/vendor/magento/module-catalog-graph-ql/Model/Resolver/Products/Query/Search.php index 221a402cb2fff..7b4d1b78e9a5b 100644 --- a/vendor/magento/module-catalog-graph-ql/Model/Resolver/Products/Query/Search.php +++ b/vendor/magento/module-catalog-graph-ql/Model/Resolver/Products/Query/Search.php @@ -9,6 +9,7 @@ use Magento\CatalogGraphQl\DataProvider\Product\SearchCriteriaBuilder; use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\ProductSearch; +use Magento\CatalogGraphQl\Model\Resolver\Products\Query\Search\QueryPopularity; use Magento\CatalogGraphQl\Model\Resolver\Products\SearchResult; use Magento\CatalogGraphQl\Model\Resolver\Products\SearchResultFactory; use Magento\Framework\Api\Search\SearchCriteriaInterface; @@ -22,6 +23,8 @@ /** * Full text search for catalog using given search criteria. + * + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class Search implements ProductQueryInterface { @@ -60,6 +63,11 @@ class Search implements ProductQueryInterface */ private $searchCriteriaBuilder; + /** + * @var QueryPopularity + */ + private $queryPopularity; + /** * @param SearchInterface $search * @param SearchResultFactory $searchResultFactory @@ -68,6 +76,7 @@ class Search implements ProductQueryInterface * @param ProductSearch $productsProvider * @param SearchCriteriaBuilder $searchCriteriaBuilder * @param ArgumentsProcessorInterface|null $argsSelection + * @param QueryPopularity|null $queryPopularity */ public function __construct( SearchInterface $search, @@ -76,7 +85,8 @@ public function __construct( FieldSelection $fieldSelection, ProductSearch $productsProvider, SearchCriteriaBuilder $searchCriteriaBuilder, - ArgumentsProcessorInterface $argsSelection = null + ArgumentsProcessorInterface $argsSelection = null, + QueryPopularity $queryPopularity = null ) { $this->search = $search; $this->searchResultFactory = $searchResultFactory; @@ -84,8 +94,8 @@ public function __construct( $this->fieldSelection = $fieldSelection; $this->productsProvider = $productsProvider; $this->searchCriteriaBuilder = $searchCriteriaBuilder; - $this->argsSelection = $argsSelection ?: ObjectManager::getInstance() - ->get(ArgumentsProcessorInterface::class); + $this->argsSelection = $argsSelection ?: ObjectManager::getInstance()->get(ArgumentsProcessorInterface::class); + $this->queryPopularity = $queryPopularity ?: ObjectManager::getInstance()->get(QueryPopularity::class); } /** @@ -124,6 +134,11 @@ public function getResult( $totalPages = $realPageSize ? ((int)ceil($searchResults->getTotalCount() / $realPageSize)) : 0; + // add query statistics data + if (!empty($args['search'])) { + $this->queryPopularity->execute($context, $args['search'], (int) $searchResults->getTotalCount()); + } + $productArray = []; /** @var \Magento\Catalog\Model\Product $product */ foreach ($searchResults->getItems() as $product) { diff --git a/vendor/magento/module-catalog-graph-ql/Model/Resolver/Products/Query/Search/QueryPopularity.php b/vendor/magento/module-catalog-graph-ql/Model/Resolver/Products/Query/Search/QueryPopularity.php new file mode 100644 index 0000000000000..164c521028200 --- /dev/null +++ b/vendor/magento/module-catalog-graph-ql/Model/Resolver/Products/Query/Search/QueryPopularity.php @@ -0,0 +1,66 @@ +queryFactory = $queryFactory; + $this->string = $string; + } + + /** + * Fill the query popularity + * + * @param ContextInterface $context + * @param string $queryText + * @param int $numResults + * @return void + * @throws NoSuchEntityException + * @throws LocalizedException + */ + public function execute(ContextInterface $context, string $queryText, int $numResults) : void + { + $query = $this->queryFactory->create(); + $maxQueryLength = (int) $query->getMaxQueryLength(); + if ($maxQueryLength && $this->string->strlen($queryText) > $maxQueryLength) { + $queryText = $this->string->substr($queryText, 0, $maxQueryLength); + } + $query->setQueryText($queryText); + $store = $context->getExtensionAttributes()->getStore(); + $query->setStoreId($store->getId()); + $query->saveIncrementalPopularity(); + $query->saveNumResults($numResults); + } +}