diff --git a/Adapter/Client.php b/Adapter/Client.php
index 8355e6e..54dd82e 100644
--- a/Adapter/Client.php
+++ b/Adapter/Client.php
@@ -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;
@@ -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
@@ -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;
}
/**
@@ -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'];
@@ -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])];
+ }
+ }
}
+
$indexName = rtrim($indexName, "_tmp");
return $this->getTypesenseClient()->collections[$indexName]->getDocuments()->import($data, ['action' => 'upsert']);
}
@@ -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;
+ }
+
}
diff --git a/Helper/ConfigChangeHelper.php b/Helper/ConfigChangeHelper.php
index 633e501..270e888 100644
--- a/Helper/ConfigChangeHelper.php
+++ b/Helper/ConfigChangeHelper.php
@@ -103,6 +103,9 @@ public function __construct(
*/
public function setCollectionConfig()
{
+ if (!$this->configService->isTypeSenseEnabled()) {
+ return $this;
+ }
$facets = [];
@@ -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;
- }
}
}
@@ -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();
@@ -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();
@@ -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(),
@@ -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()
];
}
@@ -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) {
diff --git a/Plugin/Algolia/AlgoliaSearch/Model/SaveSettings.php b/Plugin/Algolia/AlgoliaSearch/Model/SaveSettings.php
new file mode 100644
index 0000000..b1b02d0
--- /dev/null
+++ b/Plugin/Algolia/AlgoliaSearch/Model/SaveSettings.php
@@ -0,0 +1,41 @@
+configService = $configService;
+ }
+
+ public function aroundExecute(
+ \Algolia\AlgoliaSearch\Model\Observer\SaveSettings $subject,
+ \Closure $proceed
+ ) {
+ if($this->configService->isIndexModeTypeSenseOnly()){
+ return;
+ }
+
+ $result = $proceed();
+
+ return $result;
+ }
+}
diff --git a/README.md b/README.md
index 53913fc..d0410ba 100644
--- a/README.md
+++ b/README.md
@@ -1,115 +1,87 @@
-
-
+# Magento 2 Typesense Search Integration Module
-
-
-
-
-