77
88use Magento \Elasticsearch \SearchAdapter \QueryAwareInterface ;
99use Magento \Elasticsearch \SearchAdapter \QueryContainer ;
10+ use Magento \Framework \App \ObjectManager ;
11+ use Psr \Log \LoggerInterface ;
1012
1113/**
1214 * Elastic search data provider
@@ -83,6 +85,11 @@ class DataProvider implements \Magento\Framework\Search\Dynamic\DataProviderInte
8385 */
8486 private $ queryContainer ;
8587
88+ /**
89+ * @var LoggerInterface
90+ */
91+ private $ logger ;
92+
8693 /**
8794 * @param \Magento\Elasticsearch\SearchAdapter\ConnectionManager $connectionManager
8895 * @param \Magento\Elasticsearch\Model\Adapter\FieldMapperInterface $fieldMapper
@@ -94,7 +101,7 @@ class DataProvider implements \Magento\Framework\Search\Dynamic\DataProviderInte
94101 * @param string $indexerId
95102 * @param \Magento\Framework\App\ScopeResolverInterface $scopeResolver
96103 * @param QueryContainer|null $queryContainer
97- *
104+ * @param LoggerInterface|null $logger
98105 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
99106 */
100107 public function __construct (
@@ -107,7 +114,8 @@ public function __construct(
107114 \Magento \Elasticsearch \SearchAdapter \SearchIndexNameResolver $ searchIndexNameResolver ,
108115 $ indexerId ,
109116 \Magento \Framework \App \ScopeResolverInterface $ scopeResolver ,
110- QueryContainer $ queryContainer = null
117+ QueryContainer $ queryContainer = null ,
118+ LoggerInterface $ logger = null
111119 ) {
112120 $ this ->connectionManager = $ connectionManager ;
113121 $ this ->fieldMapper = $ fieldMapper ;
@@ -119,6 +127,7 @@ public function __construct(
119127 $ this ->indexerId = $ indexerId ;
120128 $ this ->scopeResolver = $ scopeResolver ;
121129 $ this ->queryContainer = $ queryContainer ;
130+ $ this ->logger = $ logger ?? ObjectManager::getInstance ()->get (LoggerInterface::class);
122131 }
123132
124133 /**
@@ -154,16 +163,19 @@ public function getAggregations(\Magento\Framework\Search\Dynamic\EntityStorage
154163 ],
155164 ];
156165
157- $ queryResult = $ this ->connectionManager ->getConnection ()
158- ->query ($ query );
159-
160- if (isset ($ queryResult ['aggregations ' ]['prices ' ])) {
161- $ aggregations = [
162- 'count ' => $ queryResult ['aggregations ' ]['prices ' ]['count ' ],
163- 'max ' => $ queryResult ['aggregations ' ]['prices ' ]['max ' ],
164- 'min ' => $ queryResult ['aggregations ' ]['prices ' ]['min ' ],
165- 'std ' => $ queryResult ['aggregations ' ]['prices ' ]['std_deviation ' ],
166- ];
166+ try {
167+ $ queryResult = $ this ->connectionManager ->getConnection ()
168+ ->query ($ query );
169+ if (isset ($ queryResult ['aggregations ' ]['prices ' ])) {
170+ $ aggregations = [
171+ 'count ' => $ queryResult ['aggregations ' ]['prices ' ]['count ' ],
172+ 'max ' => $ queryResult ['aggregations ' ]['prices ' ]['max ' ],
173+ 'min ' => $ queryResult ['aggregations ' ]['prices ' ]['min ' ],
174+ 'std ' => $ queryResult ['aggregations ' ]['prices ' ]['std_deviation ' ],
175+ ];
176+ }
177+ } catch (\Exception $ e ) {
178+ $ this ->logger ->critical ($ e );
167179 }
168180
169181 return $ aggregations ;
@@ -202,8 +214,6 @@ public function getAggregation(
202214 $ range ,
203215 \Magento \Framework \Search \Dynamic \EntityStorage $ entityStorage
204216 ) {
205- $ result = [];
206-
207217 $ query = $ this ->getBasicSearchQuery ($ entityStorage );
208218
209219 $ fieldName = $ this ->fieldMapper ->getFieldName ($ bucket ->getField ());
@@ -217,11 +227,16 @@ public function getAggregation(
217227 ],
218228 ];
219229
220- $ queryResult = $ this ->connectionManager ->getConnection ()
221- ->query ($ query );
222- foreach ($ queryResult ['aggregations ' ]['prices ' ]['buckets ' ] as $ bucket ) {
223- $ key = (int )($ bucket ['key ' ] / $ range + 1 );
224- $ result [$ key ] = $ bucket ['doc_count ' ];
230+ $ result = [];
231+ try {
232+ $ queryResult = $ this ->connectionManager ->getConnection ()
233+ ->query ($ query );
234+ foreach ($ queryResult ['aggregations ' ]['prices ' ]['buckets ' ] as $ bucket ) {
235+ $ key = (int )($ bucket ['key ' ] / $ range + 1 );
236+ $ result [$ key ] = $ bucket ['doc_count ' ];
237+ }
238+ } catch (\Exception $ e ) {
239+ $ this ->logger ->critical ($ e );
225240 }
226241
227242 return $ result ;
0 commit comments