Skip to content

Commit 511a7ed

Browse files
author
Oleksandr Iegorov
committed
ACP2E-68: Search terms queries via graphQL are not populated in popular search terms in admin
1 parent c746dcc commit 511a7ed

File tree

2 files changed

+75
-15
lines changed

2 files changed

+75
-15
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Products/Query/Search.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
use Magento\CatalogGraphQl\Model\Resolver\Products\SearchResultFactory;
1414
use Magento\Framework\Api\Search\SearchCriteriaInterface;
1515
use Magento\Framework\App\ObjectManager;
16+
use Magento\Framework\Exception\LocalizedException;
1617
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1718
use Magento\Framework\GraphQl\Query\Resolver\ArgumentsProcessorInterface;
1819
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1920
use Magento\GraphQl\Model\Query\ContextInterface;
2021
use Magento\Search\Api\SearchInterface;
2122
use Magento\Search\Model\Search\PageSizeProvider;
22-
use Magento\Search\Model\QueryFactory;
23+
use Magento\CatalogGraphQl\Model\Resolver\Products\Query\Search\QueryPopularity;
2324

2425
/**
2526
* Full text search for catalog using given search criteria.
@@ -62,9 +63,9 @@ class Search implements ProductQueryInterface
6263
private $searchCriteriaBuilder;
6364

6465
/**
65-
* @var QueryFactory
66+
* @var QueryPopularity
6667
*/
67-
private $queryFactory;
68+
private $queryPopularity;
6869

6970
/**
7071
* @param SearchInterface $search
@@ -74,7 +75,7 @@ class Search implements ProductQueryInterface
7475
* @param ProductSearch $productsProvider
7576
* @param SearchCriteriaBuilder $searchCriteriaBuilder
7677
* @param ArgumentsProcessorInterface|null $argsSelection
77-
* @param QueryFactory|null $queryFactory
78+
* @param QueryPopularity|null $queryPopularity
7879
*/
7980
public function __construct(
8081
SearchInterface $search,
@@ -84,18 +85,16 @@ public function __construct(
8485
ProductSearch $productsProvider,
8586
SearchCriteriaBuilder $searchCriteriaBuilder,
8687
ArgumentsProcessorInterface $argsSelection = null,
87-
QueryFactory $queryFactory = null
88+
QueryPopularity $queryPopularity = null
8889
) {
8990
$this->search = $search;
9091
$this->searchResultFactory = $searchResultFactory;
9192
$this->pageSizeProvider = $pageSize;
9293
$this->fieldSelection = $fieldSelection;
9394
$this->productsProvider = $productsProvider;
9495
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
95-
$this->argsSelection = $argsSelection ?: ObjectManager::getInstance()
96-
->get(ArgumentsProcessorInterface::class);
97-
$this->queryFactory = $queryFactory ?: ObjectManager::getInstance()
98-
->get(QueryFactory::class);
96+
$this->argsSelection = $argsSelection ?: ObjectManager::getInstance()->get(ArgumentsProcessorInterface::class);
97+
$this->queryPopularity = $queryPopularity ?: ObjectManager::getInstance()->get(QueryPopularity::class);
9998
}
10099

101100
/**
@@ -136,12 +135,7 @@ public function getResult(
136135

137136
// add query statistics data
138137
if (!empty($args['search'])) {
139-
$query = $this->queryFactory->get();
140-
$query->setQueryText($args['search']);
141-
$store = $context->getExtensionAttributes()->getStore();
142-
$query->setStoreId($store->getId());
143-
$query->saveIncrementalPopularity();
144-
$query->saveNumResults($searchResults->getTotalCount());
138+
$this->queryPopularity->execute($context, $args['search'], (int) $searchResults->getTotalCount());
145139
}
146140

147141
$productArray = [];
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogGraphQl\Model\Resolver\Products\Query\Search;
9+
10+
use Magento\Framework\Stdlib\StringUtils as StdlibString;
11+
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\GraphQl\Model\Query\ContextInterface;
14+
use Magento\Search\Model\QueryFactory;
15+
16+
/**
17+
* Query statics handler
18+
*/
19+
class QueryPopularity
20+
{
21+
/**
22+
* @var QueryFactory
23+
*/
24+
private $queryFactory;
25+
26+
/**
27+
* @var StdlibString
28+
*/
29+
private $string;
30+
31+
/**
32+
* @param QueryFactory $queryFactory
33+
* @param StdlibString $string
34+
*/
35+
public function __construct(
36+
QueryFactory $queryFactory,
37+
StdlibString $string
38+
) {
39+
$this->queryFactory = $queryFactory;
40+
$this->string = $string;
41+
}
42+
43+
/**
44+
* Fill the query popularity
45+
*
46+
* @param ContextInterface $context
47+
* @param string $queryText
48+
* @param int $numResults
49+
* @return void
50+
* @throws NoSuchEntityException
51+
* @throws LocalizedException
52+
*/
53+
public function execute(ContextInterface $context, string $queryText, int $numResults) : void
54+
{
55+
$query = $this->queryFactory->create();
56+
$maxQueryLength = (int) $query->getMaxQueryLength();
57+
if ($maxQueryLength && $this->string->strlen($queryText) > $maxQueryLength) {
58+
$queryText = $this->string->substr($queryText, 0, $maxQueryLength);
59+
}
60+
$query->loadByQueryText($queryText);
61+
$store = $context->getExtensionAttributes()->getStore();
62+
$query->setStoreId($store->getId());
63+
$query->saveIncrementalPopularity();
64+
$query->saveNumResults($numResults);
65+
}
66+
}

0 commit comments

Comments
 (0)