Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #317 from meilisearch/support-nested-field
Ensure nested field support
- Loading branch information
Showing
2 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Endpoints; | ||
|
||
use MeiliSearch\Endpoints\Indexes; | ||
use Tests\TestCase; | ||
|
||
final class SearchTestNestedFields extends TestCase | ||
{ | ||
private Indexes $index; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->index = $this->createEmptyIndex('nestedIndex'); | ||
$promise = $this->index->updateDocuments(self::NESTED_DOCUMENTS); | ||
$this->index->waitForTask($promise['uid']); | ||
} | ||
|
||
public function testBasicSearchOnNestedFields(): void | ||
{ | ||
$response = $this->index->search('An awesome'); | ||
|
||
$this->assertArrayHasKey('hits', $response->toArray()); | ||
$this->assertCount(1, $response->getHits()); | ||
|
||
$response = $this->index->search('An awesome', [], [ | ||
'raw' => true, | ||
]); | ||
|
||
$this->assertArrayHasKey('hits', $response); | ||
$this->assertSame(1, $response['nbHits']); | ||
$this->assertSame(5, $response['hits'][0]['id']); | ||
} | ||
|
||
public function testSearchOnNestedFieldWithMultiplesResultsOnNestedFields(): void | ||
{ | ||
$response = $this->index->search('book'); | ||
|
||
$this->assertArrayHasKey('hits', $response->toArray()); | ||
$this->assertCount(6, $response->getHits()); | ||
|
||
$response = $this->index->search('book', [], [ | ||
'raw' => true, | ||
]); | ||
|
||
$this->assertArrayHasKey('hits', $response); | ||
$this->assertSame(6, $response['nbHits']); | ||
$this->assertSame(4, $response['hits'][0]['id']); | ||
} | ||
|
||
public function testSearchOnNestedFieldWithOptions(): void | ||
{ | ||
$response = $this->index->search('book', ['limit' => 1]); | ||
|
||
$this->assertCount(1, $response->getHits()); | ||
|
||
$response = $this->index->search('book', ['limit' => 1], [ | ||
'raw' => true, | ||
]); | ||
|
||
$this->assertCount(1, $response['hits']); | ||
$this->assertSame(4, $response['hits'][0]['id']); | ||
} | ||
|
||
public function testSearchOnNestedFieldWithSearchableAtributes(): void | ||
{ | ||
$response = $this->index->updateSearchableAttributes(['title', 'info.comment']); | ||
$this->index->waitForTask($response['uid']); | ||
|
||
$response = $this->index->search('An awesome'); | ||
|
||
$this->assertArrayHasKey('hits', $response->toArray()); | ||
$this->assertCount(1, $response->getHits()); | ||
|
||
$response = $this->index->search('An awesome', [], [ | ||
'raw' => true, | ||
]); | ||
|
||
$this->assertArrayHasKey('hits', $response); | ||
$this->assertSame(1, $response['nbHits']); | ||
$this->assertSame(5, $response['hits'][0]['id']); | ||
} | ||
|
||
public function testSearchOnNestedFieldWithSortableAtributes(): void | ||
{ | ||
$response = $this->index->updateSortableAttributes(['info.reviewNb']); | ||
$this->index->waitForTask($response['uid']); | ||
|
||
$response = $this->index->search('An awesome'); | ||
|
||
$this->assertArrayHasKey('hits', $response->toArray()); | ||
$this->assertCount(1, $response->getHits()); | ||
|
||
$response = $this->index->search('An awesome', [ | ||
'sort' => ['info.reviewNb:desc'], | ||
], | ||
[ | ||
'raw' => true, | ||
]); | ||
|
||
$this->assertArrayHasKey('hits', $response); | ||
$this->assertSame(1, $response['nbHits']); | ||
$this->assertSame(5, $response['hits'][0]['id']); | ||
} | ||
|
||
public function testSearchOnNestedFieldWithSortableAtributesAndSearchableAttributes(): void | ||
{ | ||
$response = $this->index->updateSettings([ | ||
'searchableAttributes' => ['title', 'info.comment'], | ||
'sortableAttributes' => ['info.reviewNb'], | ||
]); | ||
$this->index->waitForTask($response['uid']); | ||
|
||
$response = $this->index->search('An awesome'); | ||
|
||
$this->assertArrayHasKey('hits', $response->toArray()); | ||
$this->assertCount(1, $response->getHits()); | ||
|
||
$response = $this->index->search('An awesome', [ | ||
'sort' => ['info.reviewNb:desc'], | ||
], | ||
[ | ||
'raw' => true, | ||
]); | ||
|
||
$this->assertArrayHasKey('hits', $response); | ||
$this->assertSame(1, $response['nbHits']); | ||
$this->assertSame(5, $response['hits'][0]['id']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters