Skip to content

Commit

Permalink
Upd package for MeiliSearch v0.11 (#35)
Browse files Browse the repository at this point in the history
* Upd version

* Add method to use faceting sub routes (#36)

* Implement faceting in search (#37)

* Change create_index prototype (#39)

* Add test with multiple facetFilters (#38)
  • Loading branch information
curquiza authored Jun 15, 2020
1 parent 13cc34d commit 81b67c8
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 63 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ Array
## 🤖 Compatibility with MeiliSearch

This package is compatible with the following MeiliSearch versions:
- `v0.10.X`
- `v0.11.X`

## 🎬 Examples

Expand All @@ -134,10 +134,10 @@ You can check out [the API documentation](https://docs.meilisearch.com/reference
// Create an index
$index = $client->createIndex('books');
// Create an index and give the primary-key
$index = $client->createIndex([
'uid' => 'books',
'primaryKey' => 'book_id'
]);
$index = $client->createIndex(
'books',
['primaryKey' => 'book_id']
);
```

#### List all indexes <!-- omit in toc -->
Expand Down
11 changes: 5 additions & 6 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@ public function getIndex($uid)
return $this->indexInstance($uid);
}

public function createIndex($attributes)
public function createIndex($index_uid, $options = [])
{
if (is_array($attributes)) {
$body = $attributes;
} else {
$body = ['uid' => $attributes];
}
$body = array_merge(
['uid' => $index_uid],
$options
);
$response = $this->httpPost('/indexes', $body);
$uid = $response['uid'];

Expand Down
45 changes: 32 additions & 13 deletions src/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,12 @@ public function waitForPendingUpdate($update_id, $timeout_in_ms = 5000, $interva

// Search

public function search($query, $options = null)
public function search($query, array $options = [])
{
if ($options) {
$options = $this->flattenOptions($options);
$parameters = array_merge(['q' => $query], $options);
} else {
$parameters = ['q' => $query];
}
$parameters = array_merge(
['q' => $query],
$this->parseOptions($options)
);

return $this->httpGet('/indexes/'.$this->uid.'/search', $parameters);
}
Expand Down Expand Up @@ -257,14 +255,35 @@ public function updateAcceptNewFields($accept_new_fields)
return $this->httpPost('/indexes/'.$this->uid.'/settings/accept-new-fields', $accept_new_fields);
}

private function flattenOptions(array $options)
// Settings - Attributes for faceting

public function getAttributesForFaceting()
{
return array_map(function ($entry) {
if (is_array($entry)) {
return implode(',', $entry);
return $this->httpGet('/indexes/'.$this->uid.'/settings/attributes-for-faceting');
}

public function updateAttributesForFaceting($attributes_for_faceting)
{
return $this->httpPost('/indexes/'.$this->uid.'/settings/attributes-for-faceting', $attributes_for_faceting);
}

public function resetAttributesForFaceting()
{
return $this->httpDelete('/indexes/'.$this->uid.'/settings/attributes-for-faceting');
}

// PRIVATE

private function parseOptions(array $options)
{
foreach ($options as $key => $value) {
if ('facetsDistribution' === $key || 'facetFilters' === $key) {
$options[$key] = json_encode($value);
} elseif (is_array($value)) {
$options[$key] = implode(',', $value);
}
}

return $entry;
}, $options);
return $options;
}
}
2 changes: 1 addition & 1 deletion src/MeiliSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

class MeiliSearch
{
const VERSION = '0.10.2';
const VERSION = '0.11.0';
}
18 changes: 9 additions & 9 deletions tests/Endpoints/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public function testCreateIndexWithOnlyUid()

public function testCreateIndexWithUidAndPrimaryKey()
{
$index = $this->client->createIndex([
'uid' => 'index',
'primaryKey' => 'ObjectId',
]);
$index = $this->client->createIndex(
'index',
['primaryKey' => 'ObjectId'],
);

$this->assertInstanceOf(Index::class, $index);
$this->assertSame('index', $index->getUid());
Expand Down Expand Up @@ -64,10 +64,10 @@ public function testGetAllIndexes()
public function testShowIndex()
{
$index = 'index';
$this->client->createIndex([
'uid' => $index,
'primaryKey' => 'objectID',
]);
$this->client->createIndex(
$index,
['primaryKey' => 'objectID'],
);

$response = $this->client->showIndex($index);

Expand Down Expand Up @@ -138,7 +138,7 @@ public function testExceptionIfUidTakenWhenCreating()
public function testExceptionIfNoUidWhenCreating()
{
$this->expectException(HTTPRequestException::class);
$this->client->createIndex(['primaryKey' => 'id']);
$this->client->createIndex(null);
}

public function testExceptionIfNoIndexWhenShowing()
Expand Down
34 changes: 17 additions & 17 deletions tests/Endpoints/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,31 @@ public function setUp(): void

public function testGetPrimaryKey()
{
$indexB = $this->client->createIndex([
'uid' => 'indexB',
'primaryKey' => 'objectId',
]);
$indexB = $this->client->createIndex(
'indexB',
['primaryKey' => 'objectId'],
);

$this->assertNull($this->index->getPrimaryKey());
$this->assertSame('objectId', $indexB->getPrimaryKey());
}

public function testGetUid()
{
$indexB = $this->client->createIndex([
'uid' => 'indexB',
'primaryKey' => 'objectId',
]);
$indexB = $this->client->createIndex(
'indexB',
['primaryKey' => 'objectId'],
);
$this->assertSame('index', $this->index->getUid());
$this->assertSame('indexB', $indexB->getUid());
}

public function testShow()
{
$index = $this->client->createIndex([
'uid' => 'indexB',
'primaryKey' => 'objectId',
]);
$index = $this->client->createIndex(
'indexB',
['primaryKey' => 'objectId'],
);

$response = $index->show();

Expand All @@ -67,10 +67,10 @@ public function testPrimaryKeyUpdate()

public function testExceptionIsThrownWhenOverwritingPrimaryKey()
{
$index = $this->client->createIndex([
'uid' => 'indexB',
'primaryKey' => 'objectId',
]);
$index = $this->client->createIndex(
'indexB',
['primaryKey' => 'objectId'],
);

$this->expectException(HTTPRequestException::class);

Expand All @@ -84,7 +84,7 @@ public function testIndexStats()
$this->assertArrayHasKey('numberOfDocuments', $stats);
$this->assertEquals(0, $stats['numberOfDocuments']);
$this->assertArrayHasKey('isIndexing', $stats);
$this->assertArrayHasKey('fieldsFrequency', $stats);
$this->assertArrayHasKey('fieldsDistribution', $stats);
}

public function testWaitForPendingUpdateDefault()
Expand Down
70 changes: 70 additions & 0 deletions tests/Endpoints/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,76 @@ public function testParametersCanBeAStar()
$this->assertSame('Petit <em>Prince</em>', $response['hits'][0]['_formatted']['title']);
}

public function testBasicSearchWithFacetsDistribution()
{
$this->createFreshIndexAndSeedDocuments();
$response = $this->index->updateAttributesForFaceting(['genre']);
$this->index->waitForPendingUpdate($response['updateId']);

$response = $this->index->search('prince', [
'facetsDistribution' => ['genre'],
]);
$this->assertSame(2, $response['nbHits']);
$this->assertArrayHasKey('facetsDistribution', $response);
$this->assertArrayHasKey('exhaustiveFacetsCount', $response);
$this->assertArrayHasKey('genre', $response['facetsDistribution']);
$this->assertTrue($response['exhaustiveFacetsCount']);
$this->assertSame($response['facetsDistribution']['genre']['fantasy'], 1);
$this->assertSame($response['facetsDistribution']['genre']['adventure'], 1);
$this->assertSame($response['facetsDistribution']['genre']['romance'], 0);
}

public function testBasicSearchWithFacetFilters()
{
$this->createFreshIndexAndSeedDocuments();
$response = $this->index->updateAttributesForFaceting(['genre']);
$this->index->waitForPendingUpdate($response['updateId']);

$response = $this->index->search('prince', [
'facetFilters' => [['genre:fantasy']],
]);
$this->assertSame(1, $response['nbHits']);
$this->assertArrayNotHasKey('facetsDistribution', $response);
$this->assertArrayNotHasKey('exhaustiveFacetsCount', $response);
$this->assertSame(4, $response['hits'][0]['id']);
}

public function testBasicSearchWithMultipleFacetFilters()
{
$this->createFreshIndexAndSeedDocuments();
$response = $this->index->updateAttributesForFaceting(['genre']);
$this->index->waitForPendingUpdate($response['updateId']);

$response = $this->index->search('prince', [
'facetFilters' => ['genre:fantasy', ['genre:fantasy', 'genre:fantasy']],
]);
$this->assertSame(1, $response['nbHits']);
$this->assertArrayNotHasKey('facetsDistribution', $response);
$this->assertArrayNotHasKey('exhaustiveFacetsCount', $response);
$this->assertSame(4, $response['hits'][0]['id']);
}

public function testCustomSearchWithFacetFiltersAndAttributesToRetrieve()
{
$this->createFreshIndexAndSeedDocuments();
$response = $this->index->updateAttributesForFaceting(['genre']);
$this->index->waitForPendingUpdate($response['updateId']);

$response = $this->index->search('prince', [
'facetFilters' => [['genre:fantasy']],
'attributesToRetrieve' => ['id', 'title'],
]);
$this->assertSame(1, $response['nbHits']);
$this->assertArrayNotHasKey('facetsDistribution', $response);
$this->assertArrayNotHasKey('exhaustiveFacetsCount', $response);
$this->assertSame(4, $response['hits'][0]['id']);
$this->assertArrayHasKey('id', $response['hits'][0]);
$this->assertArrayHasKey('title', $response['hits'][0]);
$this->assertArrayNotHasKey('comment', $response['hits'][0]);
}

// PRIVATE

private function createFreshIndexAndSeedDocuments()
{
$this->client->deleteAllIndexes();
Expand Down
59 changes: 59 additions & 0 deletions tests/Settings/AttributesForFacetingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Tests\Settings;

use Tests\TestCase;

class AttributesForFacetingTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
$this->client->deleteAllIndexes();
}

public function testGetDefaultAttributesForFaceting()
{
$index = $this->client->createIndex('index');

$attributes = $index->getAttributesForFaceting();

$this->assertIsArray($attributes);
$this->assertEmpty($attributes);
}

public function testUpdateAttributesForFaceting()
{
$newAttributes = ['title'];
$index = $this->client->createIndex('index');

$promise = $index->updateAttributesForFaceting($newAttributes);

$this->assertIsValidPromise($promise);
$index->waitForPendingUpdate($promise['updateId']);

$attributesForFaceting = $index->getAttributesForFaceting();

$this->assertIsArray($attributesForFaceting);
$this->assertEquals($newAttributes, $attributesForFaceting);
}

public function testResetAttributesForFaceting()
{
$index = $this->client->createIndex('index');
$newAttributes = ['title'];

$promise = $index->updateAttributesForFaceting($newAttributes);
$index->waitForPendingUpdate($promise['updateId']);

$promise = $index->resetAttributesForFaceting();

$this->assertIsValidPromise($promise);

$index->waitForPendingUpdate($promise['updateId']);

$attributesForFaceting = $index->getAttributesForFaceting();
$this->assertIsArray($attributesForFaceting);
$this->assertEmpty($attributesForFaceting);
}
}
2 changes: 1 addition & 1 deletion tests/Settings/DisplayedAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ protected function setUp(): void
public function testGetDefaultDisplayedAttributes()
{
$indexA = $this->client->createIndex('indexA');
$indexB = $this->client->createIndex(['uid' => 'indexB', 'primaryKey' => 'objectID']);
$indexB = $this->client->createIndex('indexB', ['primaryKey' => 'objectID']);

$attributesA = $indexA->getDisplayedAttributes();
$attributesB = $indexB->getDisplayedAttributes();
Expand Down
2 changes: 1 addition & 1 deletion tests/Settings/SearchableAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function setUp(): void
public function testGetDefaultSearchableAttributes()
{
$indexA = $this->client->createIndex('indexA');
$indexB = $this->client->createIndex(['uid' => 'indexB', 'primaryKey' => 'objectID']);
$indexB = $this->client->createIndex('indexB', ['primaryKey' => 'objectID']);

$searchableAttributesA = $indexA->getSearchableAttributes();
$searchableAttributesB = $indexB->getSearchableAttributes();
Expand Down
8 changes: 4 additions & 4 deletions tests/Settings/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public function testGetDefaultSettings()
->createIndex('indexA')
->getSettings();
$settingB = $this->client
->createIndex([
'uid' => 'indexB',
'primaryKey' => $primaryKey,
])->getSettings();
->createIndex(
'indexB',
['primaryKey' => $primaryKey],
)->getSettings();

$this->assertEquals(self::DEFAULT_RANKING_RULES, $settingA['rankingRules']);
$this->assertNull($settingA['distinctAttribute']);
Expand Down
Loading

0 comments on commit 81b67c8

Please sign in to comment.