Skip to content

Commit

Permalink
Merge branch 'master' into fix-limit-test
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick-S-2018 authored Jan 12, 2024
2 parents 3e02956 + c8c82b0 commit 76bd073
Show file tree
Hide file tree
Showing 27 changed files with 311 additions and 144 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,21 @@ jobs:
strategy:
matrix:
os: ['ubuntu-latest']
php-version: ['7.1', '7.3', '7.4', '8.0', '8.1']
php-version: ['7.1', '7.3', '7.4', '8.0', '8.1', '8.2']
phpunit-version: ['latest']
transport: ['', 'PhpHttp']

services:
manticoresearch-manticore:
image: manticoresearch/manticore:dev
env:
EXTRA: 1
ports:
- 9308:9308
manticoresearch-manticore-2:
image: manticoresearch/manticore:dev
env:
EXTRA: 1
ports:
- 5308:9308

Expand Down
36 changes: 34 additions & 2 deletions docs/indexclass.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,14 @@ It returns an array with:

- `_index` as the index name
- `_id` as the updated ID
- result indicating whether the update was successful (`updated`) or not (`noop`)
- result as `updated` if the update was successful or `noop` otherwise

Example of a return value:

```json
{"_index":"test","_id":4,"result":"updated"}
```

### updateDocuments()

It can update multiple documents that match a condition.
Expand Down Expand Up @@ -257,6 +260,8 @@ It returns an array with:
- `_index` as the index_name
- updated as the number of documents updated

Example of a return value:

```json
{"_index":"test","updated":2}
```
Expand All @@ -276,12 +281,37 @@ It returns an array with:
- _index as index name
- _id as the document id
- found - true if document existed
- result indicating whether the update was successful ('deleted') or not ('not found')
- result as `deleted` if the update was successful or `not found` otherwise

Example of a return value:

```json
{"_index":"test","_id":5,"found":true,"result":"deleted"}
```

### deleteDocumentsByIds()

Deletes multiple documents by ID. Expects an array of IDs.

Example:

```php
$index->deleteDocumentsByIds([100,101]);
```

It returns an array with:

- `_index` as index name
- `_id` as the first document id passed
- `found` as true if at least one document existed
- `result` as `deleted` if at least one document was deleted, or `not found` if no document was found

Example of a return value:

```json
{"_index":"test","_id":100,"found":true,"result":"deleted"}
```

### deleteDocuments()

Deletes documents using a query expression which can be passed either as an array or as a [Query](query.md) object.
Expand All @@ -303,6 +333,8 @@ It returns an array with:
- `_index` as index name
- `deleted` as the number of found and deleted documents

Example of a return value:

```json
{"_index":"test","deleted":0}
```
Expand Down
2 changes: 1 addition & 1 deletion src/Manticoresearch/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function setConfig(array $config): self
*/
public static function create($config): Client
{
return self::createFromArray($config);
return static::createFromArray($config);
}

