Skip to content

Commit 11ffe84

Browse files
committed
feat(php): add bridge to transformation on search
1 parent 8a275cd commit 11ffe84

File tree

6 files changed

+81
-5
lines changed

6 files changed

+81
-5
lines changed

scripts/cts/runCts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export async function runCts(
158158
assertValidReplaceAllObjectsFailed(languages.length - skip('dart'));
159159
assertValidReplaceAllObjectsScopes(languages.length - skip('dart'));
160160
assertValidWaitForApiKey(languages.length - skip('dart'));
161-
assertPushMockValid(only('javascript') + only('go') + only('python') + only('java'));
161+
assertPushMockValid(only('javascript') + only('go') + only('python') + only('java') + only('php'));
162162
}
163163
if (withBenchmarkServer) {
164164
printBenchmarkReport();

specs/search/helpers/partialUpdateObjectsWithTransformation.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ method:
22
post:
33
x-helper: true
44
x-available-languages:
5-
- javascript
65
- go
7-
- python
86
- java
7+
- javascript
8+
- php
9+
- python
910
tags:
1011
- Records
1112
operationId: partialUpdateObjectsWithTransformation

specs/search/helpers/saveObjectsWithTransformation.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ method:
22
get:
33
x-helper: true
44
x-available-languages:
5-
- javascript
65
- go
7-
- python
86
- java
7+
- javascript
8+
- php
9+
- python
910
tags:
1011
- Records
1112
operationId: saveObjectsWithTransformation

templates/java/api.mustache

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ public class {{classname}} extends ApiClient {
5656
{{#isSearchClient}}
5757
private IngestionClient ingestionTransporter;
5858

59+
/**
60+
* Sets the region of the current algolia application to the configuration, this is required to be called if you wish to leverage the transformation pipeline (via the *WithTransformation methods).
61+
*
62+
* @param region (required)
63+
*/
5964
public void setTransformationRegion(String region) {
6065
this.ingestionTransporter = new IngestionClient(this.authInterceptor.getApplicationId(), this.authInterceptor.getApiKey(), region, this.clientOptions);
6166
}

templates/php/api.mustache

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ use Algolia\AlgoliaSearch\Exceptions\NotFoundException;
3434
*/
3535
protected $api;
3636
37+
/**
38+
* @var IngestionClient
39+
*/
40+
protected $ingestionTransporter;
41+
3742
/**
3843
* @var {{configClassname}}
3944
*/
@@ -93,6 +98,16 @@ use Algolia\AlgoliaSearch\Exceptions\NotFoundException;
9398
self::getClusterHosts($config)
9499
);
95100
101+
if (null !== $config->getTransformationRegion()) {
102+
$ingestionConfig = IngestionConfig::create($appId, $apiKey, $config->getTransformationRegion());
103+
104+
if (isset($config->hosts)) {
105+
$ingestionConfig['hosts'] = $config->hosts;
106+
}
107+
108+
$this->ingestionTransporter = new IngestionClient($config->appId, $config->apiKey, $config->getTransformationRegion(), $ingestionConfig);
109+
}
110+
96111
return new static($apiWrapper, $config);
97112
}
98113

@@ -514,6 +529,23 @@ use Algolia\AlgoliaSearch\Exceptions\NotFoundException;
514529
return $this->chunkedBatch($indexName, $objects, 'addObject', $waitForTasks, $batchSize, $requestOptions);
515530
}
516531

532+
/**
533+
* Helper: Similar to the `saveObjects` method but requires a Push connector
534+
* (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/)
535+
* to be created first, in order to transform records before indexing them to Algolia. The
536+
* `region` must have been passed to the client instantiation method.
537+
*
538+
* @param string $indexName the `indexName` to replace `objects` in
539+
* @param array $objects the array of `objects` to store in the given Algolia `indexName`
540+
* @param bool $waitForTasks Whether or not we should wait until every `batch` tasks has been processed, this operation may slow the total execution time of this method but is more reliable
541+
* @param array $batchSize The size of the chunk of `objects`. The number of `batch` calls will be equal to `length(objects) / batchSize`. Defaults to 1000.
542+
* @param array $requestOptions Request options
543+
*/
544+
public function saveObjectsWithTransformation($indexName, $objects, $waitForTasks = false, $batchSize = 1000, $requestOptions = [])
545+
{
546+
return $this->ingestionTransporter->push($indexName, 'addObject', $objects, $waitForTasks, $requestOptions);
547+
}
548+
517549
/**
518550
* Helper: Deletes every records for the given objectIDs. The `chunkedBatch` helper is used under the hood, which creates a `batch` requests with at most 1000 objectIDs in it.
519551
*
@@ -548,6 +580,24 @@ use Algolia\AlgoliaSearch\Exceptions\NotFoundException;
548580
return $this->chunkedBatch($indexName, $objects, ($createIfNotExists == TRUE) ? 'partialUpdateObject' : 'partialUpdateObjectNoCreate', $waitForTasks, $batchSize, $requestOptions);
549581
}
550582

583+
/**
584+
* Helper: Similar to the `partialUpdateObjects` method but requires a Push connector
585+
* (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/)
586+
* to be created first, in order to transform records before indexing them to Algolia. The
587+
* `region` must have been passed to the client instantiation method.
588+
*
589+
* @param string $indexName the `indexName` to replace `objects` in
590+
* @param array $objects the array of `objects` to store in the given Algolia `indexName`
591+
* @param bool $createIfNotExists To be provided if non-existing objects are passed, otherwise, the call will fail..
592+
* @param bool $waitForTasks Whether or not we should wait until every `batch` tasks has been processed, this operation may slow the total execution time of this method but is more reliable
593+
* @param array $batchSize The size of the chunk of `objects`. The number of `batch` calls will be equal to `length(objects) / batchSize`. Defaults to 1000.
594+
* @param array $requestOptions Request options
595+
*/
596+
public function partialUpdateObjectsWithTransformation($indexName, $objects, $createIfNotExists, $waitForTasks = false, $batchSize = 1000, $requestOptions = [])
597+
{
598+
return $this->ingestionTransporter->push($indexName, (true == $createIfNotExists) ? 'partialUpdateObject' : 'partialUpdateObjectNoCreate', $objects, $waitForTasks, $requestOptions);
599+
}
600+
551601
/**
552602
* Helper: Chunks the given `objects` list in subset of 1000 elements max in order to make it fit in `batch` requests.
553603
*

templates/php/client_config.mustache

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,25 @@ class {{configClassname}} extends {{#hasRegionalHost}}ConfigWithRegion{{/hasRegi
6666
{
6767
return $this->config['defaultMaxRetries'];
6868
}
69+
70+
/**
71+
* Sets the region of the current algolia application to the configuration, this is required to be called if you wish to leverage the transformation pipeline (via the *WithTransformation methods).
72+
*
73+
* @param string $region the user agent of the api client
74+
*
75+
* @return $this
76+
*/
77+
public function setTransformationRegion($region)
78+
{
79+
$this->config['region'] = $region;
80+
81+
return $this;
82+
}
83+
84+
public function getTransformationRegion()
85+
{
86+
return $this->config['region'];
87+
}
6988
{{/isSearchClient}}
7089

7190
public function getDefaultConfiguration()

0 commit comments

Comments
 (0)