Skip to content

Commit 7a90549

Browse files
committed
Added support for document updates and upserts
1 parent 314fd10 commit 7a90549

4 files changed

Lines changed: 84 additions & 14 deletions

File tree

examples/collection_operations.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,41 @@
104104
]
105105
)
106106
);
107+
108+
// You can also upsert a document, by passing an upsert => true option:
109+
//$client->collections['books']->documents->create(
110+
// [
111+
// 'id' => '1',
112+
// 'original_publication_year' => 2008,
113+
// 'authors' => [
114+
// 'Suzanne Collins',
115+
// ],
116+
// 'average_rating' => 4.34,
117+
// 'publication_year' => 2008,
118+
// 'publication_year_facet' => '2008',
119+
// 'authors_facet' => [
120+
// 'Suzanne Collins',
121+
// ],
122+
// 'title' => 'The Hunger Games',
123+
// 'image_url' => 'https://images.gr-assets.com/books/1447303603m/2767052.jpg',
124+
// 'ratings_count' => 4780653,
125+
// ],
126+
// ['upsert' => true]
127+
//);
107128
echo "--------Create Document-------\n";
108129
echo "\n";
109130
echo "--------Export Documents-------\n";
110131
$exportedDocStrs = $client->collections['books']->documents->export();
111132
print_r($exportedDocStrs);
112133
echo "--------Export Documents-------\n";
113134
echo "\n";
135+
echo "--------Update Single Document-------\n";
136+
print_r($client->collections['books']->documents['1']->update([
137+
'id' => '1',
138+
'average_rating' => 4.5,
139+
]));
140+
echo "--------Update Single Document-------\n";
141+
echo "\n";
114142
echo "--------Fetch Single Document-------\n";
115143
print_r($client->collections['books']->documents['1']->retrieve());
116144
echo "--------Fetch Single Document-------\n";
@@ -140,8 +168,20 @@
140168
$importRes =
141169
$client->collections['books']->documents->createMany($docsToImport);
142170
print_r($importRes);
171+
172+
// Or if you have documents in JSONL format, and want to save the overhead of parsing JSON,
173+
// you can also use the import method:
174+
// $client->collections['books']->documents->import($documentsJSONLString);
143175
echo "--------Import Documents-------\n";
144176
echo "\n";
177+
echo "--------Upsert Documents-------\n";
178+
$upsertRes =
179+
$client->collections['books']->documents->createMany($docsToImport, [
180+
'upsert' => true
181+
]);
182+
print_r($upsertRes);
183+
echo "--------Upsert Documents-------\n";
184+
echo "\n";
145185
echo "--------Delete Collection-------\n";
146186
print_r($client->collections['books']->delete());
147187
echo "--------Delete Collection-------\n";

src/ApiCall.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ private function initializeNodes(): void
9696
public function get(string $endPoint, array $params, bool $asJson = true)
9797
{
9898
return $this->makeRequest('get', $endPoint, $asJson, [
99-
'data' => $params ?? [],
99+
'query' => $params ?? [],
100100
]);
101101
}
102102

@@ -105,41 +105,49 @@ public function get(string $endPoint, array $params, bool $asJson = true)
105105
* @param mixed $body
106106
*
107107
* @param bool $asJson
108+
* @param array $queryParameters
108109
*
109110
* @return array|string
110111
* @throws TypesenseClientError
111112
* @throws GuzzleException
112113
*/
113-
public function post(string $endPoint, $body, bool $asJson = true)
114+
public function post(string $endPoint, $body, bool $asJson = true, array $queryParameters = [])
114115
{
115116
return $this->makeRequest('post', $endPoint, $asJson, [
116117
'data' => $body ?? [],
118+
'query' => $queryParameters ?? []
117119
]);
118120
}
119121

120122
/**
121123
* @param string $endPoint
122124
* @param array $body
123125
*
126+
* @param array $queryParameters
127+
*
124128
* @return array
125129
* @throws TypesenseClientError|GuzzleException
126130
*/
127-
public function put(string $endPoint, array $body): array
131+
public function put(string $endPoint, array $body, array $queryParameters = []): array
128132
{
129133
return $this->makeRequest('put', $endPoint, true, [
130134
'data' => $body ?? [],
135+
'query' => $queryParameters ?? []
131136
]);
132137
}
133138

