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

News methods New format handling CSV NDJSON #235

Merged
merged 4 commits into from
Oct 11, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Contracts/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface Http
{
public function get($path, array $query = []);

public function post(string $path, $body = null, array $query = []);
public function post(string $path, $body = null, array $query = [], string $content_type = null);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why $content_type and not $contentType if in codebase camelCase is used everywhere? :)

Copy link
Member

Choose a reason for hiding this comment

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

This branch was merged into bump-meilisearch-v0.23.0 and not main. I requested this change in the PR corresponding to the merge of bump-meilisearch-v0.23.0 to main.
#214 (comment)

So good remark, we changed it! :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh, missed that 👍


public function put(string $path, $body = null, array $query = []);

Expand Down
15 changes: 15 additions & 0 deletions src/Endpoints/Delegates/HandlesDocuments.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ public function addDocumentsInBatches(array $documents, ?int $batchSize = 1000,
return $promises;
}

public function addDocumentsJson(string $documents, ?string $primaryKey = null)
{
return $this->http->post(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey], 'application/json');
}

public function addDocumentsNdjson(string $documents, ?string $primaryKey = null)
{
return $this->http->post(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey], 'application/x-ndjson');
}

public function addDocumentsCsv(string $documents, ?string $primaryKey = null)
{
return $this->http->post(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey], 'text/csv');
}

public function updateDocuments(array $documents, ?string $primaryKey = null)
{
return $this->http->put(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey]);
Expand Down
13 changes: 10 additions & 3 deletions src/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public function __construct(string $url, string $apiKey = null, ClientInterface
$this->requestFactory = Psr17FactoryDiscovery::findRequestFactory();
$this->streamFactory = Psr17FactoryDiscovery::findStreamFactory();
$this->headers = array_filter([
'Content-type' => 'application/json',
'X-Meili-API-Key' => $this->apiKey,
]);
}
Expand Down Expand Up @@ -98,18 +97,25 @@ public function get($path, $query = [])
* @throws CommunicationException
* @throws FailedJsonEncodingException
*/
public function post(string $path, $body = null, array $query = [])
public function post(string $path, $body = null, array $query = [], string $content_type = null)
{
if ($content_type) {
$this->headers['Content-type'] = $content_type;
} else {
$this->headers['Content-type'] = 'application/json';
$body = $this->jsonEncode($body);
}
$request = $this->requestFactory->createRequest(
'POST',
$this->baseUrl.$path.$this->buildQueryString($query)
)->withBody($this->streamFactory->createStream($this->jsonEncode($body)));
)->withBody($this->streamFactory->createStream($body));

return $this->execute($request);
}

public function put($path, $body = null, $query = [])
{
$this->headers['Content-type'] = 'application/json';
$request = $this->requestFactory->createRequest(
'PUT',
$this->baseUrl.$path.$this->buildQueryString($query)
Expand All @@ -130,6 +136,7 @@ public function put($path, $body = null, $query = [])
*/
public function patch($path, $body = null, $query = [])
{
$this->headers['Content-type'] = 'application/json';
$request = $this->requestFactory->createRequest(
'PATCH',
$this->baseUrl.$path.$this->buildQueryString($query)
Expand Down
63 changes: 63 additions & 0 deletions tests/Endpoints/DocumentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,69 @@ public function testAddDocumentWithSpecialChars(): void
}
}

public function testAddDocumentsCsv(): void
{
$index = $this->client->index('documentCsv');

$fileCsv = fopen('./tests/datasets/songs.csv', 'r');
$documentCsv = fread($fileCsv, filesize('./tests/datasets/songs.csv'));
fclose($fileCsv);

$promise = $index->addDocumentsCsv($documentCsv);

$this->assertIsValidPromise($promise);

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

$this->assertEquals($update['status'], 'processed');
$this->assertNotEquals($update['type']['number'], 0);

$response = $index->getDocuments();
$this->assertCount(20, $response);
}

public function testAddDocumentsJson(): void
{
$index = $this->client->index('documentJson');

$fileJson = fopen('./tests/datasets/small_movies.json', 'r');
$documentJson = fread($fileJson, filesize('./tests/datasets/small_movies.json'));
fclose($fileJson);

$promise = $index->addDocumentsJson($documentJson);

$this->assertIsValidPromise($promise);

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

$this->assertEquals($update['status'], 'processed');
$this->assertNotEquals($update['type']['number'], 0);

$response = $index->getDocuments();
$this->assertCount(20, $response);
}

public function testAddDocumentsNdJson(): void
{
$index = $this->client->index('documentNdJson');

$fileNdJson = fopen('./tests/datasets/songs.ndjson', 'r');
$documentNdJson = fread($fileNdJson, filesize('./tests/datasets/songs.ndjson'));
fclose($fileNdJson);

$promise = $index->addDocumentsNdJson($documentNdJson);

$this->assertIsValidPromise($promise);

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

$this->assertEquals($update['status'], 'processed');
$this->assertNotEquals($update['type']['number'], 0);

$response = $index->getDocuments();
$this->assertCount(20, $response);
}

public function testCannotAddDocumentWhenJsonEncodingFails(): void
{
$this->expectException(FailedJsonEncodingException::class);
Expand Down
Loading