Skip to content

Commit ce22229

Browse files
authored
Merge pull request #11 from typesense/ts-0.18.0-support
Support for TS Server 0.18.0 features
2 parents 88dada1 + 31c9b02 commit ce22229

11 files changed

Lines changed: 468 additions & 19 deletions

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ Typesense use [HTTPlug](http://httplug.io/). List of clients & adapters [here](h
2222

2323
| Typesense Server | typesense-php |
2424
|------------------|----------------|
25-
| \>= v0.16.0 | \>= v4.2.0 |
25+
| \>= v0.18.0 | \>= v4.4.0 |
26+
| \>= v0.17.0 | \>= v4.2.0 |
2627
| \>= v0.16.0 | \>= v4.1.0 |
2728
| \>= v0.15.0 | \>= v4.0.0 |
2829

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ version: '3.5'
22

33
services:
44
typesense:
5-
image: typesense/typesense:0.17.0
5+
image: typesense/typesense:0.18.0
66
environment:
77
TYPESENSE_DATA_DIR: /data
88
TYPESENSE_API_KEY: xyz

examples/cluster_operations.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/** @noinspection ForgottenDebugOutputInspection */
4+
5+
include '../vendor/autoload.php';
6+
7+
use Symfony\Component\HttpClient\HttplugClient;
8+
use Typesense\Client;
9+
10+
try {
11+
$client = new Client(
12+
[
13+
'api_key' => 'xyz',
14+
'nodes' => [
15+
[
16+
'host' => 'localhost',
17+
'port' => '8108',
18+
'protocol' => 'http',
19+
],
20+
],
21+
'client' => new HttplugClient(),
22+
]
23+
);
24+
echo '<pre>';
25+
26+
print_r($client->operations->perform('snapshot', ['snapshot_path' => '/tmp/snapshot']));
27+
} catch (Exception $e) {
28+
echo $e->getMessage();
29+
}

examples/synonym_operations.php

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<?php
2+
3+
include '../vendor/autoload.php';
4+
5+
use Symfony\Component\HttpClient\HttplugClient;
6+
use Typesense\Client;
7+
8+
try {
9+
$client = new Client(
10+
[
11+
'api_key' => 'xyz',
12+
'nodes' => [
13+
[
14+
'host' => 'localhost',
15+
'port' => '8108',
16+
'protocol' => 'http',
17+
],
18+
],
19+
'client' => new HttplugClient(),
20+
]
21+
);
22+
echo '<pre>';
23+
try {
24+
print_r($client->collections['books']->delete());
25+
} catch (Exception $e) {
26+
// Don't error out if the collection was not found
27+
}
28+
echo "--------Create Collection-------\n";
29+
print_r(
30+
$client->collections->create(
31+
[
32+
'name' => 'books',
33+
'fields' => [
34+
[
35+
'name' => 'title',
36+
'type' => 'string',
37+
],
38+
[
39+
'name' => 'authors',
40+
'type' => 'string[]',
41+
'facet' => true
42+
],
43+
[
44+
'name' => 'publication_year',
45+
'type' => 'int32',
46+
'facet' => true,
47+
],
48+
[
49+
'name' => 'ratings_count',
50+
'type' => 'int32',
51+
],
52+
[
53+
'name' => 'average_rating',
54+
'type' => 'float',
55+
],
56+
[
57+
'name' => 'image_url',
58+
'type' => 'string',
59+
],
60+
],
61+
'default_sorting_field' => 'ratings_count',
62+
]
63+
)
64+
);
65+
echo "--------Create Collection-------\n";
66+
echo "\n";
67+
echo "--------Upsert Synonym-------\n";
68+
print_r(
69+
$client->collections['books']->synonyms->upsert(
70+
'synonym-set-1',
71+
[
72+
'synonyms' => ['Hunger', 'Katniss'],
73+
]
74+
)
75+
);
76+
echo "--------Upsert Synonym-------\n";
77+
echo "\n";
78+
echo "--------Get All Synonyms-------\n";
79+
print_r($client->collections['books']->synonyms->retrieve());
80+
echo "--------Get All Synonyms-------\n";
81+
echo "\n";
82+
echo "--------Get Single Synonym-------\n";
83+
print_r(
84+
$client->collections['books']->synonyms['synonym-set-1']->retrieve()
85+
);
86+
echo "--------Get Single Synonym-------\n";
87+
echo "\n";
88+
echo "--------Create Document-------\n";
89+
print_r(
90+
$client->collections['books']->documents->create(
91+
[
92+
'id' => '1',
93+
'original_publication_year' => 2008,
94+
'authors' => [
95+
'Suzanne Collins',
96+
],
97+
'average_rating' => 4.34,
98+
'publication_year' => 2008,
99+
'title' => 'The Hunger Games',
100+
'image_url' => 'https://images.gr-assets.com/books/1447303603m/2767052.jpg',
101+
'ratings_count' => 4780653,
102+
]
103+
)
104+
);
105+
echo "--------Create Document-------\n";
106+
echo "\n";
107+
echo "--------Search Document, using a synonym-------\n";
108+
print_r(
109+
$client->collections['books']->documents->search(
110+
[
111+
'q' => 'Katniss',
112+
'query_by' => 'title'
113+
]
114+
)
115+
);
116+
echo "--------Search Document, using a synonym-------\n";
117+
echo "\n";
118+
echo "--------Upsert 1-way synonym-------\n";
119+
print_r(
120+
$client->collections['books']->synonyms->upsert(
121+
'synonym-set-1',
122+
[
123+
'root' => 'Katniss',
124+
'synonyms' => ['Hunger', 'Peeta'],
125+
]
126+
)
127+
);
128+
echo "--------Upsert 1-way synonym-------\n";
129+
echo "\n";
130+
echo "--------Search Document, using a synonym-------\n";
131+
// Won't return any results
132+
print_r(
133+
$client->collections['books']->documents->search(
134+
[
135+
'q' => 'Peeta',
136+
'query_by' => 'title'
137+
]
138+
)
139+
);
140+
echo "--------Search Document, using a synonym-------\n";
141+
echo "\n";
142+
echo "--------Delete Synonym-------\n";
143+
print_r(
144+
$client->collections['books']->getSynonyms()['synonym-set-1']->delete()
145+
);
146+
echo "--------Delete Synonym-------\n";
147+
echo "\n";
148+
} catch (Exception $e) {
149+
echo $e->getMessage();
150+
}

src/ApiCall.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ class ApiCall
6868
*/
6969
public function __construct(Configuration $config)
7070
{
71-
$this->config = $config;
72-
$this->logger = $config->getLogger();
73-
$this->client = $config->getClient();
71+
$this->config = $config;
72+
$this->logger = $config->getLogger();
73+
$this->client = $config->getClient();
7474
static::$nodes = $this->config->getNodes();
7575
static::$nearestNode = $this->config->getNearestNode();
76-
$this->nodeIndex = 0;
76+
$this->nodeIndex = 0;
7777
$this->initializeNodes();
7878
}
7979

src/Client.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ class Client
5050
*/
5151
public Health $health;
5252

53+
/**
54+
* @var Operations
55+
*/
56+
public Operations $operations;
57+
5358
/**
5459
* @var ApiCall
5560
*/
@@ -73,6 +78,7 @@ public function __construct(array $config)
7378
$this->debug = new Debug($this->apiCall);
7479
$this->metrics = new Metrics($this->apiCall);
7580
$this->health = new Health($this->apiCall);
81+
$this->operations = new Operations($this->apiCall);
7682
}
7783

