Skip to content

Fix error when parsing response from Search with NOCONTENT flag #28

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 2 commits into from
Feb 5, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Summarize Search option enabled by default (without possibility to disable it) ([Issue#26])
- Highlight Search option enabled by default (without possibility to disable it) ([Issue#26])
- Fix error when parsing response from Search with NOCONTENT flag

## [2.1.0]

Expand Down
4 changes: 3 additions & 1 deletion src/Redis/Command/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,9 @@ public function parseResponse($data)
$chunkSize = 2 // Hash + fields
+ (true === $useScores ? 1 : 0)
+ (true === $usePayloads ? 1 : 0)
+ (true === $useSortKeys ? 1 : 0);
+ (true === $useSortKeys ? 1 : 0)
- (true === $noContent ? 1 : 0)
;

$documents = array_chunk($data, $chunkSize);

Expand Down
92 changes: 92 additions & 0 deletions tests/Redis/Command/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
use MacFJA\RediSearch\Redis\Command\AbstractCommand;
use MacFJA\RediSearch\Redis\Command\Search;
use MacFJA\RediSearch\Redis\Command\SearchCommand\FilterOption;
use MacFJA\RediSearch\Redis\Response\PaginatedResponse;
use MacFJA\RediSearch\Redis\Response\SearchResponseItem;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -38,6 +40,8 @@
*
* @covers \MacFJA\RediSearch\Redis\Command\SearchCommand\SummarizeOption
*
* @covers \MacFJA\RediSearch\Redis\Response\PaginatedResponse
*
* @uses \MacFJA\RediSearch\Redis\Command\Option\AbstractCommandOption
* @uses \MacFJA\RediSearch\Redis\Command\Option\CustomValidatorOption
* @uses \MacFJA\RediSearch\Redis\Command\Option\DecoratedOptionAwareTrait
Expand Down Expand Up @@ -142,4 +146,92 @@ public function testNoSummarizeAndNoHighlight(): void
'LIMIT', 12, 10,
], $command->getArguments());
}

public function testParsingResponseNoContent(): void
{
$redisResponse = [
3,
'hash_1',
'hash_2',
'hash_3',
];

$command = new Search(AbstractCommand::MAX_IMPLEMENTED_VERSION);
$command->setQuery('*');
$command->setNoContent();

$expectedResponse = new PaginatedResponse($command, 3, [
new SearchResponseItem('hash_1'),
new SearchResponseItem('hash_2'),
new SearchResponseItem('hash_3'),
]);

$actualResponse = $command->parseResponse($redisResponse);

static::assertEquals($expectedResponse, $actualResponse);
}

public function testParsingResponseScore(): void
{
$redisResponse = [
3,
'hash_1',
'0.741',
['field', '1'],
'hash_2',
'0.654',
['field', '2'],
'hash_3',
'0.258',
['field', '3'],
];

$command = new Search(AbstractCommand::MAX_IMPLEMENTED_VERSION);
$command->setQuery('*');
$command->setWithScores();

$expectedResponse = new PaginatedResponse($command, 3, [
new SearchResponseItem('hash_1', ['field' => '1'], 0.741),
new SearchResponseItem('hash_2', ['field' => '2'], 0.654),
new SearchResponseItem('hash_3', ['field' => '3'], 0.258),
]);

$actualResponse = $command->parseResponse($redisResponse);

static::assertEquals($expectedResponse, $actualResponse);
}

public function testParsingResponseScoreAndPayload(): void
{
$redisResponse = [
3,
'hash_1',
'0.741',
null,
['field', '1'],
'hash_2',
'0.654',
null,
['field', '2', 'other', 'field'],
'hash_3',
'0.258',
'third',
['field', '3'],
];

$command = new Search(AbstractCommand::MAX_IMPLEMENTED_VERSION);
$command->setQuery('*');
$command->setWithScores();
$command->setWithPayloads();

$expectedResponse = new PaginatedResponse($command, 3, [
new SearchResponseItem('hash_1', ['field' => '1'], 0.741),
new SearchResponseItem('hash_2', ['field' => '2', 'other' => 'field'], 0.654),
new SearchResponseItem('hash_3', ['field' => '3'], 0.258, 'third'),
]);

$actualResponse = $command->parseResponse($redisResponse);

static::assertEquals($expectedResponse, $actualResponse);
}
}