Skip to content

Commit

Permalink
Merge branch 'release-1.17.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Aug 8, 2024
2 parents e165c05 + 9a11a94 commit 4c4e685
Show file tree
Hide file tree
Showing 24 changed files with 476 additions and 110 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,10 @@

> Extension required to advanced search integration with TAO platform `oat-sa/extension-tao-advanced-search`
# WARNING

The library `oat-sa/lib-tao-elasticsearch` is **deprecated** and this presence in the [composer.json](composer.json)
is maintained only for backward compatibility purposes with already installed applications.

Please, do not use this classes anymore! Use only classes from this very extension.

## Requirements

- ElasticSearch 7.10+ installed.
- ElasticSearch 8.13+ installed.
- Have this extension installed in TAO.

## Installation instructions
Expand Down Expand Up @@ -57,6 +51,12 @@ you can delete the indexes by running the command bellow:
php index.php 'oat\taoAdvancedSearch\scripts\tools\IndexDeleter'
```

### Create/Update aliases for indexes

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

## Indexation

### Warmup cache
Expand Down
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@
"oat-sa/oatbox-extension-installer": "~1.1||dev-master",
"ext-json": "*",
"oat-sa/generis": ">=15.22",
"oat-sa/tao-core": ">=53.7.1",
"oat-sa/tao-core": ">=54.21.0",
"oat-sa/extension-tao-testqti": ">=48.11.0",
"oat-sa/extension-tao-delivery": ">=14.10.1",
"oat-sa/extension-tao-outcome": ">=13.0.0",
"oat-sa/extension-tao-test": ">=15.15.1",
"oat-sa/extension-tao-mediamanager": ">=12.32.1",
"oat-sa/lib-tao-elasticsearch": ">=2.5.2"
"elasticsearch/elasticsearch": "^7.2|^8.0"
},
"require-dev": {
"php-http/mock-client": "^1.6",
"dg/bypass-finals": "^1.7"
},
"autoload": {
"psr-4": {
Expand Down
3 changes: 3 additions & 0 deletions config/assets.conf.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,8 @@
'number_of_replicas' => '1',
],
],
'aliases' => [
'assets_alias' => (object)[]
],
],
];
3 changes: 3 additions & 0 deletions config/deliveries.conf.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,8 @@
'number_of_replicas' => '1',
],
],
'aliases' => [
'deliveries_alias' => (object)[]
],
],
];
3 changes: 3 additions & 0 deletions config/delivery-results.conf.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,8 @@
'number_of_replicas' => '1',
],
],
'aliases' => [
'delivery-results_alias' => (object)[]
],
],
];
3 changes: 3 additions & 0 deletions config/groups.conf.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,8 @@
'number_of_replicas' => '1',
],
],
'aliases' => [
'groups_alias' => (object)[]
],
],
];
3 changes: 3 additions & 0 deletions config/items.conf.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,8 @@
'number_of_replicas' => '1',
],
],
'aliases' => [
'items_alias' => (object)[]
],
],
];
3 changes: 3 additions & 0 deletions config/property-list.conf.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@
'number_of_replicas' => '1',
],
],
'aliases' => [
'property-list_alias' => (object)[]
],
],
];
3 changes: 3 additions & 0 deletions config/test-takers.conf.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,8 @@
'number_of_replicas' => '1',
],
],
'aliases' => [
'test-takers_alias' => (object)[]
],
],
];
3 changes: 3 additions & 0 deletions config/tests.conf.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,8 @@
'number_of_replicas' => '1',
],
],
'aliases' => [
'tests_alias' => (object)[]
],
],
];
45 changes: 40 additions & 5 deletions model/SearchEngine/Driver/Elasticsearch/ElasticSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
namespace oat\taoAdvancedSearch\model\SearchEngine\Driver\Elasticsearch;

use ArrayIterator;
use Elasticsearch\Client;
use Elastic\Elasticsearch\Client;
use Exception;
use Iterator;
use oat\tao\model\search\SearchInterface as TaoSearchInterface;
Expand Down Expand Up @@ -111,7 +111,7 @@ public function search(Query $query): SearchResult
)
];

return $this->buildResultSet($this->client->search($query));
return $this->buildResultSet($this->client->search($query)->asArray());
}

public function query($queryString, $type, $start = 0, $count = 10, $order = '_id', $dir = 'DESC'): ResultSet
Expand All @@ -125,7 +125,7 @@ public function query($queryString, $type, $start = 0, $count = 10, $order = '_i
$this->logger->debug(sprintf('Elasticsearch Query %s', json_encode($query)));

return $this->searchResultNormalizer->normalizeByByResultSet(
$this->buildResultSet($this->client->search($query))
$this->buildResultSet($this->client->search($query)->asArray())
);
} catch (Exception $exception) {
switch ($exception->getCode()) {
Expand Down Expand Up @@ -181,6 +181,41 @@ public function createIndexes(): void
}
}

public function updateAliases(): void
{
$indexFile = $this->getIndexFile();

$indexes = [];

if ($indexFile && is_readable($indexFile)) {
$indexes = require $indexFile;
}

$aliases = [];

foreach ($indexes as $index) {
$aliases[$index['index']] = current(array_keys($index['body']['aliases']));
}
$this->client->indices()->updateAliases(
[
'body' => [
'actions' => array_map(
function ($index, $alias) {
return [
'add' => [
'index' => $index,
'alias' => $alias
]
];
},
array_keys($aliases),
$aliases
)
]
]
);
}

public function flush(): array
{
return $this->client->indices()->delete(
Expand All @@ -193,12 +228,12 @@ public function flush(): array
'ignore' => 404
]
]
);
)->asArray();
}

public function ping(): bool
{
return $this->client->ping();
return $this->client->ping()->asBool();
}

private function buildResultSet(array $elasticResult = []): SearchResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

namespace oat\taoAdvancedSearch\model\SearchEngine\Driver\Elasticsearch;

use Elasticsearch\Client;
use Elasticsearch\ClientBuilder;
use Elastic\Elasticsearch\Client;
use Elastic\Elasticsearch\ClientBuilder;

class ElasticSearchClientFactory
{
Expand All @@ -39,15 +39,20 @@ public function __construct(ElasticSearchConfig $config)

public function create(): Client
{
$clientBuilder = ClientBuilder::create();

if ($this->config->getElasticCloudId()) {
return ClientBuilder::create()
return $clientBuilder
->setElasticCloudId($this->config->getElasticCloudId())
->setApiKey($this->config->getElasticCloudApiKeyId(), $this->config->getElasticCloudApiKey())
->build();
}

return ClientBuilder::create()
->setHosts($this->config->getHosts())
->build();
$clientBuilder->setHosts($this->config->getHosts());
if ($this->config->getUsername() && $this->config->getPassword()) {
$clientBuilder->setBasicAuthentication($this->config->getUsername(), $this->config->getPassword());
}

return $clientBuilder->build();
}
}
27 changes: 26 additions & 1 deletion model/SearchEngine/Driver/Elasticsearch/ElasticSearchConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class ElasticSearchConfig
public const OPTION_ELASTIC_CLOUD_API_KEY_ID = 'elastic_cloud_api_key_id';
public const OPTION_ELASTIC_CLOUD_API_KEY = 'elastic_cloud_api_key';
public const OPTION_HOSTS = 'hosts';
public const OPTION_USERNAME = 'user';
public const OPTION_PASSWORD = 'pass';

/** @var ServiceOptionsInterface */
private $serviceOptions;
Expand All @@ -44,7 +46,25 @@ public function __construct(ServiceOptionsInterface $serviceOptions)

public function getHosts(): array
{
return $this->serviceOptions->get(self::class, self::OPTION_HOSTS);
$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;
}

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

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

public function getIndexPrefix(): ?string
Expand All @@ -66,4 +86,9 @@ public function getElasticCloudApiKeyId(): ?string
{
return $this->serviceOptions->get(self::class, self::OPTION_ELASTIC_CLOUD_API_KEY_ID);
}

private function getFirstHost(): ?array
{
return current($this->serviceOptions->get(self::class, self::OPTION_HOSTS)) ?: null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
namespace oat\taoAdvancedSearch\model\SearchEngine\Driver\Elasticsearch;

use oat\tao\model\search\index\IndexDocument;
use Elasticsearch\Client;
use Elastic\Elasticsearch\Client;
use oat\taoAdvancedSearch\model\SearchEngine\Contract\IndexerInterface;
use oat\taoAdvancedSearch\model\SearchEngine\Service\IndexPrefixer;
use Psr\Log\LoggerInterface;
Expand Down
2 changes: 1 addition & 1 deletion model/SearchEngine/Driver/Elasticsearch/IndexUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

namespace oat\taoAdvancedSearch\model\SearchEngine\Driver\Elasticsearch;

use Elasticsearch\Client;
use Elastic\Elasticsearch\Client;
use oat\oatbox\service\ConfigurableService;
use oat\tao\model\search\index\IndexUpdaterInterface;
use oat\taoAdvancedSearch\model\SearchEngine\Contract\IndexerInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

namespace oat\taoAdvancedSearch\model\SearchEngine\ServiceProvider;

use Elasticsearch\Client;
use Elastic\Elasticsearch\Client;
use oat\generis\model\data\permission\PermissionInterface;
use oat\generis\model\DependencyInjection\ContainerServiceProviderInterface;
use oat\generis\model\DependencyInjection\ServiceOptions;
Expand Down
2 changes: 1 addition & 1 deletion scripts/tools/Activate.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

namespace oat\taoAdvancedSearch\scripts\tools;

use Elasticsearch\Common\Exceptions\BadRequest400Exception;
use BadRequest400Exception;
use Exception;
use oat\generis\model\DependencyInjection\ServiceOptions;
use oat\oatbox\extension\script\ScriptAction;
Expand Down
59 changes: 59 additions & 0 deletions scripts/tools/AliasesUpdater.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2024 (original work) Open Assessment Technologies SA;
*
*/

declare(strict_types=1);

namespace oat\taoAdvancedSearch\scripts\tools;

use Exception;
use oat\oatbox\extension\script\ScriptAction;
use oat\oatbox\reporting\Report;
use oat\tao\model\search\SearchProxy;
use oat\taoAdvancedSearch\model\SearchEngine\Driver\Elasticsearch\ElasticSearch;

class AliasesUpdater extends ScriptAction
{
protected function provideOptions()
{
return [];
}

protected function provideDescription()
{
return 'Update aliases for all indexes';
}

protected function run()
{
try {
/** @var SearchProxy $searchProxy */
$searchProxy = $this->getServiceManager()->get(SearchProxy::SERVICE_ID);

/** @var ElasticSearch|null $elasticService */
$elasticService = $searchProxy->getAdvancedSearch();
$elasticService->updateAliases();

return Report::createSuccess('Elastic aliases updated successfully');
} catch (Exception $exception) {
return Report::createError(sprintf('Error while alias creation: %s', $exception->getMessage()));
}
}
}
1 change: 1 addition & 0 deletions scripts/tools/IndexCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ protected function run()
/** @var ElasticSearch|null $elasticService */
$elasticService = $searchProxy->getAdvancedSearch();
$elasticService->createIndexes();
$elasticService->updateAliases();

return Report::createSuccess('Elastic indices created successfully');
} catch (Exception $exception) {
Expand Down
Loading

0 comments on commit 4c4e685

Please sign in to comment.