Skip to content

Commit

Permalink
Merge branch 'release-1.18.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Sep 27, 2024
2 parents 36ceffe + 036ab36 commit b066c79
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 10 deletions.
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,15 @@

### Activate advanced search

#### Activate for using credentials from environment variables

```shell
php index.php 'oat\taoAdvancedSearch\scripts\tools\Activate'
```

#### Activate without credentials needed


```shell
php index.php 'oat\taoAdvancedSearch\scripts\tools\Activate' --host <host url> --port <host port> [--indexPrefix <optional>]
```
Expand Down Expand Up @@ -174,10 +181,17 @@ php index.php '\oat\taoAdvancedSearch\scripts\tools\IndexSummary'

## Environment variables

| Variable | Description | Example |
|---------------------------------------|--------------------------------------------------------------------------------------------|----------------|
| FEATURE_FLAG_ADVANCED_SEARCH_DISABLED | In case you do not want to have AdvancedSearch enabled even if this extension is installed | true |
| ADVANCED_SEARCH_METADATA_BLACK_LIST | To avoid indexing metadata that is used in the criteria filter | URI1,URI2,URI3 |
| Variable | Description | Example |
|---------------------------------------|--------------------------------------------------------------------------------------------|-----------------------|
| FEATURE_FLAG_ADVANCED_SEARCH_DISABLED | In case you do not want to have AdvancedSearch enabled even if this extension is installed | true |
| ADVANCED_SEARCH_METADATA_BLACK_LIST | To avoid indexing metadata that is used in the criteria filter | URI1,URI2,URI3 |
| ELASTICSEARCH_HOSTS | ElasticSearch hosts (use space as delimiter) | http://localhost:9200 |
| ELASTICSEARCH_USERNAME | ElasticSearch user | user |
| ELASTICSEARCH_PASSWORD | ElasticSearch password | pass |
| ELASTICSEARCH_PREFIX | Prefix to be used in the index name | tao |
| ELASTICSEARCH_CLOUD_ID | ElasticSearch cloud id | cloud_id |
| ELASTICSEARCH_API_KEY_ID | ElasticSearch api key id | api_key_id |
| ELASTICSEARCH_API_KEY | ElasticSearch api key | api_key | | URI1,URI2,URI3 |


## How to create custom indexers?
Expand Down
69 changes: 69 additions & 0 deletions migrations/Version202409231414061488_taoAdvancedSearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace oat\taoAdvancedSearch\migrations;

use Doctrine\DBAL\Schema\Schema;
use oat\generis\model\DependencyInjection\ServiceOptions;
use oat\tao\scripts\tools\migrations\AbstractMigration;
use oat\taoAdvancedSearch\model\SearchEngine\Driver\Elasticsearch\ElasticSearchConfig;

/**
* Auto-generated Migration: Please modify to your needs!
*
* phpcs:disable Squiz.Classes.ValidClassName
*/
final class Version202409231414061488_taoAdvancedSearch extends AbstractMigration
{
public function getDescription(): string
{
return 'Update ElasticSearchConfig to use properties from environment variables';
}

public function up(Schema $schema): void
{
$serviceManager = $this->getServiceManager();
$serviceOptions = $this->getServiceOptions();
$options = $serviceOptions->getOptions();

if (
isset($options[ElasticSearchConfig::class][ElasticSearchConfig::OPTION_ELASTIC_CLOUD_ID]) &&
isset($options[ElasticSearchConfig::class][ElasticSearchConfig::OPTION_INDEX_PREFIX]) &&
isset($options[ElasticSearchConfig::class][ElasticSearchConfig::OPTION_ELASTIC_CLOUD_API_KEY_ID]) &&
isset($options[ElasticSearchConfig::class][ElasticSearchConfig::OPTION_ELASTIC_CLOUD_API_KEY]) &&
$this->checkExistingEnvVariables()
) {
unset($options[ElasticSearchConfig::class]);
$serviceOptions->setOptions($options);
$serviceManager->register(ServiceOptions::SERVICE_ID, $serviceOptions);
}

if (
$serviceOptions->get(ElasticSearchConfig::class, ElasticSearchConfig::OPTION_HOSTS)
&& getenv(ElasticSearchConfig::ENV_OPTION_HOSTS)
) {
unset($options[ElasticSearchConfig::class]);
$serviceOptions->setOptions($options);
$serviceManager->register(ServiceOptions::SERVICE_ID, $serviceOptions);
}
}

private function checkExistingEnvVariables(): bool
{
return getenv(ElasticSearchConfig::ENV_OPTION_INDEX_PREFIX)
&& getenv(ElasticSearchConfig::ENV_OPTION_ELASTIC_CLOUD_ID)
&& getenv(ElasticSearchConfig::ENV_OPTION_ELASTIC_CLOUD_API_KEY_ID)
&& getenv(ElasticSearchConfig::ENV_OPTION_ELASTIC_CLOUD_API_KEY);
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
}

private function getServiceOptions(): ServiceOptions
{
return $this->getServiceManager()->getContainer()->get(ServiceOptions::class);
}
}
50 changes: 44 additions & 6 deletions model/SearchEngine/Driver/Elasticsearch/ElasticSearchConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,16 @@ class ElasticSearchConfig
public const OPTION_USERNAME = 'user';
public const OPTION_PASSWORD = 'pass';

public const ENV_OPTION_INDEX_PREFIX = 'ELASTICSEARCH_PREFIX';
public const ENV_OPTION_ELASTIC_CLOUD_ID = 'ELASTICSEARCH_CLOUD_ID';
public const ENV_OPTION_ELASTIC_CLOUD_API_KEY_ID = 'ELASTICSEARCH_API_KEY_ID';
public const ENV_OPTION_ELASTIC_CLOUD_API_KEY = 'ELASTICSEARCH_API_KEY';
public const ENV_OPTION_HOSTS = 'ELASTICSEARCH_HOSTS';
public const ENV_OPTION_USERNAME = 'ELASTICSEARCH_USERNAME';
public const ENV_OPTION_PASSWORD = 'ELASTICSEARCH_PASSWORD';

