diff --git a/Model/Indexer/LuceneSearch.php b/Model/Indexer/LuceneSearch.php index 1a1b017..9b0fa65 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,48 @@ 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 (empty($value)) { + return; + } + + 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'); + 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 (!empty($asyncEventIds)) { - $filter = $this->filterBuilder->setConditionType('in') - ->setField($this->getName()) - ->setValue($asyncEventIds) - ->create(); + $rawDocuments = $rawResponse['hits']['hits'] ?? []; + $asyncEventIds = array_column($rawDocuments, '_id'); - $this->getContext()->getDataProvider()->addFilter($filter); + 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"); } - } catch (Exception) { - // Fallback to default filter search - parent::prepare(); + + } else { + $filter = $this->filterBuilder->setConditionType('like') + ->setField('serialized_data') + ->setValue($value); } + + $this->getContext()->getDataProvider()->addFilter($filter->create()); } } 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