Skip to content

Improvement for \MacFJA\RediSearch\Index class #47

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

Merged
merged 1 commit into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Throw custom exception is client is missing
- Add compatibility for Predis version 2.x
- (dev) Add `make clean` to remove all generated files
- (dev) Add code coverage to `\MacFJA\RediSearch\Index`
- (dev) Add integration test for document insertion

### Fixed

- Missing default values on highlight option ([Issue#38])
- Don't throw an exception if the result of the index definition is incorrect ([Issue#46])

### Changed

- Rework paginated responses
- Add warning for Predis commands initialization
- Lazy load RediSearch version and index information in `\MacFJA\RediSearch\Index`
- Allow to provide RediSearch version manually to `\MacFJA\RediSearch\Index`
- (dev) Remove deprecated `--no-suggest` option of Composer in the `Makefile`
- (dev) Update PHP-CS-Fixer version

Expand Down Expand Up @@ -227,6 +232,7 @@ First version
[Issue#26]: https://github.com/MacFJA/php-redisearch/issues/26
[Issue#38]: https://github.com/MacFJA/php-redisearch/issues/38
[Issue#39]: https://github.com/MacFJA/php-redisearch/issues/39
[Issue#46]: https://github.com/MacFJA/php-redisearch/issues/46
[PR#1]: https://github.com/MacFJA/php-redisearch/pull/1
[PR#3]: https://github.com/MacFJA/php-redisearch/pull/3
[PR#8]: https://github.com/MacFJA/php-redisearch/pull/8
Expand Down
39 changes: 23 additions & 16 deletions src/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,37 +38,33 @@
use MacFJA\RediSearch\Redis\Initializer;
use MacFJA\RediSearch\Redis\Response\InfoResponse;

/**
* @codeCoverageIgnore
*/
class Index
{
/** @var Client */
private $client;

/** @var InfoResponse */
/** @var null|InfoResponse */
private $info;

/** @var string */
private $index;

/** @var string */
/** @var null|string */
private $version;

public function __construct(string $index, Client $client)
public function __construct(string $index, Client $client, ?string $version = null)
{
$this->client = $client;
$this->index = $index;
$this->version = Initializer::getRediSearchVersion($client) ?? AbstractCommand::MIN_IMPLEMENTED_VERSION;
$this->getInfo();
$this->version = $version;
}

/**
* @param array<string,float|int|string> $properties
*/
public function addDocumentFromArray(array $properties, ?string $hash = null): string
{
$prefixes = $this->info->getIndexDefinition('prefixes');
$prefixes = $this->getInfo()->getIndexDefinition('prefixes');
$prefix = '';
if (is_array($prefixes) && count($prefixes) > 0) {
$prefix = (string) reset($prefixes);
Expand All @@ -94,7 +90,7 @@ public function deleteDocument(string $hash): bool

public function addField(CreateCommandFieldOption $field): bool
{
$command = new Alter($this->version);
$command = new Alter($this->getVersion());
$command
->setIndex($this->index)
->addField($field)
Expand All @@ -105,7 +101,7 @@ public function addField(CreateCommandFieldOption $field): bool

public function delete(bool $withDocuments = false): bool
{
$command = new DropIndex($this->version);
$command = new DropIndex($this->getVersion());
$command->setIndex($this->index)
->setDeleteDocument($withDocuments)
;
Expand All @@ -116,34 +112,45 @@ public function delete(bool $withDocuments = false): bool
public function addAlias(string $alias): bool
{
return 'OK' === (string) $this->client->execute(
(new AliasAdd($this->version))
(new AliasAdd($this->getVersion()))
->setIndex($this->index)
->setAlias($alias)
);
}

public function updateAlias(string $alias): bool
{
return 'OK' === (string) $this->client->execute((new AliasUpdate($this->version))->setIndex($this->index)->setAlias($alias));
return 'OK' === (string) $this->client->execute((new AliasUpdate($this->getVersion()))->setIndex($this->index)->setAlias($alias));
}

public function deleteAlias(string $alias): bool
{
return 'OK' === (string) $this->client->execute((new AliasDel($this->version))->setAlias($alias));
return 'OK' === (string) $this->client->execute((new AliasDel($this->getVersion()))->setAlias($alias));
}

/**
* @return array<string>
*/
public function getTagValues(string $fieldName): array
{
return $this->client->execute((new TagVals($this->version))->setIndex($this->index)->setField($fieldName));
return $this->client->execute((new TagVals($this->getVersion()))->setIndex($this->index)->setField($fieldName));
}

public function getInfo(): InfoResponse
{
$this->info = $this->client->execute((new Info($this->version))->setIndex($this->index));
if (!($this->info instanceof InfoResponse)) {
$this->info = $this->client->execute((new Info($this->getVersion()))->setIndex($this->index));
}

return $this->info;
}

private function getVersion(): string
{
if (!is_string($this->version)) {
$this->version = Initializer::getRediSearchVersion($this->client) ?? AbstractCommand::MIN_IMPLEMENTED_VERSION;
}

return $this->version;
}
}
4 changes: 3 additions & 1 deletion src/Redis/Response/InfoResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ public function getFieldsAsOption(): array
public function getIndexDefinition(?string $key = null)
{
$rawDefinitions = self::getValue($this->rawLines, 'index_definition');
assert(is_array($rawDefinitions));
if (!is_array($rawDefinitions)) {
return is_string($key) ? null : [];
}
$data = self::getPairs($rawDefinitions);
if (!is_string($key)) {
return $data;
Expand Down
Loading