Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Prevent adapter from connecting to search #61

Merged
merged 3 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 39 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,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()) {
gowrizrh marked this conversation as resolved.
Show resolved Hide resolved
$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());
}
}
3 changes: 2 additions & 1 deletion etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@

<type name="\Aligent\AsyncEvents\Model\Indexer\IndexStructure">
<arguments>
<argument name="adapter" xsi:type="object">\Magento\Elasticsearch\Model\Adapter\Elasticsearch\Proxy</argument>
<argument name="scopeResolver" xsi:type="object" shared="false">\Aligent\AsyncEvents\Model\Resolver\AsyncEvent</argument>
</arguments>
</type>
Expand Down Expand Up @@ -138,7 +139,7 @@
</arguments>
</virtualType>

<virtualType name="dataMapperAdapter" type="Magento\Elasticsearch\Model\Adapter\Elasticsearch">
<virtualType name="dataMapperAdapter" type="Magento\Elasticsearch\Model\Adapter\Elasticsearch\Proxy">
<arguments>
<argument name="batchDocumentDataMapper" xsi:type="object">\Aligent\AsyncEvents\Model\Adapter\BatchDataMapper\AsyncEventLogMapper</argument>
</arguments>
Expand Down
Loading