Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/fun 1740/update elastic lib #121

Merged
merged 11 commits into from
Aug 8, 2024
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": "dev-feat/FUN-1740/update-guzzle-client as 99.99.99",
tikhanovichA marked this conversation as resolved.
Show resolved Hide resolved
"oat-sa/extension-tao-testqti": "dev-feat/FUN-1740/update-guzzle-client as 99.99.99",
"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)[]
],
],
];
16 changes: 8 additions & 8 deletions model/SearchEngine/Contract/IndexerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@

interface IndexerInterface
{
public const ITEMS_INDEX = 'items';
public const TESTS_INDEX = 'tests';
public const TEST_TAKERS_INDEX = 'test-takers';
public const DELIVERIES_INDEX = 'deliveries';
public const DELIVERY_RESULTS_INDEX = 'delivery-results';
public const GROUPS_INDEX = 'groups';
public const ASSETS_INDEX = 'assets';
public const PROPERTY_LIST = 'property-list';
public const ITEMS_INDEX = 'items_alias';
public const TESTS_INDEX = 'tests_alias';
public const TEST_TAKERS_INDEX = 'test-takers_alias';
public const DELIVERIES_INDEX = 'deliveries_alias';
public const DELIVERY_RESULTS_INDEX = 'delivery-results_alias';
public const GROUPS_INDEX = 'groups_alias';
public const ASSETS_INDEX = 'assets_alias';
public const PROPERTY_LIST = 'property-list_alias';
public const UNCLASSIFIEDS_DOCUMENTS_INDEX = 'unclassifieds';

public const MEDIA_CLASS_URI = 'http://www.tao.lu/Ontologies/TAOMedia.rdf#Media';
Expand Down
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 = [];
gabrielfs7 marked this conversation as resolved.
Show resolved Hide resolved
tikhanovichA marked this conversation as resolved.
Show resolved Hide resolved
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
tikhanovichA marked this conversation as resolved.
Show resolved Hide resolved
tikhanovichA marked this conversation as resolved.
Show resolved Hide resolved
{
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
Loading
Loading