7884
/**
@@ -122,4 +128,12 @@ public function getHealth(): Health
122128
{
123129
return $this->health;
124130
}
131+
132+
/**
133+
* @return Operations
134+
*/
135+
public function getOperations(): Operations
136+
{
137+
return $this->operations;
138+
}
125139
}

src/Collection.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ class Collection
3535
*/
3636
public Overrides $overrides;
3737

38+
/**
39+
* @var Synonyms
40+
*/
41+
public Synonyms $synonyms;
42+
3843
/**
3944
* Collection constructor.
4045
*
@@ -47,6 +52,7 @@ public function __construct(string $name, ApiCall $apiCall)
4752
$this->apiCall = $apiCall;
4853
$this->documents = new Documents($name, $this->apiCall);
4954
$this->overrides = new Overrides($name, $this->apiCall);
55+
$this->synonyms = new Synonyms($name, $this->apiCall);
5056
}
5157

5258
/**
@@ -73,6 +79,14 @@ public function getOverrides(): Overrides
7379
return $this->overrides;
7480
}
7581

82+
/**
83+
* @return Synonyms
84+
*/
85+
public function getSynonyms(): Synonyms
86+
{
87+
return $this->synonyms;
88+
}
89+
7690
/**
7791
* @return array
7892
* @throws TypesenseClientError|HttpClientException

src/Operations.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Typesense;
4+
5+
use Http\Client\Exception as HttpClientException;
6+
use Typesense\Exceptions\TypesenseClientError;
7+
8+
/**
9+
* Class Operations
10+
*
11+
* @package \Typesense
12+
*/
13+
class Operations
14+
{
15+
public const RESOURCE_PATH = '/operations';
16+
17+
/**
18+
* @var ApiCall
19+
*/
20+
private ApiCall $apiCall;
21+
22+
/**
23+
* Alias constructor.
24+
*
25+
* @param ApiCall $apiCall
26+
*/
27+
public function __construct(ApiCall $apiCall)
28+
{
29+
$this->apiCall = $apiCall;
30+
}
31+
32+
/**
33+
* @param string $operationName
34+
* @param array $queryParameters
35+
*
36+
* @return array
37+
* @throws TypesenseClientError|HttpClientException
38+
*/
39+
public function perform(string $operationName, array $queryParameters = []): array
40+
{
41+
return $this->apiCall->post(
42+
sprintf('%s/%s', static::RESOURCE_PATH, $operationName),
43+
null,
44+
true,
45+
$queryParameters
46+
);
47+
}
48+
}

