Cette rubrique explique comment utiliser les SDK OpenSearch Vector Search Edition pour Java et Python afin de mettre à jour les données d'une table. Vous pouvez télécharger, mettre à jour et supprimer des documents.
Dépendances
Java
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-sdk-ha3engine-vector</artifactId>
<version>1.1.18</version>
</dependency>
Python
# Requires: Python >=3.6
pip install alibabacloud_ha3engine_vector==1.1.18
Go
go get github.com/aliyun/alibabacloud-ha3-go-sdk@v1.1.18-vector
Asynchronous Java
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-sdk-ha3engine-async</artifactId>
<version>1.1.8</version>
</dependency>
description des paramètres
Vous devez spécifier les paramètres suivants dans les SDK pour Java et Python : endpoint, instance_id, access_user_name, access_pass_word et data_source_name.
endpoint: l'endpoint interne ou public. Spécifiez uniquement le nom de domaine, sans le préfixehttp://, par exempleha-cn-xxxxxxxx.public.ha.aliyuncs.com.
Ces informations sont disponibles sur la page Instance Details, dans les sections Network Information et API Endpoint.
Après avoir activé Public Access, vous pouvez accéder à l'instance OpenSearch Vector Search Edition depuis votre machine locale en utilisant l'endpoint public de l'instance. Un endpoint public contient le mot « public ». Vous pouvez configurer une liste d'autorisation d'adresses IP pour l'accès à l'instance. Pour plus d'informations, consultez la section Network Information de la rubrique « Afficher les détails de l'instance ». Si vous ne configurez pas cette liste d'autorisation d'adresses IP, une erreur d'interdiction d'accès sera renvoyée. Nous vous recommandons d'effectuer un ping vers l'instance avant de l'utiliser afin de vérifier qu'elle est accessible.
Si vous utilisez une instance Elastic Compute Service (ECS) pour accéder à l'instance OpenSearch Vector Search Edition, vous pouvez spécifier le même vSwitch que celui de l'instance ECS afin d'accéder à l'instance OpenSearch Vector Search Edition via l'API endpoint.
instance_id : l'ID de l'instance OpenSearch Vector Search Edition.
access_user_name : le nom d'utilisateur.
access_pass_word : le mot de passe.
Vous pouvez consulter le nom d'utilisateur et le mot de passe dans la section API Endpoint de la page Instance Details. Le mot de passe est défini lors de l'achat de l'instance et peut être modifié.
data_source_name: le nom de la source de données pour la soumission des données via l'API. Le format par défaut est<instance_id>_<table_name>.
data_source_name=ha-cn-zpr3dgzxg04_test_image_vector
Démonstration de mise à jour des données
Télécharger un document
Java
import com.aliyun.ha3engine.vector.Client;
import com.aliyun.ha3engine.vector.models.Config;
import com.aliyun.ha3engine.vector.models.PushDocumentsRequest;
import com.aliyun.ha3engine.vector.models.PushDocumentsResponse;
import com.aliyun.tea.TeaException;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
* @author alibaba
*/
public class PushDoc {
/**
* The OpenSearch Vector Search Edition client.
*/
private Client client;
@Before
public void clientInit() throws Exception {
/*
Initialize the OpenSearch Vector Search Edition client.
*/
Config config = new Config();
// The instance name. You can find it in the upper-left corner of the Instance Details page. Example: ha-cn-i7*****605.
config.setInstanceId("ha-cn-i7*****605");
// The username. You can find it in the API Endpoint section on the Instance Details page.
config.setAccessUserName("username");
// The password. You can change it in the API Endpoint section on the Instance Details page.
config.setAccessPassWord("password");
// The API endpoint. You can find it in the API Endpoint section on the Instance Details page.
config.setEndpoint("ha-cn-i7*****605.public.ha.aliyuncs.com");
client = new Client(config);
}
@Test
public void add() throws Exception {
// The name of the table to which you want to push documents. The format is `_`.
String tableName = "<instance_id>_<table_name>";
// The primary key field for the documents to be pushed.
String pkField = "<field_pk>";
try {
// This is the outer structure for document pushes. It can contain one or more document operations.
ArrayList<Map<String, ?>> documents = new ArrayList<>();
// Add a document.
Map<String, Object> add2Document = new HashMap<>();
Map<String, Object> add2DocumentFields = new HashMap<>();
// Insert the document content. Key-value pairs must match.
// The field must be consistent with the pkField parameter.
add2DocumentFields.put("<field_pk>", "<field_pk_value>");
add2DocumentFields.put("<field_map_key_1>", "<field_map_value_1>");
add2DocumentFields.put("<field_map_key_2>", "<field_map_value_2>");
// OpenSearch Vector Search Edition supports multi-value attributes. In the index, set `"multi_value": true`.
ArrayList<Object> addDocumentMultiFields = new ArrayList<>();
addDocumentMultiFields.add("multi_value_1");
addDocumentMultiFields.add("multi_value_2");
add2DocumentFields.put("<multi_value_key>", addDocumentMultiFields);
// Add the document content to the add2Document structure.
add2Document.put("fields", add2DocumentFields);
// The command to add a new document is `add`.
add2Document.put("cmd", "add");
documents.add(add2Document);
// Push the data.
PushDocumentsRequest request = new PushDocumentsRequest();
// By default, the system checks whether the primary key field exists when data is pushed. To disable this check, set the request header `X-Opensearch-Validate-Data` to `false`.
// request.headers = new HashMap<>();
// request.headers.put("X-Opensearch-Validate-Data", "false");
request.setBody(documents);
PushDocumentsResponse response = client.pushDocuments(tableName, pkField, request);
String responseBody = response.getBody();
System.out.println("result: " + responseBody);
} catch (TeaException e) {
System.out.println(e.getCode());
System.out.println(e.getMessage());
Map<String, Object> exceptionData = e.getData();
System.out.println(com.aliyun.teautil.Common.toJSONString(exceptionData));
}
}
}
Python
from alibabacloud_ha3engine_vector.client import Client
from alibabacloud_ha3engine_vector.models import Config
from alibabacloud_ha3engine_vector import models
from Tea.exceptions import TeaException, RetryError
config = Config(
# The API endpoint. You can find it in the API Endpoint section on the Instance Details page.
endpoint="ha-cn-i7*****605.public.ha.aliyuncs.com",
# The username. You can find it in the API Endpoint section on the Instance Details page.
access_user_name="username",
# The password. You can change it in the API Endpoint section on the Instance Details page.
access_pass_word="password")
# Initialize the engine client.
client = Client(config)
def push():
# The name of the table to which you want to push documents. The format is `_`.
tableName = "<instance_id>_<table_name>";
try:
# Add a document.
# If the document already exists, it is deleted and then re-added.
# =====================================================
# Update the document content.
add2DocumentFields = {
"id": 1, # Primary key ID of the INT single-value type.
"name": "search", # STRING single-value type.
"str_arr": "a\x1Db\x1Dc\x1Dd" # STRING multi-value type.
}
# Add the document content to the add2Document structure.
add2Document = {
"fields": add2DocumentFields,
"cmd": "add" # The command to add a new document is `add`.
}
optionsHeaders = {}
# This is the outer structure for document pushes. It can contain one or more document operations.
documentArrayList = []
documentArrayList.append(add2Document)
pushDocumentsRequest = models.PushDocumentsRequest(optionsHeaders, documentArrayList)
# By default, the system checks whether the primary key field exists when data is pushed. To disable this check, set the request header `X-Opensearch-Validate-Data` to `false`.
# pushDocumentsRequest.headers = {"X-Opensearch-Validate-Data": "false"}
# The primary key field for the documents to be pushed.
pkField = "id"
# Send the request by using the default runtime parameters.
response = client.push_documents(tableName, pkField, pushDocumentsRequest)
print(response.body)
except TeaException as e:
print(f"send request with TeaException : {e}")
except RetryError as e:
print(f"send request with Connection Exception : {e}")
if __name__ == "__main__":
push()
Go
La démonstration encapsule dynamiquement les données du document dans des objets Map et appelle la méthode add() pour ajouter ces objets Map au cache. Ensuite, la démonstration appelle la méthode pushDocuments() pour soumettre les données du document contenues dans ces objets Map en une seule fois.
package main
import (
"fmt"
"github.com/alibabacloud-go/tea/tea"
ha3engine "github.com/aliyun/alibabacloud-ha3-go-sdk/client"
)
func main() {
// Create a client instance for requests.
config := &ha3engine.Config{
// The API endpoint. You can find it in the API Endpoint section on the Instance Details page.
Endpoint: tea.String("ha-cn-i7*****605.public.ha.aliyuncs.com"),
// The username. You can find it in the API Endpoint section on the Instance Details page.
AccessUserName: tea.String("username"),
// The password. You can change it in the API Endpoint section on the Instance Details page.
AccessPassWord: tea.String("password"),
}
// Initialize a client to send requests.
client, _clientErr := ha3engine.NewClient(config)
// If an error occurs during client initialization, _clientErr is returned and the error message is printed.
if _clientErr != nil {
fmt.Println(_clientErr)
return
}
docPush(client)
}
func docPush(client *ha3engine.Client) {
pushDocumentsRequestModel := &ha3engine.PushDocumentsRequest{}
// The name of the table to which you want to push documents. The format is `_`.
tableName := "<instance_id>_<table_name>"
// The primary key field for the documents to be pushed.
keyField := "<field_pk>"
// The vector field.
vector := [4]float32{1.2, 2.2, 3.2, 4.2}
filed := map[string]interface{}{
"fields": map[string]interface{}{
"id": 1,
"name": "test",
"vector": vector,
},
"cmd": tea.String("add"),
}
array := []map[string]interface{}{}
array = append(array, filed)
pushDocumentsRequestModel.SetBody(array)
// By default, the system checks whether the primary key field exists when data is pushed. To disable this check, set the request header `X-Opensearch-Validate-Data` to `false`.
//headers := map[string]*string{}
//headers["X-Opensearch-Validate-Data"] = tea.String("false")
//pushDocumentsRequestModel.SetHeaders(headers)
// Call the method to send the request.
response, _requestErr := client.PushDocuments(tea.String(tableName), tea.String(keyField), pushDocumentsRequestModel)
// If an error occurs while sending the request, _requestErr is returned and the error message is printed.
if _requestErr != nil {
fmt.Println(_requestErr)
return
}
// Print the content of the response.
fmt.Println(response)
}
Java in asynchronous mode
La démonstration encapsule dynamiquement les données du document dans des objets Map et appelle la méthode add() pour ajouter ces objets Map au cache. Ensuite, la démonstration appelle la méthode pushDocuments() pour soumettre les données du document contenues dans ces objets Map en une seule fois.
import com.aliyun.ha3engine.async.AsyncClient;
import com.aliyun.ha3engine.async.models.PushDocumentsRequest;
import com.aliyun.ha3engine.async.models.PushDocumentsResponse;
import com.aliyun.sdk.ha3engine.async.core.AsyncConfigInfoProvider;
import com.aliyun.tea.TeaException;
import darabonba.core.client.ClientOverrideConfiguration;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
/**
* @author alibaba
*/
public class AddDoc {
/**
* The OpenSearch Vector Search Edition client.
*/
private AsyncClient client;
@Before
public void clientInit() {
// Configure the username and password for the instance. You can find them in the API Endpoint section on the Instance Details page.
AsyncConfigInfoProvider provider = AsyncConfigInfoProvider.create("username", "password");
// Initialize the asynchronous client.
client = AsyncClient.builder()
.credentialsProvider(provider)
.overrideConfiguration(
ClientOverrideConfiguration.create()
.setEndpointOverride("ha-cn-i7*****605.public.ha.aliyuncs.com")
.setProtocol("http")
).build();
}
@Test
public void add() {
try {
// The name of the table for document pushes.
String tableName = "<table_name>";
// The primary key field for the documents to be pushed.
String pkField = "<field_pk>";
// This is the outer structure for document pushes. It can contain one or more document operations.
ArrayList<Map<String, ?>> documents = new ArrayList<>();
// Add a document.
Map<String, Object> add2Document = new HashMap<>();
Map<String, Object> add2DocumentFields = new HashMap<>();
// Update the document content. Key-value pairs must match.
// The field must be consistent with the pkField parameter.
add2DocumentFields.put("<field_pk>", "<field_pk_value>");
add2DocumentFields.put("<field_map_key_1>", "<field_map_value_1>");
add2DocumentFields.put("<field_map_key_2>", "<field_map_value_2>");
// OpenSearch Vector Search Edition supports multi-value attributes. In the index, set `"multi_value": true`.
ArrayList<Object> addDocumentMultiFields = new ArrayList<>();
addDocumentMultiFields.add("multi_value_1");
addDocumentMultiFields.add("multi_value_2");
add2DocumentFields.put("<multi_value_key>", addDocumentMultiFields);
// Add the document content to the add2Document structure.
add2Document.put("fields", add2DocumentFields);
// The command to add a new document is `add`.
add2Document.put("cmd", "add");
documents.add(add2Document);
// Push the data.
PushDocumentsRequest request = PushDocumentsRequest.builder().body(documents).build();
// By default, the system checks whether the primary key field exists when data is pushed. To disable this check, set the request header `X-Opensearch-Validate-Data` to `false`.
// request.getHeaderParameters().put("X-Opensearch-Validate-Data", "false");
CompletableFuture<PushDocumentsResponse> responseCompletableFuture = client.pushDocuments(tableName, pkField, request);
String responseBody = responseCompletableFuture.get().getBody();
System.out.println("result: " + responseBody);
} catch (ExecutionException | InterruptedException e) {
System.out.println(e.getMessage());
} catch (TeaException e) {
System.out.println(e.getCode());
System.out.println(e.getMessage());
Map<String, Object> exceptionData = e.getData();
System.out.println(com.aliyun.teautil.Common.toJSONString(exceptionData));
}
}
}
Si vous appelez la méthode add() pour télécharger des données, les nouvelles données utilisant la même clé primaire que les anciennes données écrasent ces dernières.
Supprimer un document
Java
import com.aliyun.ha3engine.vector.Client;
import com.aliyun.ha3engine.vector.models.*;
import com.aliyun.tea.TeaException;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
* @author alibaba
*/
public class PushDoc {
/**
* The OpenSearch Vector Search Edition client.
*/
private Client client;
@Before
public void clientInit() throws Exception {
/*
Initialize the OpenSearch Vector Search Edition client.
*/
Config config = new Config();
// The instance name. You can find it in the upper-left corner of the Instance Details page. Example: ha-cn-i7*****605.
config.setInstanceId("ha-cn-i7*****605");
// The username. You can find it in the API Endpoint section on the Instance Details page.
config.setAccessUserName("username");
// The password. You can change it in the Network Information section on the Instance Details page.
config.setAccessPassWord("password");
// The API endpoint. You can find it in the API Endpoint section on the Instance Details page.
config.setEndpoint("ha-cn-i7*****605.public.ha.aliyuncs.com");
client = new Client(config);
}
@Test
public void delete() throws Exception {
// The name of the table to which you want to push documents. The format is `_`.
String tableName = "<instance_id>_<table_name>";
// The primary key field for the documents to be pushed.
String pkField = "<field_pk>";
try {
// This is the outer structure for document pushes. It can contain one or more document operations.
ArrayList<Map<String, ?>> documents = new ArrayList<>();
// Delete a document.
Map<String, Object> delete2Document = new HashMap<>();
Map<String, Object> delete2DocumentFields = new HashMap<>();
// Insert the document content. Key-value pairs must match.
// The field must be consistent with the pkField parameter.
delete2DocumentFields.put("<field_pk>", "<field_pk_value>");
// Add the document content to the delete2Document structure.
delete2Document.put("fields", delete2DocumentFields);
// The command to delete a document is `delete`.
delete2Document.put("cmd", "delete");
documents.add(delete2Document);
// Push the data.
PushDocumentsRequest request = new PushDocumentsRequest();
request.setBody(documents);
PushDocumentsResponse response = client.pushDocuments(tableName, pkField, request);
String responseBody = response.getBody();
System.out.println("result: " + responseBody);
} catch (TeaException e) {
System.out.println(e.getCode());
System.out.println(e.getMessage());
Map<String, Object> exceptionData = e.getData();
System.out.println(com.aliyun.teautil.Common.toJSONString(exceptionData));
}
}
}
Python
from alibabacloud_ha3engine_vector.client import Client
from alibabacloud_ha3engine_vector.models import Config
from alibabacloud_ha3engine_vector import models
from Tea.exceptions import TeaException, RetryError
config = Config(
# The API endpoint. You can find it in the API Endpoint section on the Instance Details page.
endpoint="ha-cn-i7*****605.public.ha.aliyuncs.com",
# The username. You can find it in the API Endpoint section on the Instance Details page.
access_user_name="username",
# The password. You can change it in the API Endpoint section on the Instance Details page.
access_pass_word="password")
# Initialize the engine client.
client = Client(config)
def push():
# The name of the table to which you want to push documents. The format is `_`.
tableName = "<instance_id>_<table_name>";
try:
# This is the outer structure for document pushes. It can contain one or more document operations.
documentArrayList = []
# Delete a document.
# To delete a document, you must specify its primary key.
# If the index was built with multi-level hashing, you must specify the primary key for each hash level.
delete2DocumentFields = {
"id": 1 # Primary key ID of the INT single-value type.
}
delete2Document = {
"fields": delete2DocumentFields, # Add the document content to the delete2Document structure.
"cmd": "delete" # The command to delete a document is `delete`.
}
optionsHeaders = {}
documentArrayList.append(delete2Document)
pushDocumentsRequest = models.PushDocumentsRequest(
optionsHeaders, documentArrayList
)
# The primary key field for the documents to be pushed.
pkField = "id"
# Send the request by using the default runtime parameters.
response = client.push_documents(
tableName, pkField, pushDocumentsRequest
)
print(response)
except TeaException as e:
print(f"send request with TeaException : {e}")
except RetryError as e:
print(f"send request with Connection Exception : {e}")
if __name__ == "__main__":
push()
Go
package main
import (
"fmt"
"github.com/alibabacloud-go/tea/tea"
ha3engine "github.com/aliyun/alibabacloud-ha3-go-sdk/client"
)
func main() {
// Create a client instance for requests.
config := &ha3engine.Config{
// The API endpoint. You can find it in the API Endpoint section on the Instance Details page.
Endpoint: tea.String("ha-cn-i7*****605.public.ha.aliyuncs.com"),
// The username. You can find it in the API Endpoint section on the Instance Details page.
AccessUserName: tea.String("username"),
// The password. You can change it in the API Endpoint section on the Instance Details page.
AccessPassWord: tea.String("password"),
}
// Initialize a client to send requests.
client, _clientErr := ha3engine.NewClient(config)
// If an error occurs during client initialization, _clientErr is returned and the error message is printed.
if _clientErr != nil {
fmt.Println(_clientErr)
return
}
deleteDoc(client)
}
func deleteDoc(client *ha3engine.Client) {
pushDocumentsRequestModel := &ha3engine.PushDocumentsRequest{}
// The name of the table to which you want to push documents. The format is `_`.
tableName := "<instance_id>_<table_name>"
// The primary key field for the documents to be pushed.
keyField := "<field_pk>"
var array []map[string]interface{}
filed := map[string]interface{}{
"fields": map[string]interface{}{
"id": 2,
},
"cmd": tea.String("delete"),
}
array = append(array, filed)
pushDocumentsRequestModel.SetBody(array)
// Call the method to send the request.
response, _requestErr := client.PushDocuments(tea.String(tableName), tea.String(keyField), pushDocumentsRequestModel)
// If an error occurs while sending the request, _requestErr is returned and the error message is printed.
if _requestErr != nil {
fmt.Println(_requestErr)
return
}
// Print the content of the response.
fmt.Println(response)
}
Java in asynchronous mode
import com.aliyun.ha3engine.async.AsyncClient;
import com.aliyun.ha3engine.async.models.PushDocumentsRequest;
import com.aliyun.ha3engine.async.models.PushDocumentsResponse;
import com.aliyun.sdk.ha3engine.async.core.AsyncConfigInfoProvider;
import com.aliyun.tea.TeaException;
import darabonba.core.client.ClientOverrideConfiguration;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
/**
* @author alibaba
*/
public class DeleteDoc {
/**
* The OpenSearch Vector Search Edition client.
*/
private AsyncClient client;
@Before
public void clientInit() {
// Configure the username and password for the instance. You can find them in the API Endpoint section on the Instance Details page.
AsyncConfigInfoProvider provider = AsyncConfigInfoProvider.create("username", "password");
// Initialize the asynchronous client.
client = AsyncClient.builder()
.credentialsProvider(provider)
.overrideConfiguration(
ClientOverrideConfiguration.create()
.setEndpointOverride("ha-cn-i7*****605.public.ha.aliyuncs.com")
.setProtocol("http")
).build();
}
@Test
public void delete() throws Exception {
try {
// The name of the table to which you want to push documents. The format is `_`.
String tableName = "<instance_id>_<table_name>";
// The primary key field for the documents to be pushed.
String pkField = "<field_pk>";
// This is the outer structure for document pushes. It can contain one or more document operations.
ArrayList<Map<String, ?>> documents = new ArrayList<>();
// Delete a document.
Map<String, Object> deleteDocument = new HashMap<>();
Map<String, Object> deleteDocumentFields = new HashMap<>();
// Update the document content. Key-value pairs must match.
// The field must be consistent with the pkField parameter.
deleteDocumentFields.put("<field_pk>", "<field_pk_value>");
// Add the document content to the deleteDocument structure.
deleteDocument.put("fields", deleteDocumentFields);
// The command to delete a document is `delete`.
deleteDocument.put("cmd", "delete");
documents.add(deleteDocument);
// Push the data.
PushDocumentsRequest request = PushDocumentsRequest.builder().body(documents).build();
CompletableFuture<PushDocumentsResponse> responseCompletableFuture = client.pushDocuments(tableName, pkField, request);
String responseBody = responseCompletableFuture.get().getBody();
System.out.println("result: " + responseBody);
} catch (ExecutionException | InterruptedException e) {
System.out.println(e.getMessage());
} catch (TeaException e) {
System.out.println(e.getCode());
System.out.println(e.getMessage());
Map<String, Object> exceptionData = e.getData();
System.out.println(com.aliyun.teautil.Common.toJSONString(exceptionData));
}
}
}
Informations complémentaires
Pour obtenir des informations sur la réponse à une requête, consultez la rubrique Mettre à jour les données.
N'utilisez pas la commande
go get github.com/aliyun/alibabacloud-ha3-go-sdkpour récupérer la dépendance Git. Vous devez spécifier une version. En effet, les tags pour OpenSearch Vector Search Edition et OpenSearch Retrieval Engine Edition se trouvent dans le même référentiel GitHub. Vous devez récupérer la dépendance correspondant à la version de votre instance.