Skip to content

Commit 3dcd34d

Browse files
feat(java): add bridge to transformation on search (#4939) (generated) [skip ci]
Co-authored-by: Clément Vannicatte <[email protected]>
1 parent c8efa23 commit 3dcd34d

File tree

16 files changed

+447
-2
lines changed

16 files changed

+447
-2
lines changed

clients/algoliasearch-client-java/algoliasearch/src/main/java/com/algolia/api/SearchClient.java

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88
import com.algolia.config.ClientOptions;
99
import com.algolia.exceptions.*;
1010
import com.algolia.internal.JsonSerializer;
11+
import com.algolia.model.ingestion.PushTaskPayload;
12+
import com.algolia.model.ingestion.PushTaskRecords;
13+
import com.algolia.model.ingestion.WatchResponse;
1114
import com.algolia.model.search.*;
1215
import com.algolia.utils.*;
1316
import com.fasterxml.jackson.core.type.TypeReference;
17+
import com.fasterxml.jackson.databind.ObjectMapper;
1418
import java.nio.charset.Charset;
1519
import java.security.InvalidKeyException;
1620
import java.security.NoSuchAlgorithmException;
@@ -36,6 +40,17 @@
3640

3741
public class SearchClient extends ApiClient {
3842

43+
private IngestionClient ingestionTransporter;
44+
45+
public void setTransformationRegion(String region) {
46+
this.ingestionTransporter = new IngestionClient(
47+
this.authInterceptor.getApplicationId(),
48+
this.authInterceptor.getApiKey(),
49+
region,
50+
this.clientOptions
51+
);
52+
}
53+
3954
public SearchClient(String appId, String apiKey) {
4055
this(appId, apiKey, null);
4156
}
@@ -6731,6 +6746,108 @@ public <T> List<BatchResponse> chunkedBatch(
67316746
return chunkedBatch(indexName, objects, action, waitForTasks, 1000, requestOptions);
67326747
}
67336748

6749+
/**
6750+
* Helper: Similar to the `saveObjects` method but requires a Push connector
6751+
* (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/)
6752+
* to be created first, in order to transform records before indexing them to Algolia. The
6753+
* `region` must have been passed to the client instantiation method.
6754+
*
6755+
* @param indexName The `indexName` to replace `objects` in.
6756+
* @param objects The array of `objects` to store in the given Algolia `indexName`.
6757+
* @throws AlgoliaRetryException When the retry has failed on all hosts
6758+
* @throws AlgoliaApiException When the API sends an http error code
6759+
* @throws AlgoliaRuntimeException When an error occurred during the serialization
6760+
*/
6761+
public <T> WatchResponse saveObjectsWithTransformation(String indexName, Iterable<T> objects) {
6762+
return saveObjectsWithTransformation(indexName, objects, null);
6763+
}
6764+
6765+
/**
6766+
* Helper: Similar to the `saveObjects` method but requires a Push connector
6767+
* (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/)
6768+
* to be created first, in order to transform records before indexing them to Algolia. The
6769+
* `region` must have been passed to the client instantiation method.
6770+
*
6771+
* @param indexName The `indexName` to replace `objects` in.
6772+
* @param objects The array of `objects` to store in the given Algolia `indexName`.
6773+
* @param requestOptions The requestOptions to send along with the query, they will be merged with
6774+
* the transporter requestOptions. (optional)
6775+
*/
6776+
public <T> WatchResponse saveObjectsWithTransformation(String indexName, Iterable<T> objects, RequestOptions requestOptions) {
6777+
return saveObjectsWithTransformation(indexName, objects, false, requestOptions);
6778+
}
6779+
6780+
/**
6781+
* Helper: Similar to the `saveObjects` method but requires a Push connector
6782+
* (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/)
6783+
* to be created first, in order to transform records before indexing them to Algolia. The
6784+
* `region` must have been passed to the client instantiation method.
6785+
*
6786+
* @param indexName The `indexName` to replace `objects` in.
6787+
* @param objects The array of `objects` to store in the given Algolia `indexName`.
6788+
* @param waitForTasks - Whether or not we should wait until every `batch` tasks has been
6789+
* processed, this operation may slow the total execution time of this method but is more
6790+
* reliable.
6791+
* @param requestOptions The requestOptions to send along with the query, they will be merged with
6792+
* the transporter requestOptions. (optional)
6793+
*/
6794+
public <T> WatchResponse saveObjectsWithTransformation(
6795+
String indexName,
6796+
Iterable<T> objects,
6797+
boolean waitForTasks,
6798+
RequestOptions requestOptions
6799+
) {
6800+
return saveObjectsWithTransformation(indexName, objects, waitForTasks, 1000, requestOptions);
6801+
}
6802+
6803+
/**
6804+
* Helper: Similar to the `saveObjects` method but requires a Push connector
6805+
* (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/)
6806+
* to be created first, in order to transform records before indexing them to Algolia. The
6807+
* `region` must have been passed to the client instantiation method.
6808+
*
6809+
* @param indexName The `indexName` to replace `objects` in.
6810+
* @param objects The array of `objects` to store in the given Algolia `indexName`.
6811+
* @param waitForTasks - Whether or not we should wait until every `batch` tasks has been
6812+
* processed, this operation may slow the total execution time of this method but is more
6813+
* reliable.
6814+
* @param batchSize The size of the chunk of `objects`. The number of `batch` calls will be equal
6815+
* to `length(objects) / batchSize`.
6816+
* @param requestOptions The requestOptions to send along with the query, they will be merged with
6817+
* the transporter requestOptions. (optional)
6818+
*/
6819+
public <T> WatchResponse saveObjectsWithTransformation(
6820+
String indexName,
6821+
Iterable<T> objects,
6822+
boolean waitForTasks,
6823+
int batchSize,
6824+
RequestOptions requestOptions
6825+
) {
6826+
if (this.ingestionTransporter == null) {
6827+
throw new AlgoliaRuntimeException("`setTransformationRegion` must have been called before calling this method.");
6828+
}
6829+
6830+
return this.ingestionTransporter.push(
6831+
indexName,
6832+
new PushTaskPayload().setAction(com.algolia.model.ingestion.Action.ADD_OBJECT).setRecords(this.objectsToPushTaskRecords(objects)),
6833+
waitForTasks,
6834+
requestOptions
6835+
);
6836+
}
6837+
6838+
private <T> List<PushTaskRecords> objectsToPushTaskRecords(Iterable<T> objects) {
6839+
try {
6840+
ObjectMapper mapper = new ObjectMapper();
6841+
String json = mapper.writeValueAsString(objects);
6842+
6843+
return mapper.readValue(json, new TypeReference<List<PushTaskRecords>>() {});
6844+
} catch (Exception e) {
6845+
throw new AlgoliaRuntimeException(
6846+
"each object must have an `objectID` key in order to be used with the" + " WithTransformation methods"
6847+
);
6848+
}
6849+
}
6850+
67346851
/**
67356852
* Helper: Saves the given array of objects in the given index. The `chunkedBatch` helper is used
67366853
* under the hood, which creates a `batch` requests with at most 1000 objects in it.
@@ -6867,6 +6984,114 @@ public List<BatchResponse> deleteObjects(
68676984
return chunkedBatch(indexName, objects, Action.DELETE_OBJECT, waitForTasks, batchSize, requestOptions);
68686985
}
68696986

6987+
/**
6988+
* Helper: Similar to the `partialUpdateObjects` method but requires a Push connector
6989+
* (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/)
6990+
* to be created first, in order to transform records before indexing them to Algolia. The
6991+
* `region` must have been passed to the client instantiation method.
6992+
*
6993+
* @param indexName The `indexName` to update `objects` in.
6994+
* @param objects The array of `objects` to update in the given Algolia `indexName`.
6995+
* @param createIfNotExists To be provided if non-existing objects are passed, otherwise, the call
6996+
* will fail.
6997+
*/
6998+
public <T> WatchResponse partialUpdateObjectsWithTransformation(String indexName, Iterable<T> objects, boolean createIfNotExists) {
6999+
return partialUpdateObjectsWithTransformation(indexName, objects, createIfNotExists, false, null);
7000+
}
7001+
7002+
/**
7003+
* Helper: Similar to the `partialUpdateObjects` method but requires a Push connector
7004+
* (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/)
7005+
* to be created first, in order to transform records before indexing them to Algolia. The
7006+
* `region` must have been passed to the client instantiation method.
7007+
*
7008+
* @param indexName The `indexName` to update `objects` in.
7009+
* @param objects The array of `objects` to update in the given Algolia `indexName`.
7010+
* @param createIfNotExists To be provided if non-existing objects are passed, otherwise, the call
7011+
* will fail.
7012+
* @param waitForTasks - Whether or not we should wait until every `batch` tasks has been
7013+
* processed, this operation may slow the total execution time of this method but is more
7014+
* reliable.
7015+
*/
7016+
public <T> WatchResponse partialUpdateObjectsWithTransformation(
7017+
String indexName,
7018+
Iterable<T> objects,
7019+
boolean createIfNotExists,
7020+
boolean waitForTasks
7021+
) {
7022+
return partialUpdateObjectsWithTransformation(indexName, objects, createIfNotExists, waitForTasks, null);
7023+
}
7024+
7025+
/**
7026+
* Helper: Similar to the `partialUpdateObjects` method but requires a Push connector
7027+
* (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/)
7028+
* to be created first, in order to transform records before indexing them to Algolia. The
7029+
* `region` must have been passed to the client instantiation method.
7030+
*
7031+
* @param indexName The `indexName` to update `objects` in.
7032+
* @param objects The array of `objects` to update in the given Algolia `indexName`.
7033+
* @param createIfNotExists To be provided if non-existing objects are passed, otherwise, the call
7034+
* will fail.
7035+
* @param waitForTasks - Whether or not we should wait until every `batch` tasks has been
7036+
* processed, this operation may slow the total execution time of this method but is more
7037+
* reliable.
7038+
* @param requestOptions The requestOptions to send along with the query, they will be merged with
7039+
* the transporter requestOptions. (optional)
7040+
*/
7041+
public <T> WatchResponse partialUpdateObjectsWithTransformation(
7042+
String indexName,
7043+
Iterable<T> objects,
7044+
boolean createIfNotExists,
7045+
boolean waitForTasks,
7046+
RequestOptions requestOptions
7047+
) {
7048+
return partialUpdateObjectsWithTransformation(indexName, objects, createIfNotExists, waitForTasks, 1000, null);
7049+
}
7050+
7051+
/**
7052+
* Helper: Similar to the `partialUpdateObjects` method but requires a Push connector
7053+
* (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/)
7054+
* to be created first, in order to transform records before indexing them to Algolia. The
7055+
* `region` must have been passed to the client instantiation method.
7056+
*
7057+
* @param indexName The `indexName` to update `objects` in.
7058+
* @param objects The array of `objects` to update in the given Algolia `indexName`.
7059+
* @param createIfNotExists To be provided if non-existing objects are passed, otherwise, the call
7060+
* will fail.
7061+
* @param waitForTasks - Whether or not we should wait until every `batch` tasks has been
7062+
* processed, this operation may slow the total execution time of this method but is more
7063+
* reliable.
7064+
* @param batchSize The size of the chunk of `objects`. The number of `batch` calls will be equal
7065+
* to `length(objects) / batchSize`.
7066+
* @param requestOptions The requestOptions to send along with the query, they will be merged with
7067+
* the transporter requestOptions. (optional)
7068+
*/
7069+
public <T> WatchResponse partialUpdateObjectsWithTransformation(
7070+
String indexName,
7071+
Iterable<T> objects,
7072+
boolean createIfNotExists,
7073+
boolean waitForTasks,
7074+
int batchSize,
7075+
RequestOptions requestOptions
7076+
) {
7077+
if (this.ingestionTransporter == null) {
7078+
throw new AlgoliaRuntimeException("`setTransformationRegion` must have been called before calling this method.");
7079+
}
7080+
7081+
return this.ingestionTransporter.push(
7082+
indexName,
7083+
new PushTaskPayload()
7084+
.setAction(
7085+
createIfNotExists
7086+
? com.algolia.model.ingestion.Action.PARTIAL_UPDATE_OBJECT
7087+
: com.algolia.model.ingestion.Action.PARTIAL_UPDATE_OBJECT_NO_CREATE
7088+
)
7089+
.setRecords(this.objectsToPushTaskRecords(objects)),
7090+
waitForTasks,
7091+
requestOptions
7092+
);
7093+
}
7094+
68707095
/**
68717096
* Helper: Replaces object content of all the given objects according to their respective
68727097
* `objectID` field. The `chunkedBatch` helper is used under the hood, which creates a `batch`

docs/bundled/search-snippets.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1927,6 +1927,9 @@
19271927
"call partialUpdateObjects with createIfNotExists=true": "client.partialUpdateObjects(\n \"<YOUR_INDEX_NAME>\",\n Arrays.asList(\n new HashMap() {\n {\n put(\"objectID\", \"1\");\n put(\"name\", \"Adam\");\n }\n },\n new HashMap() {\n {\n put(\"objectID\", \"2\");\n put(\"name\", \"Benoit\");\n }\n }\n ),\n true\n);",
19281928
"call partialUpdateObjects with createIfNotExists=false": "client.partialUpdateObjects(\n \"<YOUR_INDEX_NAME>\",\n Arrays.asList(\n new HashMap() {\n {\n put(\"objectID\", \"3\");\n put(\"name\", \"Cyril\");\n }\n },\n new HashMap() {\n {\n put(\"objectID\", \"4\");\n put(\"name\", \"David\");\n }\n }\n ),\n false\n);"
19291929
},
1930+
"partialUpdateObjectsWithTransformation": {
1931+
"default": "client.partialUpdateObjectsWithTransformation(\n \"<YOUR_INDEX_NAME>\",\n Arrays.asList(\n new HashMap() {\n {\n put(\"objectID\", \"1\");\n put(\"name\", \"Adam\");\n }\n },\n new HashMap() {\n {\n put(\"objectID\", \"2\");\n put(\"name\", \"Benoit\");\n }\n }\n ),\n true\n);"
1932+
},
19301933
"removeUserId": {
19311934
"default": "client.removeUserId(\"uniqueID\");"
19321935
},
@@ -1950,6 +1953,9 @@
19501953
"saveObjectsPlaylist": "client.saveObjects(\n \"<YOUR_INDEX_NAME>\",\n Arrays.asList(\n new HashMap() {\n {\n put(\"objectID\", \"1\");\n put(\"visibility\", \"public\");\n put(\"name\", \"Hot 100 Billboard Charts\");\n put(\"playlistId\", \"d3e8e8f3-0a4f-4b7d-9b6b-7e8f4e8e3a0f\");\n put(\"createdAt\", \"1500240452\");\n }\n }\n )\n);",
19511954
"saveObjectsPublicUser": "client.saveObjects(\n \"<YOUR_INDEX_NAME>\",\n Arrays.asList(\n new HashMap() {\n {\n put(\"objectID\", \"1\");\n put(\"visibility\", \"public\");\n put(\"name\", \"Hot 100 Billboard Charts\");\n put(\"playlistId\", \"d3e8e8f3-0a4f-4b7d-9b6b-7e8f4e8e3a0f\");\n put(\"createdAt\", \"1500240452\");\n }\n }\n ),\n false,\n 1000,\n new RequestOptions().addExtraHeader(\"X-Algolia-User-ID\", \"*\")\n);"
19521955
},
1956+
"saveObjectsWithTransformation": {
1957+
"default": "client.saveObjectsWithTransformation(\n \"<YOUR_INDEX_NAME>\",\n Arrays.asList(\n new HashMap() {\n {\n put(\"objectID\", \"1\");\n put(\"name\", \"Adam\");\n }\n },\n new HashMap() {\n {\n put(\"objectID\", \"2\");\n put(\"name\", \"Benoit\");\n }\n }\n )\n);"
1958+
},
19531959
"saveRule": {
19541960
"saveRule with minimal parameters": "client.saveRule(\n \"<YOUR_INDEX_NAME>\",\n \"id1\",\n new Rule()\n .setObjectID(\"id1\")\n .setConditions(Arrays.asList(new Condition().setPattern(\"apple\").setAnchoring(Anchoring.CONTAINS)))\n .setConsequence(new Consequence().setParams(new ConsequenceParams().setFilters(\"brand:xiaomi\")))\n);",
19551961
"saveRule with all parameters": "client.saveRule(\n \"<YOUR_INDEX_NAME>\",\n \"id1\",\n new Rule()\n .setObjectID(\"id1\")\n .setConditions(\n Arrays.asList(new Condition().setPattern(\"apple\").setAnchoring(Anchoring.CONTAINS).setAlternatives(false).setContext(\"search\"))\n )\n .setConsequence(\n new Consequence()\n .setParams(\n new ConsequenceParams()\n .setFilters(\"brand:apple\")\n .setQuery(\n new ConsequenceQueryObject()\n .setRemove(Arrays.asList(\"algolia\"))\n .setEdits(\n Arrays.asList(\n new Edit().setType(EditType.REMOVE).setDelete(\"abc\").setInsert(\"cde\"),\n new Edit().setType(EditType.REPLACE).setDelete(\"abc\").setInsert(\"cde\")\n )\n )\n )\n )\n .setHide(Arrays.asList(new ConsequenceHide().setObjectID(\"321\")))\n .setFilterPromotes(false)\n .setUserData(\n new HashMap() {\n {\n put(\"algolia\", \"aloglia\");\n }\n }\n )\n .setPromote(\n Arrays.asList(\n new PromoteObjectID().setObjectID(\"abc\").setPosition(3),\n new PromoteObjectIDs().setObjectIDs(Arrays.asList(\"abc\", \"def\")).setPosition(1)\n )\n )\n )\n .setDescription(\"test\")\n .setEnabled(true)\n .setValidity(Arrays.asList(new TimeRange().setFrom(1656670273L).setUntil(1656670277L))),\n true\n);",

docs/bundled/search.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4340,7 +4340,8 @@
43404340
"x-available-languages": [
43414341
"javascript",
43424342
"go",
4343-
"python"
4343+
"python",
4344+
"java"
43444345
],
43454346
"tags": [
43464347
"Records"
@@ -4593,7 +4594,8 @@
45934594
"x-available-languages": [
45944595
"javascript",
45954596
"go",
4596-
"python"
4597+
"python",
4598+
"java"
45974599
],
45984600
"tags": [
45994601
"Records"

docs/snippets/java/src/test/java/com/algolia/Search.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,6 +1955,37 @@ void snippetForPartialUpdateObjects1() throws Exception {
19551955
// SEPARATOR<
19561956
}
19571957

1958+
// Snippet for the partialUpdateObjectsWithTransformation method.
1959+
//
1960+
// call partialUpdateObjectsWithTransformation with createIfNotExists=true
1961+
void snippetForPartialUpdateObjectsWithTransformation() throws Exception {
1962+
// >SEPARATOR partialUpdateObjectsWithTransformation default
1963+
// Initialize the client
1964+
SearchClient client = new SearchClient("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY");
1965+
1966+
// Call the API
1967+
client.partialUpdateObjectsWithTransformation(
1968+
"<YOUR_INDEX_NAME>",
1969+
Arrays.asList(
1970+
new HashMap() {
1971+
{
1972+
put("objectID", "1");
1973+
put("name", "Adam");
1974+
}
1975+
},
1976+
new HashMap() {
1977+
{
1978+
put("objectID", "2");
1979+
put("name", "Benoit");
1980+
}
1981+
}
1982+
),
1983+
true
1984+
);
1985+
// >LOG
1986+
// SEPARATOR<
1987+
}
1988+
19581989
// Snippet for the removeUserId method.
19591990
//
19601991
// removeUserId
@@ -2279,6 +2310,36 @@ void snippetForSaveObjects3() throws Exception {
22792310
// SEPARATOR<
22802311
}
22812312

2313+
// Snippet for the saveObjectsWithTransformation method.
2314+
//
2315+
// call saveObjectsWithTransformation without error
2316+
void snippetForSaveObjectsWithTransformation() throws Exception {
2317+
// >SEPARATOR saveObjectsWithTransformation default
2318+
// Initialize the client
2319+
SearchClient client = new SearchClient("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY");
2320+
2321+
// Call the API
2322+
client.saveObjectsWithTransformation(
2323+
"<YOUR_INDEX_NAME>",
2324+
Arrays.asList(
2325+
new HashMap() {
2326+
{
2327+
put("objectID", "1");
2328+
put("name", "Adam");
2329+
}
2330+
},
2331+
new HashMap() {
2332+
{
2333+
put("objectID", "2");
2334+
put("name", "Benoit");
2335+
}
2336+
}
2337+
)
2338+
);
2339+
// >LOG
2340+
// SEPARATOR<
2341+
}
2342+
22822343
// Snippet for the saveRule method.
22832344
//
22842345
// saveRule with minimal parameters

specs/bundled/search.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3699,6 +3699,7 @@ paths:
36993699
- javascript
37003700
- go
37013701
- python
3702+
- java
37023703
tags:
37033704
- search
37043705
operationId: saveObjectsWithTransformation
@@ -3903,6 +3904,7 @@ paths:
39033904
- javascript
39043905
- go
39053906
- python
3907+
- java
39063908
tags:
39073909
- search
39083910
operationId: partialUpdateObjectsWithTransformation

tests/output/java/src/test/java/com/algolia/benchmark/Search.test.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ void benchmarkTest0() {
4848
false
4949
)
5050
);
51+
5152
for (int i = 0; i < 2000; i++) {
5253
SearchResponses res = client.search(
5354
new SearchMethodParams()

0 commit comments

Comments
 (0)