Skip to content

Commit 4e32e53

Browse files
committed
MAGETWO-75769: Cache Popular Search Terms and Their Search Results Pages
1 parent f18377b commit 4e32e53

File tree

11 files changed

+497
-19
lines changed

11 files changed

+497
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CatalogSearch\Block;
7+
8+
use Magento\Framework\View\Element\Template;
9+
use Magento\Framework\View\Element\Template\Context;
10+
use Magento\Search\Model\QueryFactory;
11+
12+
/**
13+
* Block for logging search terms on cached pages
14+
*/
15+
class SearchTermsLog extends Template
16+
{
17+
/**
18+
* @var QueryFactory
19+
*/
20+
private $queryFactory;
21+
22+
/**
23+
* @var \Magento\Framework\App\ResponseInterface
24+
*/
25+
private $response;
26+
27+
/**
28+
* @param Context $context
29+
* @param QueryFactory $queryFactory
30+
* @param array $data
31+
*/
32+
public function __construct(
33+
Context $context,
34+
QueryFactory $queryFactory,
35+
\Magento\Framework\App\ResponseInterface $response,
36+
array $data = []
37+
) {
38+
$this->queryFactory = $queryFactory;
39+
$this->response = $response;
40+
parent::__construct($context, $data);
41+
}
42+
43+
/**
44+
* Retrieve query model object
45+
*
46+
* @return \Magento\Search\Model\Query
47+
*/
48+
public function getQuery()
49+
{
50+
return $this->queryFactory->get();
51+
}
52+
53+
/**
54+
* Insert ajax block for logging search terms on cached pages
55+
*
56+
* @return bool
57+
*/
58+
public function isAjaxInsert()
59+
{
60+
$pragma = $this->response->getHeader('pragma')->getFieldValue();
61+
return ($pragma == 'cache');
62+
}
63+
}

app/code/Magento/CatalogSearch/Controller/Result/Index.php

+70-15
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
use Magento\Catalog\Model\Layer\Resolver;
1010
use Magento\Catalog\Model\Session;
1111
use Magento\Framework\App\Action\Context;
12-
use Magento\Framework\App\ResourceConnection;
1312
use Magento\Store\Model\StoreManagerInterface;
1413
use Magento\Search\Model\QueryFactory;
14+
use Magento\Search\Model\PopularSearchTerms;
1515

