Skip to content
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
37 changes: 29 additions & 8 deletions Adapter/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Develo\Typesense\Adapter;

use Algolia\AlgoliaSearch\Helper\ConfigHelper as AlgoliaConfigHelper;
use Typesense\Client as TypeSenseClient;
use Develo\Typesense\Services\ConfigService;
use Algolia\AlgoliaSearch\Helper\Data as AlgoliaHelper;
Expand All @@ -16,20 +17,18 @@
*/
class Client
{
private ?array $facets = null;

/**
* @var ConfigService
*/
private ConfigService $configService;

/**
* @var TypeSenseClient|null
*/
private ?TypeSenseClient $typeSenseClient = null;

/**
* $var AlgoliaHelper
*/
private $algoliaHelper;
private AlgoliaHelper $algoliaHelper;

private AlgoliaConfigHelper $configHelper;

/**
* Initialise Typesense Client with Magento config
Expand All @@ -40,11 +39,13 @@ class Client
*/
public function __construct(
ConfigService $configService,
AlgoliaHelper $algoliaHelper
AlgoliaHelper $algoliaHelper,
AlgoliaConfigHelper $configHelper
)
{
$this->configService = $configService;
$this->algoliaHelper = $algoliaHelper;
$this->configHelper = $configHelper;
}

/**
Expand All @@ -63,6 +64,7 @@ public function deleteIndex(string $indexName): array
*/
public function addData($indexName, $data)
{
$facets = [];
foreach ($data as &$item) {
$item['id'] = (string)$item['objectID'];
$item['objectID'] = (string)$item['objectID'];
Expand All @@ -83,7 +85,14 @@ public function addData($indexName, $data)

$price['default'] = number_format($price['default'], 2);
}

foreach ($facets as $facet) {
if (isset($item[$facet]) && !is_array($item[$facet])) {
$item[$facet] = [strval($item[$facet])];
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

composite products send their attributes in an array - I thought it's probably easier just to force this for everything.

}
}
}

$indexName = rtrim($indexName, "_tmp");
return $this->getTypesenseClient()->collections[$indexName]->getDocuments()->import($data, ['action' => 'upsert']);
}
Expand Down Expand Up @@ -140,6 +149,18 @@ public function getTypesenseClient(): TypeSenseClient
return $this->typeSenseClient;
}

private function getFacets()
{
if (!is_array($this->facets)) {
$this->facets = [];
foreach ($this->configHelper->getFacets() as $facet) {
$this->facets[] = $facet['attribute'];
}
}

return $this->facets;
}

}


67 changes: 38 additions & 29 deletions Helper/ConfigChangeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ public function __construct(
*/
public function setCollectionConfig()
{
if (!$this->configService->isTypeSenseEnabled()) {
return $this;
}

$facets = [];

Expand All @@ -128,18 +131,20 @@ public function setCollectionConfig()

$indexName = $index["indexName"] . "_{$indexToCreate}";

if (!isset($existingCollections[$indexName])) {
if (isset($existingCollections[$indexName])) {
$this->typesenseClient->collections[$indexName]->delete();
unset($existingCollections[$indexName]);
}


$this->typeSenseCollecitons->create(
[
'name' => $indexName,
'enable_nested_fields' => true,
'fields' => $fields
]
);
$this->typeSenseCollecitons->create(
[
'name' => $indexName,
'enable_nested_fields' => true,
'fields' => $fields
]
);

continue;
}
}
}

Expand Down Expand Up @@ -174,7 +179,8 @@ private function getMagentoIndexes()
return $indexNames;
}

public function getFields(array $facets, array $sortingAttributes, string $index) : array {
public function getFields(array $facets, array $sortingAttributes, string $index): array
{
switch ($index) {
case 'products':
$attributes = $this->algoliaConfigHelper->getProductAdditionalAttributes();
Expand All @@ -186,6 +192,16 @@ public function getFields(array $facets, array $sortingAttributes, string $index
['name' => 'visibility_catalog', 'type' => 'int64', 'facet' => true]
];

// The hierarchal menu widget expects 10 levels of category.
for ($i = 0; $i < 10; $i++) {
$defaultAttributes[] = [
'name' => 'categories.level' . $i,
'type' => 'string[]',
'facet' => true,
'optional' => true
];
}

break;
case 'categories':
$attributes = $this->algoliaConfigHelper->getCategoryAdditionalAttributes();
Expand Down Expand Up @@ -217,21 +233,8 @@ public function getFields(array $facets, array $sortingAttributes, string $index

$attributeCollection = $this->attributeRepository->getList($entityTypeCode, $searchCriteria->create());

$backendTypes = [
'datetime' => 'string',
'decimal' => 'float',
'int' => 'int64',
'static' => 'string',
'text' => 'string',
'varchar' => 'string'
];

$fields = [];
foreach ($attributeCollection->getItems() as $attribute) {
if (!isset($backendTypes[$attribute->getBackendType()]) || !$attribute->getIsRequired()) {
continue;
}

if ($attribute->getAttributeCode() === 'price') {
$fields[] = [
'name' => $attribute->getAttributeCode(),
Expand All @@ -258,12 +261,18 @@ public function getFields(array $facets, array $sortingAttributes, string $index
continue;
}

$isFacet = in_array($attribute->getAttributeCode(), $facets);

if (!$isFacet) {
continue;
}

$fields[] = [
'name' => $attribute->getAttributeCode(),
'type' => $backendTypes[$attribute->getBackendType()],
'facet' => in_array($attribute->getAttributeCode(), $facets),
'sort' => in_array($attribute->getAttributeCode(), $sortingAttributes) &&
in_array($backendTypes[$attribute->getBackendType()], self::SORTABLE_ATTRIBUTES),
'type' => 'string[]',
'facet' => $isFacet,
'sort' => false,
'optional' => !$attribute->getIsRequired()
];
}

Expand All @@ -274,7 +283,7 @@ public function getFields(array $facets, array $sortingAttributes, string $index
return array_values($fields);
}

public function getSearchableAttributes(string $index = self::INDEX_PRODUCTS) : string
public function getSearchableAttributes(string $index = self::INDEX_PRODUCTS): string
{
$attributes = [];
foreach ($this->getFields([], [], $index) as $field) {
Expand Down
41 changes: 41 additions & 0 deletions Plugin/Algolia/AlgoliaSearch/Model/SaveSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Copyright © All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Develo\Typesense\Plugin\Algolia\AlgoliaSearch\Model;

use Develo\Typesense\Services\ConfigService;

class SaveSettings
{
/**
* @var ConfigService
*/
protected ConfigService $configService;

/**
* @param ConfigService $configService
*/
public function __construct(
ConfigService $configService
)
{
$this->configService = $configService;
}

public function aroundExecute(
\Algolia\AlgoliaSearch\Model\Observer\SaveSettings $subject,
\Closure $proceed
) {
if($this->configService->isIndexModeTypeSenseOnly()){
return;
}

$result = $proceed();

return $result;
}
}
Loading