/**
Expand Down
17 changes: 15 additions & 2 deletions src/Manticoresearch/Connection/ConnectionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class ConnectionPool

public $retries;

public $retries_attempts =0;
public $retries_attempts = 0;

public $retries_info = [];

public function __construct(array $connections, SelectorInterface $strategy, int $retries)
{
Expand All @@ -52,14 +54,25 @@ public function setConnections(array $connections)
public function getConnection(): Connection
{
$this->retries_attempts++;
$connection = $this->strategy->getConnection($this->connections);
$connection = $this->strategy->getConnection($this->connections);
if ($this->retries_attempts <= $this->retries) {
$this->retries_info[] = [
'host' => $connection->getHost(),
'port' => $connection->getPort(),
];
}
if ($connection->isAlive()) {
return $connection;
}
if ($this->retries_attempts < $this->retries) {
return $connection;
}
$exMsg = 'After %d retr%s to %d node%s, connection has failed. No more retries left.';
$exMsg .= "\nRetries made:\n";
foreach ($this->retries_info as $i => $info) {
$i++;
$exMsg .= " $i. to {$info['host']}:{$info['port']}\n";
}
$connCount = count($this->connections);
throw new NoMoreNodesException(
sprintf($exMsg, $this->retries, $this->retries > 1 ? 'ies' : 'y', $connCount, $connCount > 1 ? 's' : '')
Expand Down
2 changes: 1 addition & 1 deletion src/Manticoresearch/Endpoints/Keywords.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function setBody($params = null)
{
if (isset($this->index)) {
$binds =[];
$binds[] = "'" . self::escape($params['query']) . "'";
$binds[] = "'" . static::escape($params['query']) . "'";
$binds[] = "'" . $this->index . "'";
if (count($params['options']) > 0) {
foreach ($params['options'] as $name => $value) {
Expand Down
2 changes: 1 addition & 1 deletion src/Manticoresearch/Endpoints/Suggest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function setBody($params = null)
{
if (isset($this->index)) {
$binds =[];
$binds[] = "'" . self::escape($params['query']) . "'";
$binds[] = "'" . static::escape($params['query']) . "'";
$binds[] = "'" . $this->index . "'";
if (count($params['options']) > 0) {
foreach ($params['options'] as $name => $value) {
Expand Down
48 changes: 40 additions & 8 deletions src/Manticoresearch/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
class Index
{
use Utils;

protected $client;
protected $index;
protected $cluster;
Expand All @@ -38,7 +40,7 @@ public function search($input): Search

public function getDocumentById($id)
{
self::checkDocumentId($id);
static::checkDocumentId($id);
$params = [
'body' => [
'index' => $this->index,
Expand All @@ -56,10 +58,14 @@ public function getDocumentByIds($ids)
if (!is_array($ids)) {
$ids = [$ids];
}
array_walk($ids, 'self::checkDocumentId');
// Deduplicate and order the list
static::checkIfList($ids);

array_walk($ids, [static::class, 'checkDocumentId']);
$params = [
'body' => [
'index' => $this->index,
'limit' => sizeof($ids),
'query' => [
'in' => ['id' => $ids]
]
Expand All @@ -70,7 +76,7 @@ public function getDocumentByIds($ids)

public function addDocument($data, $id = 0)
{
self::checkDocumentId($id);
static::checkDocumentId($id);
if (is_object($data)) {
$data = (array) $data;
} elseif (is_string($data)) {
Expand Down Expand Up @@ -101,7 +107,7 @@ public function addDocuments($documents)
}
if (isset($document['id'])) {
$id = $document['id'];
self::checkDocumentId($id);
static::checkDocumentId($id);
unset($document['id']);
} else {
$id = 0;
Expand All @@ -121,7 +127,7 @@ public function addDocuments($documents)

public function deleteDocument($id)
{
self::checkDocumentId($id);
static::checkDocumentId($id);
$params = [
'body' => [
'index' => $this->index,
Expand All @@ -134,6 +140,25 @@ public function deleteDocument($id)
return $this->client->delete($params);
}

public function deleteDocumentsByIds(array $ids)
{
// Deduplicate and order the list
static::checkIfList($ids);

array_walk($ids, 'self::checkDocumentId');
$params = [
'body' => [
'index' => $this->index,
'limit' => sizeof($ids),
'id' => $ids
]
];
if ($this->cluster !== null) {
$params['body']['cluster'] = $this->cluster;
}
return $this->client->delete($params);
}

public function deleteDocuments($query)
{
if ($query instanceof Query) {
Expand All @@ -153,7 +178,7 @@ public function deleteDocuments($query)

public function updateDocument($data, $id)
{
self::checkDocumentId($id);
static::checkDocumentId($id);
$params = [
'body' => [
'index' => $this->index,
Expand Down Expand Up @@ -187,7 +212,7 @@ public function updateDocuments($data, $query)

public function replaceDocument($data, $id)
{
self::checkDocumentId($id);
static::checkDocumentId($id);
if (is_object($data)) {
$data = (array) $data;
} elseif (is_string($data)) {
Expand Down Expand Up @@ -216,7 +241,7 @@ public function replaceDocuments($documents)
$document = json_decode($document, true);
}
$id = $document['id'];
self::checkDocumentId($id);
static::checkDocumentId($id);
unset($document['id']);
$replace = [
'index' => $this->index,
Expand Down Expand Up @@ -428,4 +453,11 @@ protected static function checkDocumentId(&$id)
}
$id = (int)$id;
}

protected static function checkIfList(array &$ids)
{
if ($ids && (array_keys($ids) !== range(0, count($ids) - 1))) {
$ids = array_values(array_unique($ids));
}
}
}
15 changes: 15 additions & 0 deletions src/Manticoresearch/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ private function stripBadUtf8()
public function hasError()
{
$response = $this->getResponse();
if (is_array($response)) {
foreach ($response as $r) {
if (isset($r['error']) && $r['error'] !== '') {
return true;
}
}
}
return (isset($response['error']) && $response['error'] !== '') ||
(isset($response['errors']) && $response['errors'] !== false);
}
Expand All @@ -116,6 +123,14 @@ public function getError()
return json_encode($response['error'], true);
} elseif (isset($response['errors'])) {
return json_encode($response['errors'], true);
} elseif (is_array($response)) {
$errors = "";
foreach ($response as $r) {
if (isset($r['error']) && $r['error'] !== '') {
$errors .= json_encode($r['error'], true);
}
}
return $errors;
} else {
return '';
}
Expand Down
10 changes: 5 additions & 5 deletions src/Manticoresearch/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,11 @@ public function filter($attr, $op = null, $values = null, $boolean = self::FILTE
}
}

if ($boolean === self::FILTER_AND) {
if ($boolean === static::FILTER_AND) {
$this->query->must($attr);
} elseif ($boolean === self::FILTER_OR) {
} elseif ($boolean === static::FILTER_OR) {
$this->query->should($attr);
} elseif ($boolean === self::FILTER_NOT) {
} elseif ($boolean === static::FILTER_NOT) {
$this->query->mustNot($attr);
}

Expand All @@ -230,12 +230,12 @@ public function filter($attr, $op = null, $values = null, $boolean = self::FILTE

public function orFilter($attr, $op = null, $values = null): self
{
return $this->filter($attr, $op, $values, self::FILTER_OR);
return $this->filter($attr, $op, $values, static::FILTER_OR);
}

public function notFilter($attr, $op = null, $values = null): self
{
return $this->filter($attr, $op, $values, self::FILTER_NOT);
return $this->filter($attr, $op, $values, static::FILTER_NOT);
}

public function offset($offset): self
Expand Down
8 changes: 4 additions & 4 deletions src/Manticoresearch/Transport/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function execute(Request $request, $params = [])
if ($errorno>0) {
$error = curl_error($conn);

self::$curl = null;
static::$curl = null;
throw new ConnectionException($error, $request);
}

Expand Down Expand Up @@ -131,9 +131,9 @@ public function execute(Request $request, $params = [])

protected function getCurlConnection(bool $persistent = true)
{
if (!$persistent || !self::$curl) {
self::$curl = curl_init();
if (!$persistent || !static::$curl) {
static::$curl = curl_init();
}
return self::$curl;
return static::$curl;
}
}
23 changes: 23 additions & 0 deletions test/Manticoresearch/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,29 @@ public function testConnectionError()
$client->search(['body'=>'']);
}

public function testConnectionNoMoreRetriesError()
{
$params = [
'connections' => [
[
'host' => '127.0.0.1',
'port' => 9418,
],
[
'host' => '127.0.0.2',
'port' => 9428,
],
],
'retries' => 2,
];
$exMsg = "After 2 retries to 2 nodes, connection has failed. No more retries left.\n"
. "Retries made:\n 1. to 127.0.0.1:9418\n 2. to 127.0.0.2:9428\n";
$client = new Client($params);
$this->expectException(ConnectionException::class);
$this->expectExceptionMessage($exMsg);
$client->search(['body'=>'']);
}

public function testDouble()
{
$params = ['connections'=>
Expand Down
Loading

0 comments on commit 76bd073

Please sign in to comment.