Skip to content

Commit b47d6f7

Browse files
committed
add upsert and search records
1 parent c83ea24 commit b47d6f7

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

src/main/java/io/pinecone/clients/Pinecone.java

+6
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,12 @@ public AsyncIndex getAsyncIndexConnection(String indexName) throws PineconeValid
982982
return new AsyncIndex(config, connection, indexName);
983983
}
984984

985+
public RestIndex getRestIndexConnection(String indexName) throws PineconeValidationException {
986+
PineconeConfig perConnectionConfig = new PineconeConfig(config.getApiKey(), config.getSourceTag());
987+
perConnectionConfig.setHost(getIndexHost(indexName));
988+
return new RestIndex(perConnectionConfig);
989+
}
990+
985991
/**
986992
* A method to create and return a new instance of the {@link Inference} client.
987993
* <p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package io.pinecone.clients;
2+
3+
public class RestAsyncIndex {
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package io.pinecone.clients;
2+
3+
import io.pinecone.configs.PineconeConfig;
4+
import okhttp3.OkHttpClient;
5+
import org.openapitools.db_data.client.ApiClient;
6+
import org.openapitools.db_data.client.ApiException;
7+
import org.openapitools.db_data.client.Configuration;
8+
import org.openapitools.db_data.client.api.VectorOperationsApi;
9+
import org.openapitools.db_data.client.model.*;
10+
11+
import java.util.List;
12+
13+
import static io.pinecone.clients.Pinecone.buildOkHttpClient;
14+
15+
public class RestIndex {
16+
private final VectorOperationsApi vectorOperations;
17+
18+
public RestIndex(PineconeConfig config) {
19+
OkHttpClient customOkHttpClient = config.getCustomOkHttpClient();
20+
ApiClient apiClient = (customOkHttpClient != null) ? new ApiClient(customOkHttpClient) : new ApiClient(buildOkHttpClient(config.getProxyConfig()));
21+
apiClient.setApiKey(config.getApiKey());
22+
apiClient.setUserAgent(config.getUserAgent());
23+
apiClient.addDefaultHeader("X-Pinecone-Api-Version", Configuration.VERSION);
24+
25+
this.vectorOperations = new VectorOperationsApi(apiClient);
26+
String protocol = config.isTLSEnabled() ? "https://" : "http://";
27+
vectorOperations.setCustomBaseUrl(protocol + config.getHost());
28+
}
29+
30+
public void upsertRecords(String namespace, List<UpsertRecord> upsertRecord) throws ApiException {
31+
vectorOperations.upsertRecordsNamespace(namespace, upsertRecord);
32+
}
33+
34+
public SearchRecordsResponse searchRecords(String id, String namespace, Object filter, int topK, SearchRecordsVector vector) throws ApiException {
35+
SearchRecordsRequestQuery searchRecordsRequestquery = new SearchRecordsRequestQuery()
36+
.id(id)
37+
.filter(filter)
38+
.topK(topK)
39+
.vector(vector);
40+
SearchRecordsRequest request = new SearchRecordsRequest().query(searchRecordsRequestquery);
41+
42+
return vectorOperations.searchRecordsNamespace(namespace, request);
43+
}
44+
}

src/main/java/org/openapitools/db_data/client/ApiClient.java

+12
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,18 @@ public RequestBody serialize(Object obj, String contentType) throws ApiException
931931
return RequestBody.create((File) obj, MediaType.parse(contentType));
932932
} else if ("text/plain".equals(contentType) && obj instanceof String) {
933933
return RequestBody.create((String) obj, MediaType.parse(contentType));
934+
} else if ("application/x-ndjson".equals(contentType)) {
935+
// Handle NDJSON (Newline Delimited JSON)
936+
if (obj instanceof Iterable) { // If obj is a collection of objects
937+
StringBuilder ndjsonContent = new StringBuilder();
938+
for (Object item : (Iterable<?>) obj) {
939+
String json = JSON.serialize(item);
940+
ndjsonContent.append(json).append("\n");
941+
}
942+
return RequestBody.create(ndjsonContent.toString(), MediaType.parse(contentType));
943+
} else {
944+
throw new ApiException("NDJSON content requires a collection of objects.");
945+
}
934946
} else if (isJsonMime(contentType)) {
935947
String content;
936948
if (obj != null) {

0 commit comments

Comments
 (0)