134139
/**
135140
* @param string $endPoint
141+
* @param array $queryParameters
136142
*
137143
* @return array
138144
* @throws TypesenseClientError|GuzzleException
139145
*/
140-
public function delete(string $endPoint): array
146+
public function delete(string $endPoint, array $queryParameters = []): array
141147
{
142-
return $this->makeRequest('delete', $endPoint, true, []);
148+
return $this->makeRequest('delete', $endPoint, true, [
149+
'query' => $queryParameters ?? []
150+
]);
143151
}
144152

145153
/**
@@ -166,15 +174,22 @@ private function makeRequest(string $method, string $endPoint, bool $asJson, arr
166174
$url = $node->url() . $endPoint;
167175
$reqOp = $this->getRequestOptions();
168176
if (isset($options['data'])) {
169-
if ($method === 'get') {
170-
$reqOp['query'] = http_build_query($options['data']);
171-
} elseif (is_string($options['data'])) {
177+
if (is_string($options['data'])) {
172178
$reqOp['body'] = $options['data'];
173179
} else {
174180
$reqOp['json'] = $options['data'];
175181
}
176182
}
177183

184+
if (isset($options['query'])) {
185+
foreach ($options['query'] as $key => $value) :
186+
if (is_bool($value)) {
187+
$options['query'][$key] = ($value) ? 'true' : 'false';
188+
}
189+
endforeach;
190+
$reqOp['query'] = http_build_query($options['query']);
191+
}
192+
178193
$response = $this->client->request($method, $url, $reqOp);
179194

180195
$statusCode = $response->getStatusCode();

src/Document.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ public function retrieve(): array
6767
return $this->apiCall->get($this->endpointPath(), []);
6868
}
6969

70+
/**
71+
* @param array $partialDocument
72+
*
73+
* @return array
74+
* @throws TypesenseClientError|GuzzleException
75+
*/
76+
public function update(array $partialDocument): array
77+
{
78+
return $this->apiCall->put($this->endpointPath(), $partialDocument);
79+
}
80+
7081
/**
7182
* @return array
7283
* @throws TypesenseClientError|GuzzleException

src/Documents.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,24 @@ private function endPointPath(string $action = ''): string
5656

5757
/**
5858
* @param array $document
59+
* @param array $options
5960
*
6061
* @return array
6162
* @throws TypesenseClientError|GuzzleException
6263
*/
63-
public function create(array $document): array
64+
public function create(array $document, array $options = []): array
6465
{
65-
return $this->apiCall->post($this->endPointPath(''), $document);
66+
return $this->apiCall->post($this->endPointPath(''), $document, true, $options);
6667
}
6768

6869
/**
6970
* @param array $documents
71+
* @param array $options
7072
*
7173
* @return array
7274
* @throws TypesenseClientError|GuzzleException|\JsonException
7375
*/
74-
public function createMany(array $documents): array
76+
public function createMany(array $documents, array $options = []): array
7577
{
7678
$res = $this->import(
7779
implode(
@@ -80,7 +82,8 @@ public function createMany(array $documents): array
8082
static fn(array $document) => json_encode($document, JSON_THROW_ON_ERROR),
8183
$documents
8284
)
83-
)
85+
),
86+
$options
8487
);
8588
return array_map(static function ($item) {
8689
return json_decode($item, true, 512, JSON_THROW_ON_ERROR);
@@ -89,14 +92,15 @@ public function createMany(array $documents): array
8992

9093
/**
9194
* @param string $documents
95+
* @param array $options
9296
*
9397
* @return string
9498
* @throws TypesenseClientError
9599
* @throws GuzzleException
96100
*/
97-
public function import(string $documents): string
101+
public function import(string $documents, array $options = []): string
98102
{
99-
return $this->apiCall->post($this->endPointPath('import'), $documents, false);
103+
return $this->apiCall->post($this->endPointPath('import'), $documents, false, $options);
100104
}
101105

102106
/**

0 commit comments

Comments
 (0)