Skip to content

Commit 0a7fb69

Browse files
authored
Merge pull request #28 from MacFJA/fix-no-content
Fix error when parsing response from Search with NOCONTENT flag
2 parents 86ae611 + 3f97b12 commit 0a7fb69

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010

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

1415
## [2.1.0]
1516

src/Redis/Command/Search.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,9 @@ public function parseResponse($data)
327327
$chunkSize = 2 // Hash + fields
328328
+ (true === $useScores ? 1 : 0)
329329
+ (true === $usePayloads ? 1 : 0)
330-
+ (true === $useSortKeys ? 1 : 0);
330+
+ (true === $useSortKeys ? 1 : 0)
331+
- (true === $noContent ? 1 : 0)
332+
;
331333

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

tests/Redis/Command/SearchTest.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
use MacFJA\RediSearch\Redis\Command\AbstractCommand;
2525
use MacFJA\RediSearch\Redis\Command\Search;
2626
use MacFJA\RediSearch\Redis\Command\SearchCommand\FilterOption;
27+
use MacFJA\RediSearch\Redis\Response\PaginatedResponse;
28+
use MacFJA\RediSearch\Redis\Response\SearchResponseItem;
2729
use PHPUnit\Framework\TestCase;
2830

2931
/**
@@ -38,6 +40,8 @@
3840
*
3941
* @covers \MacFJA\RediSearch\Redis\Command\SearchCommand\SummarizeOption
4042
*
43+
* @covers \MacFJA\RediSearch\Redis\Response\PaginatedResponse
44+
*
4145
* @uses \MacFJA\RediSearch\Redis\Command\Option\AbstractCommandOption
4246
* @uses \MacFJA\RediSearch\Redis\Command\Option\CustomValidatorOption
4347
* @uses \MacFJA\RediSearch\Redis\Command\Option\DecoratedOptionAwareTrait
@@ -142,4 +146,92 @@ public function testNoSummarizeAndNoHighlight(): void
142146
'LIMIT', 12, 10,
143147
], $command->getArguments());
144148
}
149+
150+
public function testParsingResponseNoContent(): void
151+
{
152+
$redisResponse = [
153+
3,
154+
'hash_1',
155+
'hash_2',
156+
'hash_3',
157+
];
158+
159+
$command = new Search(AbstractCommand::MAX_IMPLEMENTED_VERSION);
160+
$command->setQuery('*');
161+
$command->setNoContent();
162+
163+
$expectedResponse = new PaginatedResponse($command, 3, [
164+
new SearchResponseItem('hash_1'),
165+
new SearchResponseItem('hash_2'),
166+
new SearchResponseItem('hash_3'),
167+
]);
168+
169+
$actualResponse = $command->parseResponse($redisResponse);
170+
171+
static::assertEquals($expectedResponse, $actualResponse);
172+
}
173+
174+
public function testParsingResponseScore(): void
175+
{
176+
$redisResponse = [
177+
3,
178+
'hash_1',
179+
'0.741',
180+
['field', '1'],
181+
'hash_2',
182+
'0.654',
183+
['field', '2'],
184+
'hash_3',
185+
'0.258',
186+
['field', '3'],
187+
];
188+
189+
$command = new Search(AbstractCommand::MAX_IMPLEMENTED_VERSION);
190+
$command->setQuery('*');
191+
$command->setWithScores();
192+
193+
$expectedResponse = new PaginatedResponse($command, 3, [
194+
new SearchResponseItem('hash_1', ['field' => '1'], 0.741),
195+
new SearchResponseItem('hash_2', ['field' => '2'], 0.654),
196+
new SearchResponseItem('hash_3', ['field' => '3'], 0.258),
197+
]);
198+
199+
$actualResponse = $command->parseResponse($redisResponse);
200+
201+
static::assertEquals($expectedResponse, $actualResponse);
202+
}
203+
204+
public function testParsingResponseScoreAndPayload(): void
205+
{
206+
$redisResponse = [
207+
3,
208+
'hash_1',
209+
'0.741',
210+
null,
211+
['field', '1'],
212+
'hash_2',
213+
'0.654',
214+
null,
215+
['field', '2', 'other', 'field'],
216+
'hash_3',
217+
'0.258',
218+
'third',
219+
['field', '3'],
220+
];
221+
222+
$command = new Search(AbstractCommand::MAX_IMPLEMENTED_VERSION);
223+
$command->setQuery('*');
224+
$command->setWithScores();
225+
$command->setWithPayloads();
226+
227+
$expectedResponse = new PaginatedResponse($command, 3, [
228+
new SearchResponseItem('hash_1', ['field' => '1'], 0.741),
229+
new SearchResponseItem('hash_2', ['field' => '2', 'other' => 'field'], 0.654),
230+
new SearchResponseItem('hash_3', ['field' => '3'], 0.258, 'third'),
231+
]);
232+
233+
$actualResponse = $command->parseResponse($redisResponse);
234+
235+
static::assertEquals($expectedResponse, $actualResponse);
236+
}
145237
}

0 commit comments

Comments
 (0)