Skip to content

Commit

Permalink
Implemented the 'partial_replace' feature (#208)
Browse files Browse the repository at this point in the history
* Implemented the 'partial_replace' feature

* Updated path setting for PartialReplace

* Updated path setting for PartialReplace

* Fixed passing params for partialReplace

* Updated the test on partialReplace

---------

Co-authored-by: nick <[email protected]>
  • Loading branch information
Nick-S-2018 and nick authored Apr 17, 2024
1 parent 3f2e3c6 commit a9eee18
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 3 deletions.
10 changes: 10 additions & 0 deletions docs/indexclass.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ Replace an existing document in the index.
Expects:
- an array of values
- a document ID
- to execute a partial replace ( `false|true`, `false` by default )

Example:

Expand All @@ -156,6 +157,15 @@ $index->replaceDocument([
], 1);
```

```php
$index->replaceDocument([
'title' => 'find me',
'label' => 'not used'
], 2, true);
```



### replaceDocuments()

Replace multiple documents in the index.
Expand Down
15 changes: 15 additions & 0 deletions src/Manticoresearch/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,21 @@ public function replace(array $params = []) {
return $response->getResponse();
}

/**
* Endpoint: _update
* @param string $index
* @param int $id
* @param array $params
* @return mixed
*/
public function partialReplace(string $index, int $id, array $params = []) {
$endpoint = new Endpoints\PartialReplace($params);
$endpoint->setPathByIndexAndId($index, $id);
$response = $this->request($endpoint);

return $response->getResponse();
}

/**
* Endpoint: update
* @param array $params
Expand Down
34 changes: 34 additions & 0 deletions src/Manticoresearch/Endpoints/PartialReplace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php


namespace Manticoresearch\Endpoints;

use Manticoresearch\Request;

/**
* Class PartialReplace
* @package Manticoresearch\Endpoints
*/
class PartialReplace extends Request
{
/**
* @var string
*/
protected $index;

/**
* @param string $index
* @param int $id
*/
public function setPathByIndexAndId($index, $id) {
$path = '/' . $index . '/_update/' . $id;
parent::setPath($path);
}

/**
* @return mixed|string
*/
public function getMethod() {
return 'POST';
}
}
12 changes: 9 additions & 3 deletions src/Manticoresearch/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public function updateDocuments($data, $query) {
return $this->client->update($params);
}

public function replaceDocument($data, $id) {
public function replaceDocument($data, $id, $isPartialReplace = false) {
static::checkDocumentId($id);
static::checkDocument($data);
if (is_object($data)) {
Expand All @@ -210,11 +210,17 @@ public function replaceDocument($data, $id) {
}
$params = [
'body' => [
'index' => $this->index,
'id' => $id,
'doc' => $data,
],
];
if ($isPartialReplace) {
return $this->client->partialReplace($this->index, $id, $params);
}
$params['body'] += [
'index' => $this->index,
'id' => $id,
'doc' => $data,
];
if ($this->cluster !== null) {
$params['body']['cluster'] = $this->cluster;
}
Expand Down
17 changes: 17 additions & 0 deletions test/Manticoresearch/Endpoints/PartialReplaceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
namespace Manticoresearch\Test\Endpoints;

class PartialReplaceTest extends \PHPUnit\Framework\TestCase
{

public function testGetPath() {
$replace = new \Manticoresearch\Endpoints\PartialReplace();
$replace->setPathByIndexAndId('test', 1);
$this->assertEquals('/test/_update/1', $replace->getPath());
}

public function testGetMethod() {
$replace = new \Manticoresearch\Endpoints\Replace();
$this->assertEquals('POST', $replace->getMethod());
}
}
18 changes: 18 additions & 0 deletions test/Manticoresearch/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,24 @@ public function testReplaceDocument() {
);
}

public function testPartialReplaceDocument() {
$index = $this->getIndex();
$this->addDocument($index);
$response = $index->replaceDocument(
[
'title' => 'This is an example document for cooking',
'label' => 'not used',
], 1, true
);

$this->assertEquals(
[
'_index' => 'testindex',
'updated' => 1,
], $response
);
}

public function testReplaceDocuments() {
$index = $this->getIndex();
$this->addDocument($index);
Expand Down

0 comments on commit a9eee18

Please sign in to comment.