Skip to content

Commit e4abbc6

Browse files
committed
Added support for synonyms
1 parent 63aa7a4 commit e4abbc6

5 files changed

Lines changed: 362 additions & 1 deletion

File tree

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/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/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/Synonym.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace Typesense;
4+
5+
use Http\Client\Exception as HttpClientException;
6+
use Typesense\Exceptions\TypesenseClientError;
7+
8+
/**
9+
* Class synonym
10+
*
11+
* @package \Typesense
12+
* @date 4/5/20
13+
* @author Typesense <contact@typesense.org>
14+
*/
15+
class Synonym
16+
{
17+
18+
/**
19+
* @var string
20+
*/
21+
private string $collectionName;
22+
23+
/**
24+
* @var string
25+
*/
26+
private string $synonymId;
27+
28+
/**
29+
* @var ApiCall
30+
*/
31+
private ApiCall $apiCall;
32+
33+
/**
34+
* synonym constructor.
35+
*
36+
* @param string $collectionName
37+
* @param string $synonymId
38+
* @param ApiCall $apiCall
39+
*/
40+
public function __construct(string $collectionName, string $synonymId, ApiCall $apiCall)
41+
{
42+
$this->collectionName = $collectionName;
43+
$this->synonymId = $synonymId;
44+
$this->apiCall = $apiCall;
45+
}
46+
47+
/**
48+
* @return string
49+
*/
50+
private function endPointPath(): string
51+
{
52+
return sprintf(
53+
'%s/%s/%s/%s',
54+
Collections::RESOURCE_PATH,
55+
$this->collectionName,
56+
synonyms::RESOURCE_PATH,
57+
$this->synonymId
58+
);
59+
}
60+
61+
/**
62+
* @return array
63+
* @throws TypesenseClientError|HttpClientException
64+
*/
65+
public function retrieve(): array
66+
{
67+
return $this->apiCall->get($this->endPointPath(), []);
68+
}
69+
70+
/**
71+
* @return array
72+
* @throws TypesenseClientError|HttpClientException
73+
*/
74+
public function delete(): array
75+
{
76+
return $this->apiCall->delete($this->endPointPath());
77+
}
78+
}

src/Synonyms.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
namespace Typesense;
4+
5+
use Http\Client\Exception as HttpClientException;
6+
use Typesense\Exceptions\TypesenseClientError;
7+
8+
/**
9+
* Class Synonyms
10+
*
11+
* @package \Typesense
12+
* @date 4/5/20
13+
* @author Typesense <contact@typesense.org>
14+
*/
15+
class Synonyms implements \ArrayAccess
16+
{
17+
18+
public const RESOURCE_PATH = 'synonyms';
19+
20+
/**
21+
* @var ApiCall
22+
*/
23+
private ApiCall $apiCall;
24+
25+
/**
26+
* @var string
27+
*/
28+
private string $collectionName;
29+
30+
/**
31+
* @var array
32+
*/
33+
private array $synonyms = [];
34+
35+
/**
36+
* Synonyms constructor.
37+
*
38+
* @param string $collectionName
39+
* @param ApiCall $apiCall
40+
*/
41+
public function __construct(string $collectionName, ApiCall $apiCall)
42+
{
43+
$this->collectionName = $collectionName;
44+
$this->apiCall = $apiCall;
45+
}
46+
47+
/**
48+
* @param string $synonymId
49+
*
50+
* @return string
51+
*/
52+
public function endPointPath(string $synonymId = ''): string
53+
{
54+
return sprintf(
55+
'%s/%s/%s/%s',
56+
Collections::RESOURCE_PATH,
57+
$this->collectionName,
58+
static::RESOURCE_PATH,
59+
$synonymId
60+
);
61+
}
62+
63+
/**
64+
* @param string $synonymId
65+
* @param array $config
66+
*
67+
* @return array
68+
* @throws TypesenseClientError|HttpClientException
69+
*/
70+
public function upsert(string $synonymId, array $config): array
71+
{
72+
return $this->apiCall->put($this->endPointPath($synonymId), $config);
73+
}
74+
75+
/**
76+
* @return array
77+
* @throws TypesenseClientError|HttpClientException
78+
*/
79+
public function retrieve(): array
80+
{
81+
return $this->apiCall->get($this->endPointPath(), []);
82+
}
83+
84+
/**
85+
* @inheritDoc
86+
*/
87+
public function offsetExists($synonymId): bool
88+
{
89+
return isset($this->synonyms[$synonymId]);
90+
}
91+
92+
/**
93+
* @inheritDoc
94+
*/
95+
public function offsetGet($synonymId)
96+
{
97+
if (!isset($this->synonyms[$synonymId])) {
98+
$this->synonyms[$synonymId] = new Synonym($this->collectionName, $synonymId, $this->apiCall);
99+
}
100+
101+
return $this->synonyms[$synonymId];
102+
}
103+
104+
/**
105+
* @inheritDoc
106+
*/
107+
public function offsetSet($synonymId, $value): void
108+
{
109+
$this->synonyms[$synonymId] = $value;
110+
}
111+
112+
/**
113+
* @inheritDoc
114+
*/
115+
public function offsetUnset($synonymId): void
116+
{
117+
unset($this->synonyms[$synonymId]);
118+
}
119+
}

0 commit comments

Comments
 (0)