Skip to content

Commit

Permalink
Implements the update documents by function (#664)
Browse files Browse the repository at this point in the history
* Edit documents by function

* Apply suggestions from code review

Co-authored-by: Tomas Norkūnas <norkunas.tom@gmail.com>

* make linter happy

* make phpstan happy

* Update src/Endpoints/Delegates/HandlesDocuments.php

Co-authored-by: Tomas Norkūnas <norkunas.tom@gmail.com>

* Update src/Endpoints/Delegates/HandlesDocuments.php

Co-authored-by: Tomas Norkūnas <norkunas.tom@gmail.com>

* do not use the splat operator on arraymap

* make linter happy

---------

Co-authored-by: Tomas Norkūnas <norkunas.tom@gmail.com>
Co-authored-by: Clémentine <clementine@meilisearch.com>
  • Loading branch information
3 people authored Aug 1, 2024
1 parent e76988c commit 3c237bb
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
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

0 comments on commit 3c237bb

Please sign in to comment.