Skip to content

Commit a56cf5f

Browse files
authored
Merge pull request #47 from MacFJA/handle-missing-data-issue-46
Improvement for `\MacFJA\RediSearch\Index` class
2 parents f8612d4 + a90422b commit a56cf5f

File tree

6 files changed

+371
-18
lines changed

6 files changed

+371
-18
lines changed

Diff for: CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- Throw custom exception is client is missing
1313
- Add compatibility for Predis version 2.x
1414
- (dev) Add `make clean` to remove all generated files
15+
- (dev) Add code coverage to `\MacFJA\RediSearch\Index`
16+
- (dev) Add integration test for document insertion
1517

1618
### Fixed
1719

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

2023
### Changed
2124

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

@@ -227,6 +232,7 @@ First version
227232
[Issue#26]: https://github.com/MacFJA/php-redisearch/issues/26
228233
[Issue#38]: https://github.com/MacFJA/php-redisearch/issues/38
229234
[Issue#39]: https://github.com/MacFJA/php-redisearch/issues/39
235+
[Issue#46]: https://github.com/MacFJA/php-redisearch/issues/46
230236
[PR#1]: https://github.com/MacFJA/php-redisearch/pull/1
231237
[PR#3]: https://github.com/MacFJA/php-redisearch/pull/3
232238
[PR#8]: https://github.com/MacFJA/php-redisearch/pull/8

Diff for: src/Index.php

+23-16
Original file line numberDiff line numberDiff line change
@@ -38,37 +38,33 @@
3838
use MacFJA\RediSearch\Redis\Initializer;
3939
use MacFJA\RediSearch\Redis\Response\InfoResponse;
4040

41-
/**
42-
* @codeCoverageIgnore
43-
*/
4441
class Index
4542
{
4643
/** @var Client */
4744
private $client;
4845

49-
/** @var InfoResponse */
46+
/** @var null|InfoResponse */
5047
private $info;
5148

5249
/** @var string */
5350
private $index;
5451

55-
/** @var string */
52+
/** @var null|string */
5653
private $version;
5754

58-
public function __construct(string $index, Client $client)
55+
public function __construct(string $index, Client $client, ?string $version = null)
5956
{
6057
$this->client = $client;
6158
$this->index = $index;
62-
$this->version = Initializer::getRediSearchVersion($client) ?? AbstractCommand::MIN_IMPLEMENTED_VERSION;
63-
$this->getInfo();
59+
$this->version = $version;
6460
}
6561

6662
/**
6763
* @param array<string,float|int|string> $properties
6864
*/
6965
public function addDocumentFromArray(array $properties, ?string $hash = null): string
7066
{
71-
$prefixes = $this->info->getIndexDefinition('prefixes');
67+
$prefixes = $this->getInfo()->getIndexDefinition('prefixes');
7268
$prefix = '';
7369
if (is_array($prefixes) && count($prefixes) > 0) {
7470
$prefix = (string) reset($prefixes);
@@ -94,7 +90,7 @@ public function deleteDocument(string $hash): bool
9490

9591
public function addField(CreateCommandFieldOption $field): bool
9692
{
97-
$command = new Alter($this->version);
93+
$command = new Alter($this->getVersion());
9894
$command
9995
->setIndex($this->index)
10096
->addField($field)
@@ -105,7 +101,7 @@ public function addField(CreateCommandFieldOption $field): bool
105101

106102
public function delete(bool $withDocuments = false): bool
107103
{
108-
$command = new DropIndex($this->version);
104+
$command = new DropIndex($this->getVersion());
109105
$command->setIndex($this->index)
110106
->setDeleteDocument($withDocuments)
111107
;
@@ -116,34 +112,45 @@ public function delete(bool $withDocuments = false): bool
116112
public function addAlias(string $alias): bool
117113
{
118114
return 'OK' === (string) $this->client->execute(
119-
(new AliasAdd($this->version))
115+
(new AliasAdd($this->getVersion()))
120116
->setIndex($this->index)
121117
->setAlias($alias)
122118
);
123119
}
124120

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

130126
public function deleteAlias(string $alias): bool
131127
{
132-
return 'OK' === (string) $this->client->execute((new AliasDel($this->version))->setAlias($alias));
128+
return 'OK' === (string) $this->client->execute((new AliasDel($this->getVersion()))->setAlias($alias));
133129
}
134130

135131
/**
136132
* @return array<string>
137133
*/
138134
public function getTagValues(string $fieldName): array
139135
{
140-
return $this->client->execute((new TagVals($this->version))->setIndex($this->index)->setField($fieldName));
136+
return $this->client->execute((new TagVals($this->getVersion()))->setIndex($this->index)->setField($fieldName));
141137
}
142138

143139
public function getInfo(): InfoResponse
144140
{
145-
$this->info = $this->client->execute((new Info($this->version))->setIndex($this->index));
141+
if (!($this->info instanceof InfoResponse)) {
142+
$this->info = $this->client->execute((new Info($this->getVersion()))->setIndex($this->index));
143+
}
146144

147145
return $this->info;
148146
}
147+
148+
private function getVersion(): string
149+
{
150+
if (!is_string($this->version)) {
151+
$this->version = Initializer::getRediSearchVersion($this->client) ?? AbstractCommand::MIN_IMPLEMENTED_VERSION;
152+
}
153+
154+
return $this->version;
155+
}
149156
}

Diff for: src/Redis/Response/InfoResponse.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,9 @@ public function getFieldsAsOption(): array
171171
public function getIndexDefinition(?string $key = null)
172172
{
173173
$rawDefinitions = self::getValue($this->rawLines, 'index_definition');
174-
assert(is_array($rawDefinitions));
174+
if (!is_array($rawDefinitions)) {
175+
return is_string($key) ? null : [];
176+
}
175177
$data = self::getPairs($rawDefinitions);
176178
if (!is_string($key)) {
177179
return $data;

0 commit comments

Comments
 (0)