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

Implements the update documents by function #664

Merged
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
15 changes: 15 additions & 0 deletions src/Endpoints/Delegates/HandlesDocuments.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,21 @@ public function updateDocumentsNdjsonInBatches(string $documents, ?int $batchSiz
return $promises;
}

/**
* This is an EXPERIMENTAL feature, which may break without a major version.
* It's available after Meilisearch v1.10.
*
* More info about the feature: https://github.com/orgs/meilisearch/discussions/762
* More info about experimental features in general: https://www.meilisearch.com/docs/reference/api/experimental-features
*
* @param non-empty-string $function
* @param array{filter?: non-empty-string|list<non-empty-string>|null, context?: array<non-empty-string, mixed>} $options
*/
public function updateDocumentsByFunction(string $function, array $options = [])
{
return $this->http->post(self::PATH.'/'.$this->uid.'/documents/edit', array_merge(['function' => $function], $options));
}

public function deleteAllDocuments(): array
{
return $this->http->delete(self::PATH.'/'.$this->uid.'/documents');
Expand Down
53 changes: 53 additions & 0 deletions tests/Endpoints/DocumentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,59 @@ public function testUpdateDocumentsInBatches(): void
self::assertCount(\count(self::DOCUMENTS), $response);
}

public function testUpdateDocumentsByFunction(): void
{
$http = new Client($this->host, getenv('MEILISEARCH_API_KEY'));
$http->patch('/experimental-features', ['editDocumentsByFunction' => true]);
$index = $this->createEmptyIndex($this->safeIndexName('movies'));
$documentPromise = $index->addDocuments(self::DOCUMENTS);
$index->waitForTask($documentPromise['taskUid']);

$function = '
if doc.id % context.modulo == 0 {
doc.title = `kefir would read ${doc.title}`;
};
doc.remove("comment");
doc.remove("genre");
';
$documentPromise = $index->updateDocumentsByFunction($function, ['context' => ['modulo' => 3]]);
$index->waitForTask($documentPromise['taskUid']);

$documents = $index->getDocuments()->getResults();

$replacements = [
[
'id' => 123,
'title' => 'kefir would read Pride and Prejudice',
],
[
'id' => 456,
'title' => 'kefir would read Le Petit Prince',
],
[
'id' => 2,
'title' => 'Le Rouge et le Noir',
],
[
'id' => 1,
'title' => 'Alice In Wonderland',
],
[
'id' => 1344,
'title' => 'kefir would read The Hobbit',
],
[
'id' => 4,
'title' => 'Harry Potter and the Half-Blood Prince',
],
[
'id' => 42,
'title' => 'kefir would read The Hitchhiker\'s Guide to the Galaxy',
],
];
self::assertSame($replacements, $documents);
}

public function testAddDocumentsCsvInBatches(): void
{
$index = $this->client->index('documentCsv');
Expand Down
Loading