Skip to content

Commit

Permalink
🔧 refactor: use lucene search if indexing is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
gowrizrh committed Mar 18, 2024
1 parent 8b64f16 commit ae5ec76
Showing 1 changed file with 42 additions and 23 deletions.
65 changes: 42 additions & 23 deletions Model/Indexer/LuceneSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
*/
Expand All @@ -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 = []
) {
Expand All @@ -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();
}
}
}

0 comments on commit ae5ec76

Please sign in to comment.