Skip to content

Commit c3f658b

Browse files
committed
MAGETWO-94172: Elasticsearch 5.0+ exception is shown if customer searches product before reindexing.
Add try catch block to catch exceptions thrown in Elastic search and return empty search results response.
1 parent 86ea94c commit c3f658b

File tree

1 file changed

+40
-18
lines changed
  • app/code/Magento/Elasticsearch/Elasticsearch5/SearchAdapter

1 file changed

+40
-18
lines changed

app/code/Magento/Elasticsearch/Elasticsearch5/SearchAdapter/Adapter.php

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Elasticsearch\Elasticsearch5\SearchAdapter;
78

89
use Magento\Framework\App\ObjectManager;
@@ -12,6 +13,7 @@
1213
use Magento\Elasticsearch\SearchAdapter\Aggregation\Builder as AggregationBuilder;
1314
use Magento\Elasticsearch\SearchAdapter\ConnectionManager;
1415
use \Magento\Elasticsearch\SearchAdapter\ResponseFactory;
16+
use Psr\Log\LoggerInterface;
1517

1618
/**
1719
* Elasticsearch Search Adapter
@@ -47,25 +49,56 @@ class Adapter implements AdapterInterface
4749
*/
4850
private $queryContainerFactory;
4951

52+
/**
53+
* Empty response from Elasticsearch.
54+
*
55+
* @var array
56+
*/
57+
private static $emptyRawResponse = [
58+
"hits" =>
59+
[
60+
"hits" => []
61+
],
62+
"aggregations" =>
63+
[
64+
"price_bucket" => [],
65+
"category_bucket" =>
66+
[
67+
"buckets" => []
68+
69+
]
70+
]
71+
];
72+
73+
/**
74+
* @var LoggerInterface
75+
*/
76+
private $logger;
77+
5078
/**
5179
* @param ConnectionManager $connectionManager
5280
* @param Mapper $mapper
5381
* @param ResponseFactory $responseFactory
5482
* @param AggregationBuilder $aggregationBuilder
5583
* @param \Magento\Elasticsearch\SearchAdapter\QueryContainerFactory $queryContainerFactory
84+
* @param LoggerInterface $logger
5685
*/
5786
public function __construct(
5887
ConnectionManager $connectionManager,
5988
Mapper $mapper,
6089
ResponseFactory $responseFactory,
6190
AggregationBuilder $aggregationBuilder,
62-
\Magento\Elasticsearch\SearchAdapter\QueryContainerFactory $queryContainerFactory
63-
) {
91+
\Magento\Elasticsearch\SearchAdapter\QueryContainerFactory $queryContainerFactory,
92+
LoggerInterface $logger = null
93+
)
94+
{
6495
$this->connectionManager = $connectionManager;
6596
$this->mapper = $mapper;
6697
$this->responseFactory = $responseFactory;
6798
$this->aggregationBuilder = $aggregationBuilder;
6899
$this->queryContainerFactory = $queryContainerFactory;
100+
$this->logger = $logger ?: ObjectManager::getInstance()
101+
->get(LoggerInterface::class);
69102
}
70103

71104
/**
@@ -78,23 +111,12 @@ public function query(RequestInterface $request)
78111
$aggregationBuilder = $this->aggregationBuilder;
79112
$query = $this->mapper->buildQuery($request);
80113
$aggregationBuilder->setQuery($this->queryContainerFactory->create(['query' => $query]));
81-
if ($client->indexExists($query['index'])) {
114+
try {
82115
$rawResponse = $client->query($query);
83-
} else {
84-
$rawResponse = [
85-
"hits" =>
86-
[
87-
"hits" => []
88-
],
89-
"aggregations" =>
90-
[
91-
"price_bucket" => [],
92-
"category_bucket" =>
93-
[
94-
"buckets" => []
95-
]
96-
]
97-
];
116+
} catch (\Exception $e) {
117+
$this->logger->critical($e);
118+
// return empty search result in case an exception is thrown from Elasticsearch
119+
$rawResponse = self::$emptyRawResponse;
98120
}
99121
$rawDocuments = isset($rawResponse['hits']['hits']) ? $rawResponse['hits']['hits'] : [];
100122
$queryResponse = $this->responseFactory->create(

0 commit comments

Comments
 (0)