1616
class Index extends \Magento\Framework\App\Action\Action
1717
{
@@ -64,34 +64,89 @@ public function __construct(
6464
* Display search result
6565
*
6666
* @return void
67+
*
68+
* @throws \Magento\Framework\Exception\LocalizedException
6769
*/
6870
public function execute()
6971
{
7072
$this->layerResolver->create(Resolver::CATALOG_LAYER_SEARCH);
73+
7174
/* @var $query \Magento\Search\Model\Query */
7275
$query = $this->_queryFactory->get();
7376

74-
$query->setStoreId($this->_storeManager->getStore()->getId());
77+
$storeId = $this->_storeManager->getStore()->getId();
78+
$query->setStoreId($storeId);
79+
80+
$queryText = $query->getQueryText();
81+
82+
if ($queryText != '') {
83+
$catalogSearchHelper = $this->_objectManager->get(\Magento\CatalogSearch\Helper\Data::class);
7584

76-
if ($query->getQueryText() != '') {
77-
if ($this->_objectManager->get(\Magento\CatalogSearch\Helper\Data::class)->isMinQueryLength()) {
78-
$query->setId(0)->setIsActive(1)->setIsProcessed(1);
85+
$getAdditionalRequestParameters = $this->getRequest()->getParams();
86+
unset($getAdditionalRequestParameters[$this->_queryFactory::QUERY_VAR_NAME]);
87+
88+
if (
89+
empty($getAdditionalRequestParameters) &&
90+
$this->_objectManager->get(PopularSearchTerms::class)->isCacheable($queryText, $storeId)
91+
) {
92+
$this->getCacheableResult($catalogSearchHelper, $query);
7993
} else {
80-
$query->saveIncrementalPopularity();
94+
$this->getNotCacheableResult($catalogSearchHelper, $query);
95+
}
96+
} else {
97+
$this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
98+
}
99+
}
81100

82-
$redirect = $query->getRedirect();
83-
if ($redirect && $this->_url->getCurrentUrl() !== $redirect) {
84-
$this->getResponse()->setRedirect($redirect);
85-
return;
86-
}
101+
/**
102+
* Return cacheable result
103+
*
104+
* @param \Magento\CatalogSearch\Helper\Data $catalogSearchHelper
105+
* @param \Magento\Search\Model\Query $query
106+
* @return void
107+
*/
108+
private function getCacheableResult($catalogSearchHelper, $query)
109+
{
110+
if (!$catalogSearchHelper->isMinQueryLength()) {
111+
$redirect = $query->getRedirect();
112+
if ($redirect && $this->_url->getCurrentUrl() !== $redirect) {
113+
$this->getResponse()->setRedirect($redirect);
114+
return;
87115
}
116+
}
88117

89-
$this->_objectManager->get(\Magento\CatalogSearch\Helper\Data::class)->checkNotes();
118+
$catalogSearchHelper->checkNotes();
119+
120+
$this->_view->loadLayout();
121+
$this->_view->renderLayout();
122+
}
90123

91-
$this->_view->loadLayout();
92-
$this->_view->renderLayout();
124+
/**
125+
* Return not cacheable result
126+
*
127+
* @param \Magento\CatalogSearch\Helper\Data $catalogSearchHelper
128+
* @param \Magento\Search\Model\Query $query
129+
* @return void
130+
*
131+
* @throws \Magento\Framework\Exception\LocalizedException
132+
*/
133+
private function getNotCacheableResult($catalogSearchHelper, $query)
134+
{
135+
if ($catalogSearchHelper->isMinQueryLength()) {
136+
$query->setId(0)->setIsActive(1)->setIsProcessed(1);
93137
} else {
94-
$this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
138+
$query->saveIncrementalPopularity();
139+
$redirect = $query->getRedirect();
140+
if ($redirect && $this->_url->getCurrentUrl() !== $redirect) {
141+
$this->getResponse()->setRedirect($redirect);
142+
return;
143+
}
95144
}
145+
146+
$catalogSearchHelper->checkNotes();
147+
148+
$this->_view->loadLayout();
149+
$this->getResponse()->setNoCacheHeaders();
150+
$this->_view->renderLayout();
96151
}
97152
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
namespace Magento\CatalogSearch\Controller\SearchTermsLog;
8+
9+
use Magento\Framework\App\Action\Context;
10+
use Magento\Store\Model\StoreManagerInterface;
11+
use Magento\Search\Model\QueryFactory;
12+
use Magento\Framework\Controller\Result\JsonFactory;
13+
use Magento\CatalogSearch\Helper\Data as HelperData;
14+
use Magento\Framework\Controller\Result\Json;
15+
16+
/**
17+
* Controller for save search terms
18+
*/
19+
class Save extends \Magento\Framework\App\Action\Action
20+
{
21+
/**
22+
* @var StoreManagerInterface
23+
*/
24+
private $storeManager;
25+
26+
/**
27+
* Catalog search helper
28+
*
29+
* @var HelperData
30+
*/
31+
private $catalogSearchHelper;
32+
33+
/**
34+
* @var QueryFactory
35+
*/
36+
private $queryFactory;
37+
38+
/**
39+
* @var JsonFactory
40+
*/
41+
private $resultJsonFactory;
42+
43+
/**
44+
* @param Context $context
45+
* @param HelperData $catalogSearchHelper
46+
* @param StoreManagerInterface $storeManager
47+
* @param QueryFactory $queryFactory
48+
* @param JsonFactory $resultJsonFactory
49+
*/
50+
public function __construct(
51+
Context $context,
52+
HelperData $catalogSearchHelper,
53+
StoreManagerInterface $storeManager,
54+
QueryFactory $queryFactory,
55+
JsonFactory $resultJsonFactory
56+
) {
57+
parent::__construct($context);
58+
$this->storeManager = $storeManager;
59+
$this->catalogSearchHelper = $catalogSearchHelper;
60+
$this->queryFactory = $queryFactory;
61+
$this->resultJsonFactory = $resultJsonFactory;
62+
}
63+
64+
/**
65+
* Save search term
66+
*
67+
* @return Json
68+
*/
69+
public function execute()
70+
{
71+
/* @var $query \Magento\Search\Model\Query */
72+
$query = $this->queryFactory->get();
73+
74+
$query->setStoreId($this->storeManager->getStore()->getId());
75+
76+
if ($query->getQueryText() != '') {
77+
try {
78+
if ($this->catalogSearchHelper->isMinQueryLength()) {
79+
$query->setId(0)->setIsActive(1)->setIsProcessed(1);
80+
} else {
81+
$query->saveIncrementalPopularity();
82+
}
83+
$responseContent = ['success' => true, 'error_message' => ''];
84+
} catch (\Magento\Framework\Exception\LocalizedException $e) {
85+
$responseContent = ['success' => false, 'error_message' => $e];
86+
}
87+
} else {
88+
$responseContent = ['success' => false, 'error_message' => __('Search term is empty')];
89+
}
90+
91+
/** @var Json $resultJson */
92+
$resultJson = $this->resultJsonFactory->create();
93+
return $resultJson->setData($responseContent);
94+
}
95+
}

app/code/Magento/CatalogSearch/etc/adminhtml/system.xml

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
<label>Maximum Query Length</label>
2828
<validate>validate-digits</validate>
2929
</field>
30+
<field id="max_count_cacheable_search_terms" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
31+
<label>Maximum Count Cacheable Search Terms</label>
32+
<validate>validate-digits</validate>
33+
</field>
3034
</group>
3135
</section>
3236
</system>

app/code/Magento/CatalogSearch/etc/config.xml

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<engine>mysql</engine>
1616
<min_query_length>1</min_query_length>
1717
<max_query_length>128</max_query_length>
18+
<max_count_cacheable_search_terms>100</max_count_cacheable_search_terms>
1819
</search>
1920
</catalog>
2021
</default>

app/code/Magento/CatalogSearch/view/frontend/layout/catalogsearch_result_index.xml

+5-4
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
<body>
1010
<attribute name="class" value="page-products"/>
1111
<referenceContainer name="content">
12-
<block class="Magento\CatalogSearch\Block\Result" name="search.result" template="Magento_CatalogSearch::result.phtml" cacheable="false">
13-
<block class="Magento\CatalogSearch\Block\SearchResult\ListProduct" name="search_result_list" template="Magento_Catalog::product/list.phtml" cacheable="false">
12+
<block class="Magento\CatalogSearch\Block\Result" name="search.result" template="Magento_CatalogSearch::result.phtml">
13+
<block class="Magento\CatalogSearch\Block\SearchResult\ListProduct" name="search_result_list" template="Magento_Catalog::product/list.phtml">
1414
<arguments>
1515
<!-- If argument's position depends on image size changeable in VDE:
1616
positions:list-secondary,grid-secondary,list-actions,grid-actions,list-primary,grid-primary
1717
-->
1818
<argument name="positioned" xsi:type="string">positions:list-secondary</argument>
1919
</arguments>
20-
<block class="Magento\Catalog\Block\Product\ProductList\Toolbar" name="product_list_toolbar" template="Magento_Catalog::product/list/toolbar.phtml" cacheable="false">
21-
<block class="Magento\Theme\Block\Html\Pager" name="product_list_toolbar_pager" cacheable="false"/>
20+
<block class="Magento\Catalog\Block\Product\ProductList\Toolbar" name="product_list_toolbar" template="Magento_Catalog::product/list/toolbar.phtml">
21+
<block class="Magento\Theme\Block\Html\Pager" name="product_list_toolbar_pager"/>
2222
</block>
2323
<action method="setToolbarBlockName">
2424
<argument name="name" xsi:type="string">product_list_toolbar</argument>
@@ -36,6 +36,7 @@
3636
<action method="setListModes"/>
3737
<action method="setListCollection"/>
3838
</block>
39+
<block class="Magento\CatalogSearch\Block\SearchTermsLog" name="search.search_terms_log" template="Magento_CatalogSearch::search_terms_log.phtml"/>
3940
</referenceContainer>
4041
</body>
4142
</page>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
?>
7+
<?php if ($block->isAjaxInsert()):?>
8+
<script type="text/x-magento-init">
9+
{
10+
"*": {
11+
"Magento_CatalogSearch/js/search_terms_log": {
12+
"url": "<?= $block->getUrl('catalogsearch/searchTermsLog/save') ?>",
13+
"query": "<?= $block->getQuery()->getQueryText() ?>"
14+
}
15+
}
16+
}
17+
</script>
18+
<?php endif; ?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
define([
7+
'jquery'
8+
], function ($) {
9+
'use strict';
10+
11+
return function (data) {
12+
var url = data.url;
13+
var query = data.query;
14+
15+
$.ajax({
16+
method: 'GET',
17+
url: url,
18+
data: {
19+
'q': query
20+
},
21+
cache: false
22+
}).done();
23+
};
24+
});

0 commit comments

Comments
 (0)