Skip to content

Commit 4ea3434

Browse files
Add Support for autocreate of an Index for Loupe and Memory Adapter (#471)
Sadly it seems like we are not able to standardize IndexNotFoundException (#470) for save, delete, bulk or search actions. We will still support in Loupe Autocreate of the Index if it does not yet exists. While Indexes are in some cases autocreated we recommend to create Indexes via the frameworks CLI commands / scripts. Else Search Engine will create there own setting for the indexes and things like searchable, filterable, sortable, does may not work like expected. Why this is not the case for Loupe it is case for other Engine which also support auto create: - Elasticsearch - Opensearch - Algolia - Meilisearch They all create indexes with some kind of default settings not the defined mapping of yours. The following search engines will just fail if you try to save, delete, ... a document from it: - Solr - Typesense The following search engines will not fail when adding, deleting a document but still no Index exist so nothing is searchable. - Redisearch So fixes only parts of #463. --- TLDR; Only adapters which auto create indexes with correct configuration are with this PR: - Loupe - Memory All others are not created with the correct settings and createIndex should definitely still be called for them to update the settings of the specific engines correctly.
1 parent a3101ae commit 4ea3434

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

packages/seal-loupe-adapter/src/LoupeHelper.php

+24-16
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,7 @@ public function dropIndex(Index $index): void
8080

8181
public function createIndex(Index $index): void
8282
{
83-
$indexDirectory = $this->getIndexDirectory($index);
84-
if (!\file_exists($indexDirectory)) {
85-
\mkdir($indexDirectory, recursive: true);
86-
}
87-
88-
// dumping the configuration allows us to search and index without knowing the configuration
89-
// this way when a similar class like this would be part of loupe only the createIndex method
90-
// would require then to know the configuration
91-
$configuration = $this->createConfiguration($index);
83+
$configuration = $this->createAndDumpConfiguration($index);
9284
\file_put_contents($this->getConfigurationFile($index), \serialize($configuration));
9385
$this->loupe[$index->name] = $this->createLoupe($index, $configuration);
9486
}
@@ -112,14 +104,14 @@ private function createLoupe(Index $index, Configuration|null $configuration = n
112104
$configurationFile = $this->getConfigurationFile($index);
113105

114106
if (!\file_exists($configurationFile)) {
115-
throw new \LogicException('Configuration need to exist before creating Loupe instance.');
116-
}
107+
$configuration = $this->createAndDumpConfiguration($index);
108+
} else {
109+
/** @var string $configurationContent */
110+
$configurationContent = \file_get_contents($configurationFile);
117111

118-
/** @var string $configurationContent */
119-
$configurationContent = \file_get_contents($configurationFile);
120-
121-
/** @var Configuration $configuration */
122-
$configuration = \unserialize($configurationContent);
112+
/** @var Configuration $configuration */
113+
$configuration = \unserialize($configurationContent);
114+
}
123115
}
124116

125117
if ('' === $this->directory) {
@@ -129,6 +121,22 @@ private function createLoupe(Index $index, Configuration|null $configuration = n
129121
return $this->loupeFactory->create($this->getIndexDirectory($index), $configuration);
130122
}
131123

124+
private function createAndDumpConfiguration(Index $index): Configuration
125+
{
126+
$indexDirectory = $this->getIndexDirectory($index);
127+
if (!\file_exists($indexDirectory)) {
128+
\mkdir($indexDirectory, recursive: true);
129+
}
130+
131+
// dumping the configuration allows us to search and index without knowing the configuration
132+
// this way when a similar class like this would be part of loupe only the createIndex method
133+
// would require then to know the configuration
134+
$configuration = $this->createConfiguration($index);
135+
\file_put_contents($this->getConfigurationFile($index), \serialize($configuration));
136+
137+
return $configuration;
138+
}
139+
132140
private function createConfiguration(Index $index): Configuration
133141
{
134142
return Configuration::create()

packages/seal-memory-adapter/src/MemoryStorage.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ final class MemoryStorage
3636
public static function getDocuments(Index $index): array
3737
{
3838
if (!\array_key_exists($index->name, self::$indexes)) {
39-
throw new \RuntimeException('Index "' . $index->name . '" does not exist.');
39+
self::createIndex($index);
4040
}
4141

4242
return self::$documents[$index->name];
@@ -50,7 +50,7 @@ public static function getDocuments(Index $index): array
5050
public static function save(Index $index, array $document): array
5151
{
5252
if (!\array_key_exists($index->name, self::$indexes)) {
53-
throw new \RuntimeException('Index "' . $index->name . '" does not exist.');
53+
self::createIndex($index);
5454
}
5555

5656
$identifierField = $index->getIdentifierField();
@@ -66,7 +66,7 @@ public static function save(Index $index, array $document): array
6666
public static function delete(Index $index, string $identifier): void
6767
{
6868
if (!\array_key_exists($index->name, self::$indexes)) {
69-
throw new \RuntimeException('Index "' . $index->name . '" does not exist.');
69+
self::createIndex($index);
7070
}
7171

7272
unset(self::$documents[$index->name][$identifier]);

0 commit comments

Comments
 (0)