/** @var ServiceOptionsInterface */
private $serviceOptions;
private ServiceOptionsInterface $serviceOptions;

public function __construct(ServiceOptionsInterface $serviceOptions)
{
Expand All @@ -47,48 +55,78 @@ public function __construct(ServiceOptionsInterface $serviceOptions)
public function getHosts(): array
{
$hosts = [];
foreach ($this->serviceOptions->get(self::class, self::OPTION_HOSTS) as $host) {
if (is_array($host) && isset($host['host'])) {
$hosts[] = ($host['scheme'] ?? 'http') . '://' . $host['host'] . ':' . ($host['port'] ?? 9200);
} else {
$hosts[] = $host;

if (getenv(self::ENV_OPTION_HOSTS)) {
return explode(' ', getenv(self::ENV_OPTION_HOSTS));
}

if ($this->serviceOptions->get(self::class, self::OPTION_HOSTS)) {
foreach ($this->serviceOptions->get(self::class, self::OPTION_HOSTS) as $host) {
if (is_array($host) && isset($host['host'])) {
$hosts[] = ($host['scheme'] ?? 'http') . '://' . $host['host'] . ':' . ($host['port'] ?? 9200);
} else {
$hosts[] = $host;
}
}
return $hosts;
}

return $hosts;
}

public function getUsername(): ?string
{
if (getenv(self::ENV_OPTION_USERNAME)) {
return getenv(self::ENV_OPTION_USERNAME) ?: null;
}
return $this->getFirstHost()[self::OPTION_USERNAME] ?? null;
}

public function getPassword(): ?string
{
if (getenv(self::ENV_OPTION_PASSWORD)) {
return getenv(self::ENV_OPTION_PASSWORD) ?: null;
}
return $this->getFirstHost()[self::OPTION_PASSWORD] ?? null;
}

public function getIndexPrefix(): ?string
{
if (getenv(self::ENV_OPTION_INDEX_PREFIX)) {
return getenv(self::ENV_OPTION_INDEX_PREFIX) ?: null;
}
return $this->serviceOptions->get(self::class, self::OPTION_INDEX_PREFIX);
}

public function getElasticCloudId(): ?string
{
if (getenv(self::ENV_OPTION_ELASTIC_CLOUD_ID)) {
return getenv(self::ENV_OPTION_ELASTIC_CLOUD_ID) ?: null;
}
return $this->serviceOptions->get(self::class, self::OPTION_ELASTIC_CLOUD_ID);
}

public function getElasticCloudApiKey(): ?string
{
if (getenv(self::ENV_OPTION_ELASTIC_CLOUD_API_KEY)) {
return getenv(self::ENV_OPTION_ELASTIC_CLOUD_API_KEY) ?: null;
}
return $this->serviceOptions->get(self::class, self::OPTION_ELASTIC_CLOUD_API_KEY);
}

public function getElasticCloudApiKeyId(): ?string
{
if (getenv(self::ENV_OPTION_ELASTIC_CLOUD_API_KEY_ID)) {
return getenv(self::ENV_OPTION_ELASTIC_CLOUD_API_KEY_ID) ?: null;
}
return $this->serviceOptions->get(self::class, self::OPTION_ELASTIC_CLOUD_API_KEY_ID);
}

private function getFirstHost(): ?array
{
if (getenv(self::ENV_OPTION_HOSTS)) {
return [current($this->getHosts())];
}
return current($this->serviceOptions->get(self::class, self::OPTION_HOSTS)) ?: null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ protected function setUp(): void
{
$this->options = $this->createMock(ServiceOptionsInterface::class);
$this->sut = new ElasticSearchConfig($this->options);
putenv('ELASTICSEARCH_HOSTS');
putenv('ELASTICSEARCH_USERNAME');
putenv('ELASTICSEARCH_PASSWORD');
putenv('ELASTICSEARCH_PREFIX');
putenv('ELASTICSEARCH_CLOUD_ID');
putenv('ELASTICSEARCH_API_KEY_ID');
putenv('ELASTICSEARCH_API_KEY');
}

public function testGetters(): void
Expand Down Expand Up @@ -97,4 +104,23 @@ static function (string $class, string $option) {
$this->assertSame('username', $this->sut->getUsername());
$this->assertSame('password', $this->sut->getPassword());
}

public function testEnvGetters(): void
{
putenv('ELASTICSEARCH_HOSTS=scheme://host:port');
putenv('ELASTICSEARCH_USERNAME=username');
putenv('ELASTICSEARCH_PASSWORD=password');
putenv('ELASTICSEARCH_PREFIX=prefix');
putenv('ELASTICSEARCH_CLOUD_ID=cloud_id');
putenv('ELASTICSEARCH_API_KEY_ID=api_key_id');
putenv('ELASTICSEARCH_API_KEY=api_key');

$this->assertSame(['scheme://host:port'], $this->sut->getHosts());
$this->assertSame('username', $this->sut->getUsername());
$this->assertSame('password', $this->sut->getPassword());
$this->assertSame('prefix', $this->sut->getIndexPrefix());
$this->assertSame('cloud_id', $this->sut->getElasticCloudId());
$this->assertSame('api_key_id', $this->sut->getElasticCloudApiKeyId());
$this->assertSame('api_key', $this->sut->getElasticCloudApiKey());
}
}

0 comments on commit b066c79

Please sign in to comment.