Skip to content

Commit

Permalink
Merge pull request #317 from meilisearch/support-nested-field
Browse files Browse the repository at this point in the history
Ensure nested field support
  • Loading branch information
alallema committed Apr 25, 2022
2 parents 624d8ff + ef78b5b commit d570136
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
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

0 comments on commit d570136

Please sign in to comment.