Skip to content

Commit 4e6a3bc

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-58042' into 2.1.8-develop-pr19
2 parents c8fe101 + 960ba65 commit 4e6a3bc

File tree

15 files changed

+772
-77
lines changed

15 files changed

+772
-77
lines changed

app/code/Magento/CatalogSearch/Block/Advanced/Result.php

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,26 @@
1414
use Magento\Framework\View\Element\Template\Context;
1515

1616
/**
17-
* Advanced search result
17+
* Advanced search result.
1818
*/
1919
class Result extends Template
2020
{
2121
/**
22-
* Url factory
22+
* Url factory.
2323
*
2424
* @var UrlFactory
2525
*/
2626
protected $_urlFactory;
2727

2828
/**
29-
* Catalog layer
29+
* Catalog layer.
3030
*
3131
* @var \Magento\Catalog\Model\Layer
3232
*/
3333
protected $_catalogLayer;
3434

3535
/**
36-
* Catalog search advanced
36+
* Catalog search advanced.
3737
*
3838
* @var Advanced
3939
*/
@@ -64,6 +64,7 @@ public function __construct(
6464
*/
6565
protected function _prepareLayout()
6666
{
67+
$this->pageConfig->getTitle()->set($this->getPageTitle());
6768
$breadcrumbs = $this->getLayout()->getBlock('breadcrumbs');
6869
if ($breadcrumbs) {
6970
$breadcrumbs->addCrumb(
@@ -81,11 +82,22 @@ protected function _prepareLayout()
8182
['label' => __('Results')]
8283
);
8384
}
85+
8486
return parent::_prepareLayout();
8587
}
8688

8789
/**
88-
* Set order options
90+
* Get page title.
91+
*
92+
* @return \Magento\Framework\Phrase
93+
*/
94+
private function getPageTitle()
95+
{
96+
return __('Advanced Search Results');
97+
}
98+
99+
/**
100+
* Set order options.
89101
*
90102
* @return void
91103
*/
@@ -101,7 +113,7 @@ public function setListOrders()
101113
}
102114

103115
/**
104-
* Set view mode options
116+
* Set view mode options.
105117
*
106118
* @return void
107119
*/
@@ -111,6 +123,8 @@ public function setListModes()
111123
}
112124

113125
/**
126+
* Set search result collection.
127+
*
114128
* @return void
115129
*/
116130
public function setListCollection()
@@ -119,6 +133,8 @@ public function setListCollection()
119133
}
120134

121135
/**
136+
* Return product collection.
137+
*
122138
* @return Collection
123139
*/
124140
protected function _getProductCollection()
@@ -127,6 +143,8 @@ protected function _getProductCollection()
127143
}
128144

129145
/**
146+
* Return search model.
147+
*
130148
* @return Advanced
131149
*/
132150
public function getSearchModel()
@@ -135,6 +153,8 @@ public function getSearchModel()
135153
}
136154

137155
/**
156+
* Return results count.
157+
*
138158
* @return mixed
139159
*/
140160
public function getResultCount()
@@ -143,10 +163,13 @@ public function getResultCount()
143163
$size = $this->getSearchModel()->getProductCollection()->getSize();
144164
$this->setResultCount($size);
145165
}
166+
146167
return $this->getData('result_count');
147168
}
148169

149170
/**
171+
* Return search product listing html.
172+
*
150173
* @return string
151174
*/
152175
public function getProductListHtml()
@@ -155,6 +178,8 @@ public function getProductListHtml()
155178
}
156179

157180
/**
181+
* Return form url.
182+
*
158183
* @return string
159184
*/
160185
public function getFormUrl()
@@ -168,6 +193,8 @@ public function getFormUrl()
168193
}
169194

170195
/**
196+
* Return search criteria.
197+
*
171198
* @return array
172199
*/
173200
public function getSearchCriterias()

app/code/Magento/CatalogSearch/Model/Search/RequestGenerator.php

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88
use Magento\Catalog\Api\Data\EavAttributeInterface;
99
use Magento\Catalog\Model\Entity\Attribute;
1010
use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory;
11-
use Magento\Framework\Search\Request\BucketInterface;
11+
use Magento\CatalogSearch\Model\Search\RequestGenerator\GeneratorResolver;
12+
use Magento\Framework\App\ObjectManager;
1213
use Magento\Framework\Search\Request\FilterInterface;
1314
use Magento\Framework\Search\Request\QueryInterface;
1415

