From 8b64f16c997798854749d2dbcac21cc3a124252d Mon Sep 17 00:00:00 2001 From: Gowri Date: Mon, 18 Mar 2024 13:08:01 +1030 Subject: [PATCH 1/3] :bug: fix: prevent adapter from connecting to es --- etc/di.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/etc/di.xml b/etc/di.xml index 74c2d36..6b51ecb 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -101,6 +101,7 @@ + \Magento\Elasticsearch\Model\Adapter\Elasticsearch\Proxy \Aligent\AsyncEvents\Model\Resolver\AsyncEvent @@ -138,7 +139,7 @@ - + \Aligent\AsyncEvents\Model\Adapter\BatchDataMapper\AsyncEventLogMapper From ae5ec7686c7bf787d699e1262211ecba80134fb0 Mon Sep 17 00:00:00 2001 From: Gowri Date: Mon, 18 Mar 2024 14:22:40 +1030 Subject: [PATCH 2/3] :wrench: refactor: use lucene search if indexing is enabled --- Model/Indexer/LuceneSearch.php | 65 ++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 23 deletions(-) diff --git a/Model/Indexer/LuceneSearch.php b/Model/Indexer/LuceneSearch.php index 1a1b017..a5e4063 100644 --- a/Model/Indexer/LuceneSearch.php +++ b/Model/Indexer/LuceneSearch.php @@ -12,6 +12,7 @@ use Magento\Framework\View\Element\UiComponentFactory; use Magento\Ui\Component\Filters\FilterModifier; use Magento\Ui\Component\Filters\Type\Search; +use Aligent\AsyncEvents\Helper\Config as AsyncEventsConfig; class LuceneSearch extends Search { @@ -22,6 +23,7 @@ class LuceneSearch extends Search * @param FilterModifier $filterModifier * @param ConnectionManager $connectionManager * @param Config $config + * @param AsyncEventsConfig $asyncEventsConfig * @param array $components * @param array $data */ @@ -32,6 +34,7 @@ public function __construct( FilterModifier $filterModifier, private readonly ConnectionManager $connectionManager, private readonly Config $config, + private readonly AsyncEventsConfig $asyncEventsConfig, array $components = [], array $data = [] ) { @@ -49,35 +52,51 @@ public function __construct( */ public function prepare(): void { - $client = $this->connectionManager->getConnection(); $value = $this->getContext()->getRequestParam('search'); - $indexPrefix = $this->config->getIndexPrefix(); - try { - $rawResponse = $client->query( - [ - 'index' => $indexPrefix . '_async_event_*', - 'q' => $value, - // the default page size is 10. The highest limit is 10000. If we want to traverse further, we will - // have to use the search after parameter. There are no plans to implement this right now. - 'size' => 100 - ] - ); + if ($this->asyncEventsConfig->isIndexingEnabled()) { + $client = $this->connectionManager->getConnection(); + $indexPrefix = $this->config->getIndexPrefix(); + $filter = $this->filterBuilder->setConditionType('in') + ->setField($this->getName()); - $rawDocuments = $rawResponse['hits']['hits'] ?? []; - $asyncEventIds = array_column($rawDocuments, '_id'); + if ($value === "") { + return; + } + + try { + $rawResponse = $client->query( + [ + 'index' => $indexPrefix . '_async_event_*', + 'q' => $value, + // the default page size is 10. The highest limit is 10000. If we want to traverse further, we + // will have to use the search after parameter. There are no plans to implement this right now. + 'size' => 100 + ] + ); + + $rawDocuments = $rawResponse['hits']['hits'] ?? []; + $asyncEventIds = array_column($rawDocuments, '_id'); + + if (!empty($asyncEventIds)) { + $filter->setValue($asyncEventIds); + } else { + $filter->setValue("0"); + } + } catch (Exception) { + // If we're unable to connect to Elasticsearch, we'll return nothing + $filter->setValue("0"); + } - if (!empty($asyncEventIds)) { - $filter = $this->filterBuilder->setConditionType('in') - ->setField($this->getName()) - ->setValue($asyncEventIds) - ->create(); + $this->getContext()->getDataProvider()->addFilter($filter->create()); + } else { + if ((string)$value !== '') { + $filter = $this->filterBuilder->setConditionType('like') + ->setField('serialized_data') + ->setValue($value); - $this->getContext()->getDataProvider()->addFilter($filter); + $this->getContext()->getDataProvider()->addFilter($filter->create()); } - } catch (Exception) { - // Fallback to default filter search - parent::prepare(); } } } From 6816c29044db8f87b3d40e1ace713cfa20224d7f Mon Sep 17 00:00:00 2001 From: Gowri Date: Mon, 18 Mar 2024 14:59:45 +1030 Subject: [PATCH 3/3] :wrench: chore: early return if empty value --- Model/Indexer/LuceneSearch.php | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/Model/Indexer/LuceneSearch.php b/Model/Indexer/LuceneSearch.php index a5e4063..9b0fa65 100644 --- a/Model/Indexer/LuceneSearch.php +++ b/Model/Indexer/LuceneSearch.php @@ -54,16 +54,16 @@ public function prepare(): void { $value = $this->getContext()->getRequestParam('search'); + if (empty($value)) { + return; + } + if ($this->asyncEventsConfig->isIndexingEnabled()) { $client = $this->connectionManager->getConnection(); $indexPrefix = $this->config->getIndexPrefix(); $filter = $this->filterBuilder->setConditionType('in') ->setField($this->getName()); - if ($value === "") { - return; - } - try { $rawResponse = $client->query( [ @@ -88,15 +88,12 @@ public function prepare(): void $filter->setValue("0"); } - $this->getContext()->getDataProvider()->addFilter($filter->create()); } else { - if ((string)$value !== '') { - $filter = $this->filterBuilder->setConditionType('like') - ->setField('serialized_data') - ->setValue($value); - - $this->getContext()->getDataProvider()->addFilter($filter->create()); - } + $filter = $this->filterBuilder->setConditionType('like') + ->setField('serialized_data') + ->setValue($value); } + + $this->getContext()->getDataProvider()->addFilter($filter->create()); } }