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

Add new search parameters highlightPreTag, highlightPostTag and cropM… #318

Merged
merged 3 commits into from
May 4, 2022
Merged
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
94 changes: 94 additions & 0 deletions tests/Endpoints/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,100 @@ public function testExceptionIfNoIndexWhenSearching(): void
$index->search('prince');
}

public function testParametersCropMarker(): void
{
$response = $this->index->search('blood', [
'limit' => 1,
'attributesToCrop' => ['title'],
'cropLength' => 2,
]);

$this->assertArrayHasKey('_formatted', $response->getHit(0));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why here did you use $response->getHit(0) and in the assertion below you opted to use $response['hits'][0]

Copy link
Contributor Author

@alallema alallema May 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use $response['hits'] on the result of the rawSearch function who returns array instead of SearchResult. The getter can be called on an array:
See here the rawSearch function

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer getters! Maybe it is worth creating an issue to change this consistently throughout the tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rawSearch is here to access your specific fields in results I find it really useful, I used it in tests when I need to check the exact field of it. Don't think it's not consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we can create a specific class for the movies result or the books result in test

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, but is there any specific reason to use the raw result?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rawResult gives me access to the subfields of _formatted because it is an array.I used it for check the cropMarker

$this->assertSame('…Half-Blood…', $response->getHit(0)['_formatted']['title']);

$response = $this->index->search('blood', [
'limit' => 1,
'attributesToCrop' => ['title'],
'cropLength' => 2,
], [
'raw' => true,
]);

$this->assertArrayHasKey('_formatted', $response['hits'][0]);
$this->assertSame('…Half-Blood…', $response['hits'][0]['_formatted']['title']);
bidoubiwa marked this conversation as resolved.
Show resolved Hide resolved
}

public function testParametersWithCustomizedCropMarker(): void
{
$response = $this->index->search('blood', [
'limit' => 1,
'attributesToCrop' => ['title'],
'cropLength' => 3,
'cropMarker' => '(ꈍᴗꈍ)',
]);

$this->assertArrayHasKey('_formatted', $response->getHit(0));
$this->assertSame('(ꈍᴗꈍ)Half-Blood Prince', $response->getHit(0)['_formatted']['title']);

$response = $this->index->search('blood', [
'limit' => 1,
'attributesToCrop' => ['title'],
'cropLength' => 3,
'cropMarker' => '(ꈍᴗꈍ)',
], [
'raw' => true,
]);

$this->assertArrayHasKey('_formatted', $response['hits'][0]);
$this->assertSame('(ꈍᴗꈍ)Half-Blood Prince', $response['hits'][0]['_formatted']['title']);
bidoubiwa marked this conversation as resolved.
Show resolved Hide resolved
}

public function testParametersWithHighlightTag(): void
{
$response = $this->index->search('and', [
'limit' => 1,
'attributesToHighlight' => ['*'],
]);

$this->assertArrayHasKey('_formatted', $response->getHit(0));
$this->assertSame('Pride <em>and</em> Prejudice', $response->getHit(0)['_formatted']['title']);

$response = $this->index->search('and', [
'limit' => 1,
'attributesToHighlight' => ['*'],
], [
'raw' => true,
]);

$this->assertArrayHasKey('_formatted', $response['hits'][0]);
$this->assertSame('Pride <em>and</em> Prejudice', $response['hits'][0]['_formatted']['title']);
}

public function testParametersWithCustomizedHighlightTag(): void
{
$response = $this->index->search('and', [
'limit' => 1,
'attributesToHighlight' => ['*'],
'highlightPreTag' => '(⊃。•́‿•̀。)⊃ ',
'highlightPostTag' => ' ⊂(´• ω •`⊂)',
]);

$this->assertArrayHasKey('_formatted', $response->getHit(0));
$this->assertSame('Pride (⊃。•́‿•̀。)⊃ and ⊂(´• ω •`⊂) Prejudice', $response->getHit(0)['_formatted']['title']);

$response = $this->index->search('and', [
'limit' => 1,
'attributesToHighlight' => ['*'],
'highlightPreTag' => '(⊃。•́‿•̀。)⊃ ',
'highlightPostTag' => ' ⊂(´• ω •`⊂)',
], [
'raw' => true,
]);

$this->assertArrayHasKey('_formatted', $response['hits'][0]);
$this->assertSame('Pride (⊃。•́‿•̀。)⊃ and ⊂(´• ω •`⊂) Prejudice', $response['hits'][0]['_formatted']['title']);
}

public function testParametersArray(): void
{
$response = $this->index->updateFilterableAttributes(['title']);
Expand Down