16+
/**
17+
* Search request generator.
18+
*/
1519
class RequestGenerator
1620
{
1721
/** Filter name suffix */
@@ -21,20 +25,34 @@ class RequestGenerator
2125
const BUCKET_SUFFIX = '_bucket';
2226

2327
/**
28+
* Product attribute collection factory.
29+
*
2430
* @var CollectionFactory
2531
*/
2632
private $productAttributeCollectionFactory;
2733

34+
/**
35+
* Search generator resolver.
36+
*
37+
* @var GeneratorResolver
38+
*/
39+
private $generatorResolver;
40+
2841
/**
2942
* @param CollectionFactory $productAttributeCollectionFactory
43+
* @param GeneratorResolver $generatorResolver
3044
*/
31-
public function __construct(CollectionFactory $productAttributeCollectionFactory)
32-
{
45+
public function __construct(
46+
CollectionFactory $productAttributeCollectionFactory,
47+
GeneratorResolver $generatorResolver = null
48+
) {
3349
$this->productAttributeCollectionFactory = $productAttributeCollectionFactory;
50+
$this->generatorResolver = $generatorResolver
51+
?: ObjectManager::getInstance()->get(GeneratorResolver::class);
3452
}
3553

3654
/**
37-
* Generate dynamic fields requests
55+
* Generate dynamic fields requests.
3856
*
3957
* @return array
4058
*/
@@ -46,11 +64,12 @@ public function generate()
4664
$requests['quick_search_container'] =
4765
$this->generateRequest(EavAttributeInterface::IS_FILTERABLE_IN_SEARCH, 'quick_search_container', true);
4866
$requests['advanced_search_container'] = $this->generateAdvancedSearchRequest();
67+
4968
return $requests;
5069
}
5170

5271
/**
53-
* Generate search request
72+
* Generate search request.
5473
*
5574
* @param string $attributeType
5675
* @param string $container
@@ -62,7 +81,7 @@ private function generateRequest($attributeType, $container, $useFulltext)
6281
$request = [];
6382
foreach ($this->getSearchableAttributes() as $attribute) {
6483
if ($attribute->getData($attributeType)) {
65-
if (!in_array($attribute->getAttributeCode(), ['price', 'category_ids'])) {
84+
if (!in_array($attribute->getAttributeCode(), ['price', 'category_ids'], true)) {
6685
$queryName = $attribute->getAttributeCode() . '_query';
6786

6887
$request['queries'][$container]['queryReference'][] = [
@@ -76,58 +95,33 @@ private function generateRequest($attributeType, $container, $useFulltext)
7695
'filterReference' => [['ref' => $filterName]],
7796
];
7897
$bucketName = $attribute->getAttributeCode() . self::BUCKET_SUFFIX;
79-
if ($attribute->getBackendType() == 'decimal') {
80-
$request['filters'][$filterName] = [
81-
'type' => FilterInterface::TYPE_RANGE,
82-
'name' => $filterName,
83-
'field' => $attribute->getAttributeCode(),
84-
'from' => '$' . $attribute->getAttributeCode() . '.from$',
85-
'to' => '$' . $attribute->getAttributeCode() . '.to$',
86-
];
87-
$request['aggregations'][$bucketName] = [
88-
'type' => BucketInterface::TYPE_DYNAMIC,
89-
'name' => $bucketName,
90-
'field' => $attribute->getAttributeCode(),
91-
'method' => 'manual',
92-
'metric' => [["type" => "count"]],
93-
];
94-
} else {
95-
$request['filters'][$filterName] = [
96-
'type' => FilterInterface::TYPE_TERM,
97-
'name' => $filterName,
98-
'field' => $attribute->getAttributeCode(),
99-
'value' => '$' . $attribute->getAttributeCode() . '$',
100-
];
101-
$request['aggregations'][$bucketName] = [
102-
'type' => BucketInterface::TYPE_TERM,
103-
'name' => $bucketName,
104-
'field' => $attribute->getAttributeCode(),
105-
'metric' => [["type" => "count"]],
106-
];
107-
}
98+
$generator = $this->generatorResolver->getGeneratorForType($attribute->getBackendType());
99+
$request['filters'][$filterName] = $generator->getFilterData($attribute, $filterName);
100+
$request['aggregations'][$bucketName] = $generator->getAggregationData($attribute, $bucketName);
108101
}
109102
}
110103
/** @var $attribute Attribute */
111-
if (in_array($attribute->getAttributeCode(), ['price', 'sku'])
112-
|| !$attribute->getIsSearchable()
113-
) {
114-
//same fields have special semantics
104+
if (!$attribute->getIsSearchable() || in_array($attribute->getAttributeCode(), ['price', 'sku'], true)) {
105+
// Some fields have their own specific handlers
115106
continue;
116107
}
117-
if ($useFulltext) {
108+
109+
// Match search by custom price attribute isn't supported
110+
if ($useFulltext && $attribute->getFrontendInput() !== 'price') {
118111
$request['queries']['search']['match'][] = [
119112
'field' => $attribute->getAttributeCode(),
120113
'boost' => $attribute->getSearchWeight() ?: 1,
121114
];
122115
}
123116
}
117+
124118
return $request;
125119
}
126120

127121
/**
128-
* Retrieve searchable attributes
122+
* Retrieve searchable attributes.
129123
*
130-
* @return \Magento\Catalog\Model\Entity\Attribute[]
124+
* @return \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection
131125
*/
132126
protected function getSearchableAttributes()
133127
{
@@ -142,7 +136,7 @@ protected function getSearchableAttributes()
142136
}
143137

144138
/**
145-
* Generate advanced search request
139+
* Generate advanced search request.
146140
*
147141
* @return array
148142
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
@@ -231,6 +225,7 @@ private function generateAdvancedSearchRequest()
231225
];
232226
}
233227
}
228+
234229
return $request;
235230
}
236231
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\CatalogSearch\Model\Search\RequestGenerator;
8+
9+
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
10+
use Magento\Framework\Search\Request\BucketInterface;
11+
use Magento\Framework\Search\Request\FilterInterface;
12+
13+
/**
14+
* Search request generator for decimal attribute.
15+
*/
16+
class Decimal implements GeneratorInterface
17+
{
18+
/**
19+
* {@inheritdoc}
20+
*/
21+
public function getFilterData(Attribute $attribute, $filterName)
22+
{
23+
return [
24+
'type' => FilterInterface::TYPE_RANGE,
25+
'name' => $filterName,
26+
'field' => $attribute->getAttributeCode(),
27+
'from' => '$' . $attribute->getAttributeCode() . '.from$',
28+
'to' => '$' . $attribute->getAttributeCode() . '.to$',
29+
];
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function getAggregationData(Attribute $attribute, $bucketName)
36+
{
37+
return [
38+
'type' => BucketInterface::TYPE_DYNAMIC,
39+
'name' => $bucketName,
40+
'field' => $attribute->getAttributeCode(),
41+
'method' => 'manual',
42+
'metric' => [['type' => 'count']],
43+
];
44+
}
45+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\CatalogSearch\Model\Search\RequestGenerator;
8+
9+
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
10+
use Magento\Framework\Search\Request\BucketInterface;
11+
use Magento\Framework\Search\Request\FilterInterface;
12+
13+
/**
14+
* General search request generator for attribute.
15+
*/
16+
class General implements GeneratorInterface
17+
{
18+
/**
19+
* {@inheritdoc}
20+
*/
21+
public function getFilterData(Attribute $attribute, $filterName)
22+
{
23+
return [
24+
'type' => FilterInterface::TYPE_TERM,
25+
'name' => $filterName,
26+
'field' => $attribute->getAttributeCode(),
27+
'value' => '$' . $attribute->getAttributeCode() . '$',
28+
];
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public function getAggregationData(Attribute $attribute, $bucketName)
35+
{
36+
return [
37+
'type' => BucketInterface::TYPE_TERM,
38+
'name' => $bucketName,
39+
'field' => $attribute->getAttributeCode(),
40+
'metric' => [['type' => 'count']],
41+
];
42+
}
43+
}

0 commit comments

Comments
 (0)