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

Ensure nested field support #317

Merged
merged 2 commits into from Apr 25, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
133 changes: 133 additions & 0 deletions tests/Endpoints/SearchTestNestedFields.php
@@ -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']);
}
}
10 changes: 10 additions & 0 deletions tests/TestCase.php
Expand Up @@ -20,6 +20,16 @@ abstract class TestCase extends BaseTestCase
['id' => 42, 'title' => 'The Hitchhiker\'s Guide to the Galaxy'],
];

protected const NESTED_DOCUMENTS = [
['id' => 1, 'title' => 'Pride and Prejudice', 'info' => ['comment' => 'A great book', 'reviewNb' => 50]],
['id' => 2, 'title' => 'Le Petit Prince', 'info' => ['comment' => 'A french book', 'reviewNb' => 600]],
['id' => 3, 'title' => 'Le Rouge et le Noir', 'info' => ['comment' => 'Another french book', 'reviewNb' => 700]],
['id' => 4, 'title' => 'Alice In Wonderland', 'comment' => 'A weird book', 'info' => ['comment' => 'A weird book', 'reviewNb' => 800]],
['id' => 5, 'title' => 'The Hobbit', 'info' => ['comment' => 'An awesome book', 'reviewNb' => 900]],
['id' => 6, 'title' => 'Harry Potter and the Half-Blood Prince', 'info' => ['comment' => 'The best book', 'reviewNb' => 1000]],
['id' => 7, 'title' => "The Hitchhiker's Guide to the Galaxy"],
];

protected const INFO_KEY = [
'actions' => ['search'],
'indexes' => ['index'],
Expand Down