src/Overrides.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ public function endPointPath(string $overrideId = ''): string
6161
}
6262

6363
/**
64-
* @param string $documentId
64+
* @param string $overrideId
6565
* @param array $config
6666
*
6767
* @return array
6868
* @throws TypesenseClientError|HttpClientException
6969
*/
70-
public function upsert(string $documentId, array $config): array
70+
public function upsert(string $overrideId, array $config): array
7171
{
72-
return $this->apiCall->put($this->endPointPath($documentId), $config);
72+
return $this->apiCall->put($this->endPointPath($overrideId), $config);
7373
}
7474

7575
/**
@@ -84,36 +84,36 @@ public function retrieve(): array
8484
/**
8585
* @inheritDoc
8686
*/
87-
public function offsetExists($documentId): bool
87+
public function offsetExists($overrideId): bool
8888
{
89-
return isset($this->overrides[$documentId]);
89+
return isset($this->overrides[$overrideId]);
9090
}
9191

9292
/**
9393
* @inheritDoc
9494
*/
95-
public function offsetGet($documentId)
95+
public function offsetGet($overrideId)
9696
{
97-
if (!isset($this->overrides[$documentId])) {
98-
$this->overrides[$documentId] = new Override($this->collectionName, $documentId, $this->apiCall);
97+
if (!isset($this->overrides[$overrideId])) {
98+
$this->overrides[$overrideId] = new Override($this->collectionName, $overrideId, $this->apiCall);
9999
}
100100

101-
return $this->overrides[$documentId];
101+
return $this->overrides[$overrideId];
102102
}
103103

104104
/**
105105
* @inheritDoc
106106
*/
107-
public function offsetSet($documentId, $value): void
107+
public function offsetSet($overrideId, $value): void
108108
{
109-
$this->overrides[$documentId] = $value;
109+
$this->overrides[$overrideId] = $value;
110110
}
111111

112112
/**
113113
* @inheritDoc
114114
*/
115-
public function offsetUnset($documentId): void
115+
public function offsetUnset($overrideId): void
116116
{
117-
unset($this->overrides[$documentId]);
117+
unset($this->overrides[$overrideId]);
118118
}
119119
}

0 commit comments

Comments
 (0)