All Products
Search
Document Center

OpenSearch:Query data

Last Updated:Mar 21, 2025

This topic describes how to use OpenSearch Vector Search Edition SDKs for Java in asynchronous mode, Java, Python, and Go to perform vector-based queries, prediction-based queries, and queries by using filter conditions.

Dependencies

Java

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-sdk-ha3engine-vector</artifactId>
    <version>1.1.11</version>
</dependency>

Python

# Requires: Python >=3.6
pip install alibabacloud_ha3engine_vector

Go

go get github.com/aliyun/alibabacloud-ha3-go-sdk@v1.1.12-vector

Java in asynchronous mode

<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-sdk-ha3engine-async</artifactId>
  <version>1.1.4</version>
</dependency>

Parameters

You must configure the following parameters in SDKs for Java and Python: endpoint, instance_id, access_user_name, and access_pass_word.

  • endpoint: the internal or public endpoint.

You can view the endpoints in the Network Information and API Endpoint sections of the Instance Details page.

image.png

After you turn on Public Access, you can access the OpenSearch Vector Search Edition instance by using a public endpoint on your on-premises machine. A public endpoint contains the word "public". You can configure an IP address whitelist for access to the instance. For more information, see Network Information section in the "View instance details" topic.

If you use an Elastic Compute Service (ECS) instance to access the OpenSearch Vector Search Edition instance, you can specify the same vSwitch as that of the ECS instance to access the OpenSearch Vector Search Edition instance by using the API endpoint.

  • instance_id: the ID of the OpenSearch Vector Search Edition instance.

image.png

  • access_user_name: the username.

  • access_pass_word: the password.

You can view the username and password in the API Endpoint section of the Instance Details page. The password is specified when you purchase the instance and can be modified.

image.png

Vector-based query

Simple query

Java

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

import com.aliyun.ha3engine.vector.Client;
import com.aliyun.ha3engine.vector.models.Config;
import com.aliyun.ha3engine.vector.models.QueryRequest;
import com.aliyun.ha3engine.vector.models.SearchResponse;
import com.aliyun.tea.TeaException;

import org.junit.Before;
import org.junit.Test;

public class Demo {

    /**
     * The engine client of the OpenSearch Vector Search Edition instance.
     */
    private Client client;

    @Before
    public void clientInit() throws Exception {
        /*
          Initialize the engine client.
         */
        Config config = new Config();

        // The name of the instance. You can view the instance name 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 view the username in the API Endpoint section of the Instance Details page.
        config.setAccessUserName("username");
        // The password. You can modify the password in the API Endpoint section of the Instance Details page.
        config.setAccessPassWord("password");
        // The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
        config.setEndpoint("ha-cn-i7*****605.public.ha.aliyuncs.com");

        client = new Client(config);
    }

    @Test
    public void query() throws Exception {
        try {
            QueryRequest request = new QueryRequest();
            request.setTableName("gist"); // Required. The name of the table to be queried.
            request.setVector(Arrays.asList(0.183762561f, 0.738372617f)); // Required. The vector data to be queried.
            request.setTopK(100); // Optional. The number of results to be returned.
            request.setIncludeVector(true); // Optional. Specifies whether to return the vector information in documents.
            
            SearchResponse searchResponse = client.query(request);
            System.out.println("Result:\n" + searchResponse.getBody());
        } 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.models import QueryRequest

config = Config(
    # The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
    endpoint="http://ha-cn-i7*****605.public.ha.aliyuncs.com",
    # The username. You can view the username in the API Endpoint section of the Instance Details page.
    access_user_name="username",
    # The password. You can modify the password in the API Endpoint section of the Instance Details page.
    access_pass_word="password")
client = Client(config)

request = QueryRequest(table_name="gist",
                    	vector=[0.1, 0.2, 0.3],
                        include_vector=True,
                        top_k=10)
result = client.query(request)

Go

package main

import (
    "fmt"
    "github.com/alibabacloud-go/tea-utils/v2/service"
    "github.com/alibabacloud-go/tea/tea"
    ha3engine "github.com/aliyun/alibabacloud-ha3-go-sdk/client"
)

func main() {

    // Create a client instance for sending requests.
    config := &ha3engine.Config{
        // The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
        Endpoint: tea.String("ha-cn-i7*****605.public.ha.aliyuncs.com"),
        // The username. You can view the username in the API Endpoint section of the Instance Details page.
        AccessUserName: tea.String("username"),
        // The password. You can modify the password in the API Endpoint section of the Instance Details page.
        AccessPassWord: tea.String("password"),
        RuntimeOptions: &service.RuntimeOptions{
            KeepAlive:    tea.Bool(true),
            MaxIdleConns: tea.Int(1000),
            ReadTimeout:  tea.Int(1000),
        },
    }

    // Initialize a client for sending requests.
    client, _clientErr := ha3engine.NewClient(config)

    // If an error occurs when the system creates the client, _clientErr and an error message are returned.
    if _clientErr != nil {
        fmt.Println(_clientErr)
        return
    }

    query(client)
}

/**
 * Simple query
 */
func query(client *ha3engine.Client) {

    searchRequestModel := &ha3engine.QueryRequest{}
    searchRequestModel.SetTableName("test1")

    // Create an array of 32-bit floating-point numbers.
    ids := make([]*float32, 4)

    // Assign values to the elements of the array.
    ids[0] = tea.Float32(0.1)
    ids[1] = tea.Float32(0.2)
    ids[2] = tea.Float32(0.3)
    ids[3] = tea.Float32(0.4)

    searchRequestModel.SetVector(ids)

    response, _requestErr := client.Query(searchRequestModel)

    // If an error occurs when the system sends the request, _requestErr and an error message are returned.
    if _requestErr != nil {
        fmt.Println(_requestErr)
        return
    }

    // Display the response if no error occurs.
    fmt.Println(response)
}

Java in asynchronous mode

import com.aliyun.ha3engine.async.AsyncClient;
import com.aliyun.ha3engine.async.models.QueryRequest;
import com.aliyun.ha3engine.async.models.SearchResponse;
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.Arrays;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author alibaba
 */
public class SimpleQuery {

    /**
     * The engine client of the OpenSearch Vector Search Edition instance.
     */
    private AsyncClient client;

    @Before
    public void clientInit() {
        // The username and password that are used to access the instance. You can view the username and password in the API Endpoint section of 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 simpleQuery() {
        try {
            QueryRequest queryRequest = QueryRequest.builder()
                    .tableName("table_name") // The name of the table to be queried.
                    .indexName("index_name") // The name of the index to be queried.
                    .vector(Arrays.asList(0.0001575F, 0.00682848F)) // The vector data to be queried.
                    .topK(10) // The number of results to be returned.
                    .includeVector(true) // Specifies whether to return the vector information in documents.
                    .build();
            
            CompletableFuture<SearchResponse> responseCompletableFuture = client.query(queryRequest);
            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));
        }
    }
}

Query based on multiple vectors

You can specify multiple vectors to perform a vector-based query. The system merges and sorts the retrieved results, and returns the top K results.

Java

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

import com.aliyun.ha3engine.vector.Client;
import com.aliyun.ha3engine.vector.models.Config;
import com.aliyun.ha3engine.vector.models.QueryRequest;
import com.aliyun.ha3engine.vector.models.SearchResponse;
import com.aliyun.tea.TeaException;

import org.junit.Before;
import org.junit.Test;

public class Demo {

    /**
     * The engine client of the OpenSearch Vector Search Edition instance.
     */
    private Client client;

    @Before
    public void clientInit() throws Exception {
        /*
          Initialize the engine client.
         */
        Config config = new Config();

        // The name of the instance. You can view the instance name 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 view the username in the API Endpoint section of the Instance Details page.
        config.setAccessUserName("username");
        // The password. You can modify the password in the API Endpoint section of the Instance Details page.
        config.setAccessPassWord("password");
        // The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
        config.setEndpoint("ha-cn-i7*****605.public.ha.aliyuncs.com");

        client = new Client(config);
    }

    @Test
    public void query() throws Exception {
        try {
            QueryRequest request = new QueryRequest();
            request.setTableName("gist"); // Required. The name of the table to be queried.
            request.setVector(Arrays.asList(0.1f, 0.2f, 0.3f, 0.4f)); // Required. The vector data to be queried.
            request.setVectorCount(2); // Optional. The number of vectors contained in the vector field.
            request.setTopK(100); // Optional. The number of results to be returned.
            request.setIncludeVector(true); // Optional. Specifies whether to return the vector information in documents.
            request.setOutputFields(new ArrayList<String>()); // Optional. The fields to be returned.
            request.setFilter("a>10"); // Optional. The filter expression.
            
            SearchResponse searchResponse = client.query(request);
            System.out.println("Result:\n" + searchResponse.getBody());
        } 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.models import QueryRequest

config = Config(
    # The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
    endpoint="http://ha-cn-i7*****605.public.ha.aliyuncs.com",
    # The username. You can view the username in the API Endpoint section of the Instance Details page.
    access_user_name="username",
    # The password. You can modify the password in the API Endpoint section of the Instance Details page.
    access_pass_word="password")
client = Client(config)

request = QueryRequest(table_name="t1",
                        vector=[0.1, 0.2, 0.3, 0.4],
                        vector_count=2,
                        include_vector=True,
                        filter="a > 10",
                        output_fields=["a", "b"]
                        top_k=10)
result = client.query(request)

Go

package main

import (
	"fmt"
	"github.com/alibabacloud-go/tea-utils/v2/service"
	"github.com/alibabacloud-go/tea/tea"
	ha3engine "github.com/aliyun/alibabacloud-ha3-go-sdk/client"
	"sync"
)

func main() {

	// Create a client instance for sending requests.
	config := &ha3engine.Config{
		// The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
		Endpoint: tea.String("ha-cn-i7*****605.public.ha.aliyuncs.com"),
		// The username. You can view the username in the API Endpoint section of the Instance Details page.
		AccessUserName: tea.String("username"),
		// The password. You can modify the password in the API Endpoint section of the Instance Details page.
		AccessPassWord: tea.String("password"),
		RuntimeOptions: &service.RuntimeOptions{
			KeepAlive:    tea.Bool(true),
			MaxIdleConns: tea.Int(1000),
			ReadTimeout:  tea.Int(1000),
		},
	}

	// Initialize a client for sending requests.
	client, _clientErr := ha3engine.NewClient(config)

	// If an error occurs when the system creates the client, _clientErr and an error message are returned.
	if _clientErr != nil {
		fmt.Println(_clientErr)
		return
	}

	multiVectorQuery(client)
}

func multiVectorQuery(client *ha3engine.Client) {

	queryRequest := &ha3engine.QueryRequest{}
	queryRequest.SetTableName("test1")

	// Create an array of 32-bit floating-point numbers.
	ids := make([]*float32, 8)

	// Assign values to the elements of the array.
	ids[0] = tea.Float32(0.1)
	ids[1] = tea.Float32(0.2)
	ids[2] = tea.Float32(0.3)
	ids[3] = tea.Float32(0.4)
	ids[4] = tea.Float32(0.1)
	ids[5] = tea.Float32(0.2)
	ids[6] = tea.Float32(0.3)
	ids[7] = tea.Float32(0.4)

	queryRequest.SetVector(ids)
	queryRequest.SetVectorCount(2)

	response, _requestErr := client.Query(queryRequest)

	// If an error occurs when the system sends the request, _requestErr and an error message are returned.
	if _requestErr != nil {
		fmt.Println(_requestErr)
		return
	}

	// Display the response if no error occurs.
	fmt.Println(response)
}

Java in asynchronous mode

import com.aliyun.ha3engine.async.AsyncClient;
import com.aliyun.ha3engine.async.models.QueryRequest;
import com.aliyun.ha3engine.async.models.SearchResponse;
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.Arrays;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * Query based on multiple vectors
 */
public class MultiVectorQuery {

    /**
     * The engine client of the OpenSearch Vector Search Edition instance.
     */
    private AsyncClient client;

    @Before
    public void clientInit() {
        // The username and password that are used to access the instance. You can view the username and password in the API Endpoint section of 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 multiVectorQuery() {
        try {
            QueryRequest queryRequest = QueryRequest.builder()
                    .tableName("test1") // The name of the table to be queried.
                    .indexName("vector") // The name of the index to be queried.
                    .vector(Arrays.asList(0.62498F, 0.886408F, 0.586114F, 0.043189F,0.62498F, 0.886408F, 0.586114F, 0.043189F)) // The vector data to be queried.
                    .vectorCount(2)// Optional. The number of vectors contained in the vector field.
                    .topK(10) // The number of results to be returned.
                    .includeVector(false) // Specifies whether to return the vector information in documents.
                    .build();

            CompletableFuture<SearchResponse> responseCompletableFuture = client.query(queryRequest);
            String responseBody = responseCompletableFuture.get().getBody();
            System.out.println("query 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));
        }
    }
}

Namespace-based query

OpenSearch Vector Search Edition allows you to partition indexes by using namespaces. After you configure namespaces for vector indexes, you can specify a namespace to query data. This way, you can query different subsets of indexes by sending different query requests.

Note: If you configure a namespace for a vector index, you must specify the namespace when you query data.

Java

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

import com.aliyun.ha3engine.vector.Client;
import com.aliyun.ha3engine.vector.models.Config;
import com.aliyun.ha3engine.vector.models.QueryRequest;
import com.aliyun.ha3engine.vector.models.SearchResponse;
import com.aliyun.tea.TeaException;

import org.junit.Before;
import org.junit.Test;

public class Demo {

    /**
     * The engine client of the OpenSearch Vector Search Edition instance.
     */
    private Client client;

    @Before
    public void clientInit() throws Exception {
        /*
          Initialize the engine client.
         */
        Config config = new Config();

        // The name of the instance. You can view the instance name 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 view the username in the API Endpoint section of the Instance Details page.
        config.setAccessUserName("username");
        // The password. You can modify the password in the API Endpoint section of the Instance Details page.
        config.setAccessPassWord("password");
        // The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
        config.setEndpoint("ha-cn-i7*****605.public.ha.aliyuncs.com");

        client = new Client(config);
    }

    @Test
    public void query() throws Exception {
        try {
            QueryRequest request = new QueryRequest();
            request.setTableName("gist"); // Required. The name of the table to be queried.
            request.setVector(Arrays.asList(0.183762561f, 0.738372617f)); // Required. The vector data to be queried.
            request.setNamespace("namespace"); // Optional. The namespace of the vector to be queried.
            request.setTopK(100); // Optional. The number of results to be returned.
            request.setIncludeVector(true); // Optional. Specifies whether to return the vector information in documents.
            
            SearchResponse searchResponse = client.query(request);
            System.out.println("Result:\n" + searchResponse.getBody());
        } 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.models import QueryRequest

config = Config(
    # The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
    endpoint="http://ha-cn-i7*****605.public.ha.aliyuncs.com",
    # The username. You can view the username in the API Endpoint section of the Instance Details page.
    access_user_name="username",
    # The password. You can modify the password in the API Endpoint section of the Instance Details page.
    access_pass_word="password")
client = Client(config)

request = QueryRequest(table_name="t1",
                        namespace="space_b",
                        vector=[0.1, 0.2],
                        include_vector=True,
                        top_k=10)
result = client.query(request)

Go

package main

import (
	"fmt"
	"github.com/alibabacloud-go/tea-utils/v2/service"
	"github.com/alibabacloud-go/tea/tea"
	ha3engine "github.com/aliyun/alibabacloud-ha3-go-sdk/client"
	"sync"
)

func main() {

	// Create a client instance for sending requests.
	config := &ha3engine.Config{
		// The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
		Endpoint: tea.String("ha-cn-i7*****605.public.ha.aliyuncs.com"),
		// The username. You can view the username in the API Endpoint section of the Instance Details page.
		AccessUserName: tea.String("username"),
		// The password. You can modify the password in the API Endpoint section of the Instance Details page.
		AccessPassWord: tea.String("password"),
		RuntimeOptions: &service.RuntimeOptions{
			KeepAlive:    tea.Bool(true),
			MaxIdleConns: tea.Int(1000),
			ReadTimeout:  tea.Int(1000),
		},
	}

	// Initialize a client for sending requests.
	client, _clientErr := ha3engine.NewClient(config)

	// If an error occurs when the system creates the client, _clientErr and an error message are returned.
	if _clientErr != nil {
		fmt.Println(_clientErr)
		return
	}

	nameSpaceQuery(client)
}

func nameSpaceQuery(client *ha3engine.Client) {

	searchRequestModel := &ha3engine.QueryRequest{}
	searchRequestModel.SetTableName("test1")

	// Create an array of 32-bit floating-point numbers.
	ids := make([]*float32, 4)

	// Assign values to the elements of the array.
	ids[0] = tea.Float32(0.1)
	ids[1] = tea.Float32(0.2)
	ids[2] = tea.Float32(0.3)
	ids[3] = tea.Float32(0.4)

	searchRequestModel.SetVector(ids)
	searchRequestModel.SetNamespace("1")

	response, _requestErr := client.Query(searchRequestModel)

	// If an error occurs when the system sends the request, _requestErr and an error message are returned.
	if _requestErr != nil {
		fmt.Println(_requestErr)
		return
	}

	// Display the response if no error occurs.
	fmt.Println(response)
}

Java in asynchronous mode

import com.aliyun.ha3engine.async.AsyncClient;
import com.aliyun.ha3engine.async.models.QueryRequest;
import com.aliyun.ha3engine.async.models.SearchResponse;
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.Arrays;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class NameSpaceQuery {
    /**
     * The engine client of the OpenSearch Vector Search Edition instance.
     */
    private AsyncClient client;

    @Before
    public void clientInit() {
        // The username and password that are used to access the instance. You can view the username and password in the API Endpoint section of 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 nameSpaceQuery() {
        try {
            QueryRequest queryRequest = QueryRequest.builder()
                    .tableName("test1") // The name of the table to be queried.
                    .indexName("vector") // The name of the index to be queried.
                    .namespace("1")
                    .vector(Arrays.asList(0.62498F, 0.886408F, 0.586114F, 0.043189F)) // The vector data to be queried.
                    .topK(10) // The number of results to be returned.
                    .includeVector(false) // Specifies whether to return the vector information in documents.
                    .build();

            CompletableFuture<SearchResponse> responseCompletableFuture = client.query(queryRequest);
            String responseBody = responseCompletableFuture.get().getBody();
            System.out.println("query 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));
        }
    }
}

Query based on multiple namespaces

If you configure namespaces for vector indexes, you can query data in multiple namespaces.

Java

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

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;

public class Demo {

    /**
     * The engine client of the OpenSearch Vector Search Edition instance.
     */
    private Client client;

    @Before
    public void clientInit() throws Exception {
        /*
          Initialize the engine client.
         */
        Config config = new Config();

        // The name of the instance. You can view the instance name 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 view the username in the API Endpoint section of the Instance Details page.
        config.setAccessUserName("username");
        // The password. You can modify the password in the API Endpoint section of the Instance Details page.
        config.setAccessPassWord("password");
        // The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
        config.setEndpoint("ha-cn-i7*****605.public.ha.aliyuncs.com");

        client = new Client(config);
    }

    @Test
    public void multiQuery() throws Exception {
        try {
            MultiQueryRequest request = new MultiQueryRequest();
            request.setTableName("gist");
            request.setTopK(3);
            request.setIncludeVector(true);

            QueryRequest query1 = new QueryRequest();
            query1.setVector(Arrays.asList(0.1f, 0.2f, 0.3f));
            query1.setNamespace("space_a");

            QueryRequest query2 = new QueryRequest();
            query2.setVector(Arrays.asList(0.1f, 0.2f, 0.3f));
            query2.setNamespace("space_b");

            request.setQueries(Arrays.asList(query1, query2));

            SearchResponse searchResponse = client.multiQuery(request);
            System.out.println("Result:\n" + searchResponse.getBody());
        } 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.models import QueryRequest
from alibabacloud_ha3engine_vector.models import MultiQueryRequest

config = Config(
    # The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
    endpoint="http://ha-cn-i7*****605.public.ha.aliyuncs.com",
    # The username. You can view the username in the API Endpoint section of the Instance Details page.
    access_user_name="username",
    # The password. You can modify the password in the API Endpoint section of the Instance Details page.
    access_pass_word="password")
client = Client(config)

q1 = QueryRequest(vector=[0.1, 0.2, 0.3], namespace="space_a")
q2 = QueryRequest(vector=[0.4, 0.5, 0.6], namespace="space_b")
request = MultiQueryRequest(table_name = "gist", 
                            queries=[q1, q2], 
                            top_k=3, 
                            include_vector=True)
result = client.multi_query(request)

Go

package main

import (
	"fmt"
	"github.com/alibabacloud-go/tea-utils/v2/service"
	"github.com/alibabacloud-go/tea/tea"
	ha3engine "github.com/aliyun/alibabacloud-ha3-go-sdk/client"
	"sync"
)

func main() {

	// Create a client instance for sending requests.
	config := &ha3engine.Config{
		// The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
		Endpoint: tea.String("ha-cn-i7*****605.public.ha.aliyuncs.com"),
		// The username. You can view the username in the API Endpoint section of the Instance Details page.
		AccessUserName: tea.String("username"),
		// The password. You can modify the password in the API Endpoint section of the Instance Details page.
		AccessPassWord: tea.String("password"),
		RuntimeOptions: &service.RuntimeOptions{
			KeepAlive:    tea.Bool(true),
			MaxIdleConns: tea.Int(1000),
			ReadTimeout:  tea.Int(1000),
		},
	}

	// Initialize a client for sending requests.
	client, _clientErr := ha3engine.NewClient(config)

	// If an error occurs when the system creates the client, _clientErr and an error message are returned.
	if _clientErr != nil {
		fmt.Println(_clientErr)
		return
	}

	multiNameSpaceQuery(client)
}

func multiNameSpaceQuery(client *ha3engine.Client) {

	query1 := &ha3engine.QueryRequest{}
	// Create an array of 32-bit floating-point numbers.
	array := make([]*float32, 4)

	// Allocate a memory address to each value of the array and initialize the values.
	value1 := float32(1.23)
	value2 := float32(4.56)
	value3 := float32(7.89)
	value4 := float32(7.89)

	// Assign values to the elements of the array.
	array[0] = &value1
	array[1] = &value2
	array[2] = &value3
	array[3] = &value4

	query1.SetVector(array)
	query1.SetNamespace("space_a")

	query2 := &ha3engine.QueryRequest{}
	query2.SetVector(array)
	query2.SetNamespace("space_b")

	request := &ha3engine.MultiQueryRequest{}

	request.SetTableName("test2")

	querys := make([]*ha3engine.QueryRequest, 2)
	querys[0] = query1
	querys[1] = query2
	request.SetQueries(querys)
	request.SetTopK(10)
	request.SetIncludeVector(false)
	request.SetOrder("DESC")

	response, _requestErr := client.MultiQuery(request)

	// If an error occurs when the system sends the request, _requestErr and an error message are returned.
	if _requestErr != nil {
		fmt.Println(_requestErr)
		return
	}

	// Display the response if no error occurs.
	fmt.Println(response)
}

Java in asynchronous mode

import com.aliyun.ha3engine.async.AsyncClient;
import com.aliyun.ha3engine.async.models.MultiQueryRequest;
import com.aliyun.ha3engine.async.models.QueryRequest;
import com.aliyun.ha3engine.async.models.SearchResponse;
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.Arrays;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class MultiNameSpaceQuery {
    /**
     * The engine client of the OpenSearch Vector Search Edition instance.
     */
    private AsyncClient client;

    @Before
    public void clientInit() {
        // The username and password that are used to access the instance. You can view the username and password in the API Endpoint section of 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 multiNameSpaceQuery() {
        try {
            QueryRequest query1 = QueryRequest.builder()
                    .vector(Arrays.asList(0.62498F, 0.886408F, 0.586114F, 0.043189F))
                    .namespace("space_a")
                    .build();
            QueryRequest query2 = QueryRequest.builder()
                    .vector(Arrays.asList(0.62498F, 0.886408F, 0.586114F, 0.043189F))
                    .namespace("space_b")
                    .build();
            MultiQueryRequest request = MultiQueryRequest.builder()
                    .tableName("test1")// The name of the table to be queried.
                    .topK(3)
                    .includeVector(false)// Specifies whether to return the vector information in documents.
                    .queries(Arrays.asList(query1, query2))// The filter conditions of the namespaces to be queried.
                    .build();

            CompletableFuture<SearchResponse> responseCompletableFuture = client.multiQuery(request);
            String responseBody = responseCompletableFuture.get().getBody();

            System.out.println("multiQuery 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));
        }
    }
}

Query by using filter conditions

You can use filter expressions to specify filter conditions for data queries.

Java

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

import com.aliyun.ha3engine.vector.Client;
import com.aliyun.ha3engine.vector.models.Config;
import com.aliyun.ha3engine.vector.models.QueryRequest;
import com.aliyun.ha3engine.vector.models.SearchResponse;
import com.aliyun.tea.TeaException;

import org.junit.Before;
import org.junit.Test;

public class Demo {

    /**
     * The engine client of the OpenSearch Vector Search Edition instance.
     */
    private Client client;

    @Before
    public void clientInit() throws Exception {
        /*
          Initialize the engine client.
         */
        Config config = new Config();

        // The name of the instance. You can view the instance name 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 view the username in the API Endpoint section of the Instance Details page.
        config.setAccessUserName("username");
        // The password. You can modify the password in the API Endpoint section of the Instance Details page.
        config.setAccessPassWord("password");
        // The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
        config.setEndpoint("ha-cn-i7*****605.public.ha.aliyuncs.com");

        client = new Client(config);
    }

    @Test
    public void query() throws Exception {
        try {
            QueryRequest request = new QueryRequest();
            request.setTableName("gist"); // Required. The name of the table to be queried.
            request.setVector(Arrays.asList(0.183762561f, 0.738372617f)); // Required. The vector data to be queried.
            request.setTopK(100); // Optional. The number of results to be returned.
            request.setIncludeVector(true); // Optional. Specifies whether to return the vector information in documents.
            request.setOutputFields(new ArrayList<String>()); // Optional. The fields to be returned.
            request.setFilter("a>10"); // Optional. The filter expression.
            
            SearchResponse searchResponse = client.query(request);
            System.out.println("Result:\n" + searchResponse.getBody());
        } 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.models import QueryRequest

config = Config(
    # The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
    endpoint="http://ha-cn-i7*****605.public.ha.aliyuncs.com",
    # The username. You can view the username in the API Endpoint section of the Instance Details page.
    access_user_name="username",
    # The password. You can modify the password in the API Endpoint section of the Instance Details page.
    access_pass_word="password")
client = Client(config)

request = QueryRequest(table_name="t1",
                        vector=[0.1, 0.2],
                        include_vector=True,
                        filter="a > 10",
                        output_fields=["a", "b"]
                        top_k=10)
result = client.query(request)

Go

package main

import (
	"fmt"
	"github.com/alibabacloud-go/tea-utils/v2/service"
	"github.com/alibabacloud-go/tea/tea"
	ha3engine "github.com/aliyun/alibabacloud-ha3-go-sdk/client"
	"sync"
)

func main() {

	// Create a client instance for sending requests.
	config := &ha3engine.Config{
		// The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
		Endpoint: tea.String("ha-cn-i7*****605.public.ha.aliyuncs.com"),
		// The username. You can view the username in the API Endpoint section of the Instance Details page.
		AccessUserName: tea.String("username"),
		// The password. You can modify the password in the API Endpoint section of the Instance Details page.
		AccessPassWord: tea.String("password"),
		RuntimeOptions: &service.RuntimeOptions{
			KeepAlive:    tea.Bool(true),
			MaxIdleConns: tea.Int(1000),
			ReadTimeout:  tea.Int(1000),
		},
	}

	// Initialize a client for sending requests.
	client, _clientErr := ha3engine.NewClient(config)

	// If an error occurs when the system creates the client, _clientErr and an error message are returned.
	if _clientErr != nil {
		fmt.Println(_clientErr)
		return
	}

	filterQuery(client)
}

func filterQuery(client *ha3engine.Client) {

	searchRequestModel := &ha3engine.QueryRequest{}
	searchRequestModel.SetTableName("test1")

	// Create an array of 32-bit floating-point numbers.
	ids := make([]*float32, 4)

	// Assign values to the elements of the array.
	ids[0] = tea.Float32(0.1)
	ids[1] = tea.Float32(0.2)
	ids[2] = tea.Float32(0.3)
	ids[3] = tea.Float32(0.4)

	searchRequestModel.SetVector(ids)
	searchRequestModel.SetFilter("count>25")

	response, _requestErr := client.Query(searchRequestModel)

	// If an error occurs when the system sends the request, _requestErr and an error message are returned.
	if _requestErr != nil {
		fmt.Println(_requestErr)
		return
	}

	// Display the response if no error occurs.
	fmt.Println(response)
}

Java in asynchronous mode

import com.aliyun.ha3engine.async.AsyncClient;
import com.aliyun.ha3engine.async.models.QueryRequest;
import com.aliyun.ha3engine.async.models.SearchResponse;
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.Arrays;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class FilterQuery {
    /**
     * The engine client of the OpenSearch Vector Search Edition instance.
     */
    private AsyncClient client;

    @Before
    public void clientInit() {
        // The username and password that are used to access the instance. You can view the username and password in the API Endpoint section of 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 filterQuery() {
        try {
            QueryRequest queryRequest = QueryRequest.builder()
                    .tableName("test1") // The name of the table to be queried.
                    .indexName("vector") // The name of the index to be queried.
                    .vector(Arrays.asList(0.62498F, 0.886408F, 0.586114F, 0.043189F)) // The vector data to be queried.
                    .topK(10) // The number of results to be returned.
                    .filter("count>25")// The filter condition.
                    .includeVector(false) // Specifies whether to return the vector information in documents.
                    .build();

            CompletableFuture<SearchResponse> responseCompletableFuture = client.query(queryRequest);
            String responseBody = responseCompletableFuture.get().getBody();

            System.out.println("multiQuery 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));
        }
    }
}

For more information about how to use filter expressions, see Filter expression.

Primary key-based query

Java

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

import com.aliyun.ha3engine.vector.Client;
import com.aliyun.ha3engine.vector.models.Config;
import com.aliyun.ha3engine.vector.models.FetchRequest;
import com.aliyun.ha3engine.vector.models.SearchResponse;
import com.aliyun.tea.TeaException;

import org.junit.Before;
import org.junit.Test;

public class Demo {

    /**
     * The engine client of the OpenSearch Vector Search Edition instance.
     */
    private Client client;

    @Before
    public void clientInit() throws Exception {
        /*
          Initialize the engine client.
         */
        Config config = new Config();

        // The name of the instance. You can view the instance name 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 view the username in the API Endpoint section of the Instance Details page.
        config.setAccessUserName("username");
        // The password. You can modify the password in the API Endpoint section of the Instance Details page.
        config.setAccessPassWord("password");
        // The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
        config.setEndpoint("ha-cn-i7*****605.public.ha.aliyuncs.com");

        client = new Client(config);
    }

    @Test
    public void fetch() throws Exception {
        try {
            FetchRequest request = new FetchRequest();
            request.setTableName("gist");
            request.setIds(Arrays.asList("1", "2"));
            SearchResponse searchResponse = client.fetch(request);
            System.out.println("Result:\n" + searchResponse.getBody());
        } 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.models import FetchRequest

config = Config(
    # The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
    endpoint="http://ha-cn-i7*****605.public.ha.aliyuncs.com",
    # The username. You can view the username in the API Endpoint section of the Instance Details page.
    access_user_name="username",
    # The password. You can modify the password in the API Endpoint section of the Instance Details page.
    access_pass_word="password")
client = Client(config)

request = FetchRequest(table_name="gist", ids=["1", "2"])
result = client.fetch(request)

Go

package main

import (
	"fmt"
	"github.com/alibabacloud-go/tea-utils/v2/service"
	"github.com/alibabacloud-go/tea/tea"
	ha3engine "github.com/aliyun/alibabacloud-ha3-go-sdk/client"
)

func main() {

	// Create a client instance for sending requests.
	config := &ha3engine.Config{
		// The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
		Endpoint: tea.String("ha-cn-i7*****605.public.ha.aliyuncs.com"),
		// The username. You can view the username in the API Endpoint section of the Instance Details page.
		AccessUserName: tea.String("username"),
		// The password. You can modify the password in the API Endpoint section of the Instance Details page.
		AccessPassWord: tea.String("password"),
		RuntimeOptions: &service.RuntimeOptions{
			KeepAlive:    tea.Bool(true),
			MaxIdleConns: tea.Int(1000),
			ReadTimeout:  tea.Int(1000),
		},
	}

	// Initialize a client for sending requests.
	client, _clientErr := ha3engine.NewClient(config)

	// If an error occurs when the system creates the client, _clientErr and an error message are returned.
	if _clientErr != nil {
		fmt.Println(_clientErr)
		return
	}

	fetch(client)
}

/**
 * Primary key-based query
 */
func fetch(client *ha3engine.Client) {

	searchRequestModel := &ha3engine.FetchRequest{}
	searchRequestModel.SetTableName("test1")

	// Assign values to the elements of the array.
	ids := make([]*string, 3)
	ids[0] = tea.String("1")
	ids[1] = tea.String("2")
	ids[2] = tea.String("3")
	searchRequestModel.SetIds(ids)

	response, _requestErr := client.Fetch(searchRequestModel)

	// If an error occurs when the system sends the request, _requestErr and an error message are returned.
	if _requestErr != nil {
		fmt.Println(_requestErr)
		return
	}

	// Display the response if no error occurs.
	fmt.Println(response)
}

Java in asynchronous mode

import com.aliyun.ha3engine.async.AsyncClient;
import com.aliyun.ha3engine.async.models.FetchRequest;
import com.aliyun.ha3engine.async.models.SearchResponse;
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.Arrays;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author alibaba
 */
public class PrimaryKeyQuery {
    
    /**
     * The engine client of the OpenSearch Vector Search Edition instance.
     */
    private AsyncClient client;

    @Before
    public void clientInit() {
        // The username and password that are used to access the instance. You can view the username and password in the API Endpoint section of 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 primaryKeyQuery() {
        try {
            FetchRequest fetchRequest = FetchRequest.builder().tableName("table_name").ids(Arrays.asList("1", "2")).build();
            CompletableFuture<SearchResponse> searchResponseCompletableFuture = client.fetch(fetchRequest);
            String responseBody = searchResponseCompletableFuture.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));
        }
    }
}

Prediction-based query

You can perform prediction-based queries when you use OpenSearch Vector Search Edition to vectorize text or images for vector-based queries. The following code provides examples on how to use SDKs to perform prediction-based queries.

Java

import com.aliyun.ha3engine.vector.Client;
import com.aliyun.ha3engine.vector.models.Config;
import com.aliyun.ha3engine.vector.models.QueryRequest;
import com.aliyun.ha3engine.vector.models.SearchResponse;
import com.aliyun.tea.TeaException;
import org.junit.Before;
import org.junit.Test;

import java.util.Map;

public class InferenceQuery {

    /**
     * The engine client of the OpenSearch Vector Search Edition instance.
     */
    private Client client;

    @Before
    public void clientInit() throws Exception {
        /*
          Initialize the engine client.
         */
        Config config = new Config();

        // The name of the instance. You can view the instance name 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 view the username in the API Endpoint section of the Instance Details page.
        config.setAccessUserName("username");
        // The password. You can modify the password in the API Endpoint section of the Instance Details page.
        config.setAccessPassWord("password");
        // The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
        config.setEndpoint("ha-cn-i7*****605.public.ha.aliyuncs.com");

        client = new Client(config);
    }

    @Test
    public void inferenceQuery() throws Exception {
        try {
            QueryRequest request = new QueryRequest();
            request.setTableName("test"); // Required. The name of the table to be queried.
            request.setContent("Child"); // Required. The content that you want to query.
            request.setModal("text"); // Required. This parameter is used to vectorize the terms to be queried.
            request.setTopK(100); // Optional. The number of results to be returned.
            request.setSearchParams("{\\\"qc.searcher.scan_ratio\\\":0.01}");
            SearchResponse searchResponse = client.inferenceQuery(request);
            System.out.println("Result:\n" + searchResponse.getBody());
        } 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.models import QueryRequest

config = Config(
    # The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
    endpoint="http://ha-cn-i7*****605.public.ha.aliyuncs.com",
    # The username. You can view the username in the API Endpoint section of the Instance Details page.
    access_user_name="username",
    # The password. You can modify the password in the API Endpoint section of the Instance Details page.
    access_pass_word="password")
client = Client(config)

def inference_query():
    request = QueryRequest(table_name="test",
                           content="Puppy",
                           modal="text",
                           namespace="123",
                           search_params="{\\\"qc.searcher.scan_ratio\\\":0.01}",
                           top_k=10)
    result = client.inference_query(request)
    print(result.body)

if __name__ == "__main__":
    inference_query()

Go

package main

import (
	"fmt"
	"github.com/alibabacloud-go/tea-utils/v2/service"
	"github.com/alibabacloud-go/tea/tea"
	ha3engine "github.com/aliyun/alibabacloud-ha3-go-sdk/client"
)


func main() {

	// Create a client instance for sending requests.
	config := &ha3engine.Config{
		// The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
		Endpoint: tea.String("ha-cn-i7*****605.public.ha.aliyuncs.com"),
		// The username. You can view the username in the API Endpoint section of the Instance Details page.
		AccessUserName: tea.String("username"),
		// The password. You can modify the password in the API Endpoint section of the Instance Details page.
		AccessPassWord: tea.String("password"),
		RuntimeOptions: &service.RuntimeOptions{
			KeepAlive:    tea.Bool(true),
			MaxIdleConns: tea.Int(1000),
			ReadTimeout:  tea.Int(1000),
		},
	}

	// Initialize a client for sending requests.
	client, _clientErr := ha3engine.NewClient(config)

	// If an error occurs when the system creates the client, _clientErr and an error message are returned.
	if _clientErr != nil {
		fmt.Println(_clientErr)
		return
	}

	inferenceQuery(client)
}

/**
 * Prediction-based query
 */
func inferenceQuery(client *ha3engine.Client) {

	searchRequestModel := &ha3engine.QueryRequest{}
	searchRequestModel.SetTableName("test1")

	// Create an array of 32-bit floating-point numbers.
	array := make([]*float32, 3)

	// Allocate a memory address to each value of the array and initialize the values.
	value1 := float32(1.23)
	value2 := float32(4.56)
	value3 := float32(7.89)

	// Assign values to the elements of the array.
	array[0] = &value1
	array[1] = &value2
	array[2] = &value3

	//searchRequestModel.SetVector(array)
	searchRequestModel.SetContent("Puppy")
	searchRequestModel.SetNamespace("123")
	searchRequestModel.SetModal("text") // Required. This parameter is used to vectorize the terms to be queried.

	response, _requestErr := client.InferenceQuery(searchRequestModel)

	// If an error occurs when the system sends the request, _requestErr and an error message are returned.
	if _requestErr != nil {
		fmt.Println(_requestErr)
		return
	}

	// Display the response if no error occurs.
	fmt.Println(response)
}

Java in asynchronous mode

import com.aliyun.ha3engine.async.AsyncClient;
import com.aliyun.ha3engine.async.models.QueryRequest;
import com.aliyun.ha3engine.async.models.SearchResponse;
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.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * Prediction-based query
 */
public class InferenceQuery {
    /**
     * The engine client of the OpenSearch Vector Search Edition instance.
     */
    private AsyncClient client;

    @Before
    public void clientInit() {
        // The username and password that are used to access the instance. You can view the username and password in the API Endpoint section of 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 inferenceQuery() {
        try {
            QueryRequest queryRequest = QueryRequest.builder()
                    .tableName("test2") // The name of the table to be queried.
                    .indexName("source_text_vector") // The name of the index to be queried.
                    .namespace("1")// The name of the namespace to be queried.
                    .modal("text")// The model name.
                    .content("Test text-to-vector conversion")// The content to be vectorized.
                    .topK(10) // The number of results to be returned.
                    .includeVector(false) // Specifies whether to return the vector information in documents.
                    .build();

            CompletableFuture<SearchResponse> responseCompletableFuture = client.inferenceQuery(queryRequest);
            String responseBody = responseCompletableFuture.get().getBody();
            System.out.println("queryInference 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));
        }
    }
}

Parameters:

  • The content parameter in QueryRequest indicates the content that you specify for the query. In text vectorization scenarios, you can specify text as the content to be queried. In image vectorization scenarios, you can specify text or images that are encoded in Base64 as the content to be queried.

  • The modal parameter in QueryRequest indicates the query mode. If you set the parameter to text, the system queries text based on the specified text in text vectorization scenarios, and queries images based on the specified text in image vectorization scenarios.

Multi-query

Java

import java.util.Arrays;
import java.util.Map;

import com.aliyun.ha3engine.vector.Client;
import com.aliyun.ha3engine.vector.models.Config;
import com.aliyun.ha3engine.vector.models.MultiQueryRequest;
import com.aliyun.ha3engine.vector.models.QueryRequest;
import com.aliyun.ha3engine.vector.models.SearchResponse;
import com.aliyun.tea.TeaException;

import org.junit.Before;
import org.junit.Test;

/**
 * Multi-query
 */
public class Demo {

    /**
     * The engine client of the OpenSearch Vector Search Edition instance.
     */
    private Client client;

    @Before
    public void clientInit() throws Exception {
        /*
          Initialize the engine client.
         */
        Config config = new Config();

        // The name of the instance. You can view the instance name 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 view the username in the API Endpoint section of the Instance Details page.
        config.setAccessUserName("username");
        // The password. You can modify the password in the API Endpoint section of the Instance Details page.
        config.setAccessPassWord("password");
        // The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
        config.setEndpoint("ha-cn-i7*****605.public.ha.aliyuncs.com");

        client = new Client(config);
    }

    @Test
    public void query() throws Exception {
        try {

            MultiQueryRequest multiQueryRequest = new MultiQueryRequest();

            multiQueryRequest.setTableName("test1"); // The name of the table to be queried.
            multiQueryRequest.setTopK(50); // The number of results to be returned.
            multiQueryRequest.setOutputFields(Arrays.asList("content", "name", "age")); // The fields to be returned.
            multiQueryRequest.setOrder("DESC");

            QueryRequest queryRequest = new QueryRequest();
            queryRequest.setIndexName("vector"); // The name of the index to be queried. Specify this parameter in SearchRequest or in each query.
            queryRequest.setNamespace("space_a"); // The namespace of the vector data to be queried.
            queryRequest.setVector(Arrays.asList(0.62498F, 0.886408F, 0.586114F, 0.043189F)); // The vector data to be queried. The data is of the ARRAY type.

            multiQueryRequest.setQueries(Arrays.asList(queryRequest));

            SearchResponse searchResponse = client.multiQuery(multiQueryRequest);
            System.out.println(searchResponse.getBody());

        } 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, MultiQueryRequest
from alibabacloud_ha3engine_vector.models import QueryRequest

config = Config(
    # The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
    endpoint="http://ha-cn-i7*****605.public.ha.aliyuncs.com",
    # The username. You can view the username in the API Endpoint section of the Instance Details page.
    access_user_name="username",
    # The password. You can modify the password in the API Endpoint section of the Instance Details page.
    access_pass_word="password")
client = Client(config)

def multi_query():
    query1 = QueryRequest(table_name="test",
                          vector=[0.1, 0.2, 0.3, 0.4],
                          include_vector=True,
                          search_params="{\\\"qc.searcher.scan_ratio\\\":0.01}",
                          top_k=10)
    queries = [query1]
    request = MultiQueryRequest(table_name="test",
                                queries=queries,
                                top_k=10)
    result = client.multi_query(request)
    print(result.body)

if __name__ == "__main__":
    multi_query()

Go

package main

import (
	"fmt"
	"github.com/alibabacloud-go/tea-utils/v2/service"
	"github.com/alibabacloud-go/tea/tea"
	ha3engine "github.com/aliyun/alibabacloud-ha3-go-sdk/client"
)

func main() {

	// Create a client instance for sending requests.
	config := &ha3engine.Config{
		// The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
		Endpoint: tea.String("ha-cn-i7*****605.public.ha.aliyuncs.com"),
		// The username. You can view the username in the API Endpoint section of the Instance Details page.
		AccessUserName: tea.String("username"),
		// The password. You can modify the password in the API Endpoint section of the Instance Details page.
		AccessPassWord: tea.String("password"),
		RuntimeOptions: &service.RuntimeOptions{
			KeepAlive:    tea.Bool(true),
			MaxIdleConns: tea.Int(1000),
			ReadTimeout:  tea.Int(1000),
		},
	}

	// Initialize a client for sending requests.
	client, _clientErr := ha3engine.NewClient(config)

	// If an error occurs when the system creates the client, _clientErr and an error message are returned.
	if _clientErr != nil {
		fmt.Println(_clientErr)
		return
	}
	multiQuery(client)
}


/**
 * Multi-query
 */
func multiQuery(client *ha3engine.Client) {

	query := &ha3engine.QueryRequest{}
	query.SetTableName("test1")

	// Create an array of 32-bit floating-point numbers.
	array := make([]*float32, 4)

	// Allocate a memory address to each value of the array and initialize the values.
	value1 := float32(1.23)
	value2 := float32(4.56)
	value3 := float32(7.89)
	value4 := float32(7.89)

	// Assign values to the elements of the array.
	array[0] = &value1
	array[1] = &value2
	array[2] = &value3
	array[3] = &value4

	query.SetVector(array)

	request := &ha3engine.MultiQueryRequest{}

	request.SetTableName("test1")

	querys := make([]*ha3engine.QueryRequest, 1)
	querys[0] = query
	request.SetQueries(querys)
	request.SetTopK(10)
	request.SetIncludeVector(true)
	request.SetOrder("DESC")

	response, _requestErr := client.MultiQuery(request)

	// If an error occurs when the system sends the request, _requestErr and an error message are returned.
	if _requestErr != nil {
		fmt.Println(_requestErr)
		return
	}

	// Display the response if no error occurs.
	fmt.Println(response)
}

Java in asynchronous mode

import com.aliyun.ha3engine.async.AsyncClient;
import com.aliyun.ha3engine.async.models.MultiQueryRequest;
import com.aliyun.ha3engine.async.models.QueryRequest;
import com.aliyun.ha3engine.async.models.SearchResponse;
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.Arrays;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author alibaba
 */
public class SearchDoc {

    /**
     * The engine client of the OpenSearch Vector Search Edition instance.
     */
    private AsyncClient client;

    @Before
    public void clientInit() {
        // The username and password that are used to access the instance. You can view the username and password in the API Endpoint section of 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 searchDoc()  {
        try {
            QueryRequest queryRequest = QueryRequest.builder()
                    .indexName("content_vector") // The name of the index to be queried.
                    .namespace("150086193") // The namespace of the index to be queried.
                    .vector(Arrays.asList(0.0001575F, 0.00682848F)) // The vector data to be queried.
                    .build();
            MultiQueryRequest multiQueryRequest = MultiQueryRequest.builder()
                    .tableName("hybrid") // The name of the table to be queried.
                    .topK(10) // The number of results to be returned.
                    .outputFields(Arrays.asList("content", "source", "instance_id")) // The fields to be returned.
                    .filter("type='TEXT'") // The filter expression.
                    .order("DESC") // The order in which the returned documents are sorted.
                    .queries(Arrays.asList(queryRequest)).build();
            CompletableFuture<SearchResponse> searchResponseCompletableFuture = client.multiQuery(multiQueryRequest);
            String responseBody = searchResponseCompletableFuture.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));
        }
    }
}

Batch query

Java

import com.aliyun.ha3engine.vector.Client;
import com.aliyun.ha3engine.vector.models.BatchRequest;
import com.aliyun.ha3engine.vector.models.Config;
import com.aliyun.ha3engine.vector.models.QueryRequest;
import com.aliyun.ha3engine.vector.models.SearchResponse;
import com.aliyun.tea.TeaException;
import org.junit.Before;
import org.junit.Test;
import org.something.util.VectorUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Batch query
 */
public class BatchQuery {

    /**
     * The engine client of the OpenSearch Vector Search Edition instance.
     */
    private Client client;

    @Before
    public void clientInit() throws Exception {
        /*
          Initialize the engine client.
         */
        Config config = new Config();

        // The name of the instance. You can view the instance name 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 view the username in the API Endpoint section of the Instance Details page.
        config.setAccessUserName("username");
        // The password. You can modify the password in the API Endpoint section of the Instance Details page.
        config.setAccessPassWord("password");
        // The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
        config.setEndpoint("ha-cn-i7*****605.public.ha.aliyuncs.com");

        client = new Client(config);
    }

    @Test
    public void batch() throws Exception {
        try {
            BatchRequest request = new BatchRequest();

            QueryRequest queryRequest1 = new QueryRequest();
            queryRequest1.setTableName("test");
            queryRequest1.setIndexName("vector");
            queryRequest1.setOutputFields(Arrays.asList("id", "name"));
            queryRequest1.setVector(Arrays.asList(0.62498F, 0.886408F, 0.586114F, 0.043189F));

            QueryRequest queryRequest2 = new QueryRequest();
            queryRequest2.setTableName("test1");
            queryRequest2.setIndexName("vector");
            queryRequest2.setOutputFields(Arrays.asList("id", "title"));
            queryRequest2.setVector(Arrays.asList(0.62498F, 0.886408F, 0.586114F, 0.043189F));
            List<QueryRequest> queries = new ArrayList<>();
            queries.add(queryRequest1);
            queries.add(queryRequest2);
            request.setQueries(queries);

            SearchResponse searchResponse = client.batch(request);
            System.out.println("Result:\n" + searchResponse.getBody());
        } 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, QueryRequest
from alibabacloud_ha3engine_vector.models import BatchRequest

config = Config(
    # The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
    endpoint="http://ha-cn-i7*****605.public.ha.aliyuncs.com",
    # The username. You can view the username in the API Endpoint section of the Instance Details page.
    access_user_name="username",
    # The password. You can modify the password in the API Endpoint section of the Instance Details page.
    access_pass_word="password")
client = Client(config)

def batch_query():
    query_request1 = QueryRequest(table_name="test",
                                  index_name="vector",
                                  vector=[0.524355, 0.940892, 0.002874, 0.551031],
                                  include_vector=True,
                                  search_params="{\\\"qc.searcher.scan_ratio\\\":0.01}",
                                  top_k=10)
    query_request2 = QueryRequest(table_name="test1",
                                  index_name="vector",
                                  vector=[0.524355, 0.940892, 0.002874, 0.551031],
                                  include_vector=True,
                                  search_params="{\\\"qc.searcher.scan_ratio\\\":0.01}",
                                  top_k=10)
    request = BatchRequest(queries=[query_request1, query_request2])
    result = client.batch_query(request)
    print(result.headers)
    print(result.body)

if __name__ == "__main__":
    batch_query()

Go

package main

import (
    "fmt"
    "github.com/alibabacloud-go/tea-utils/v2/service"
    "github.com/alibabacloud-go/tea/tea"
    ha3engine "github.com/aliyun/alibabacloud-ha3-go-sdk/client"
)

func main() {

    // Create a client instance for sending requests.
    config := &ha3engine.Config{
        // The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
        Endpoint: tea.String("ha-cn-i7*****605.public.ha.aliyuncs.com"),
        // The username. You can view the username in the API Endpoint section of the Instance Details page.
        AccessUserName: tea.String("username"),
        // The password. You can modify the password in the API Endpoint section of the Instance Details page.
        AccessPassWord: tea.String("password"),
        RuntimeOptions: &service.RuntimeOptions{
            KeepAlive:    tea.Bool(true),
            MaxIdleConns: tea.Int(1000),
            ReadTimeout:  tea.Int(1000),
        },
    }

    // Initialize a client for sending requests.
    client, _clientErr := ha3engine.NewClient(config)

    // If an error occurs when the system creates the client, _clientErr and an error message are returned.
    if _clientErr != nil {
        fmt.Println(_clientErr)
        return
    }

    batch(client)
}

func batch(client *ha3engine.Client) {

    queryRequestModel1 := &ha3engine.QueryRequest{}
    queryRequestModel1.SetTableName("test")
    // Create an array of 32-bit floating-point numbers.
    ids1 := make([]*float32, 4)
    // Assign values to the elements of the array.
    ids1[0] = tea.Float32(0.1)
    ids1[1] = tea.Float32(0.2)
    ids1[2] = tea.Float32(0.3)
    ids1[3] = tea.Float32(0.4)
    queryRequestModel1.SetVector(ids1)

    queryRequestModel2 := &ha3engine.QueryRequest{}
    queryRequestModel2.SetTableName("test1")
    // Create an array of 32-bit floating-point numbers.
    ids2 := make([]*float32, 4)
    // Assign values to the elements of the array.
    ids2[0] = tea.Float32(0.1)
    ids2[1] = tea.Float32(0.2)
    ids2[2] = tea.Float32(0.3)
    ids2[3] = tea.Float32(0.4)
    queryRequestModel2.SetVector(ids2)

    batchRequestModel := &ha3engine.BatchRequest{}
    batchRequestModel.SetQueries([]*ha3engine.QueryRequest{queryRequestModel1, queryRequestModel2})

    response, _requestErr := client.BatchQuery(batchRequestModel)

    // If an error occurs when the system sends the request, _requestErr and an error message are returned.
    if _requestErr != nil {
        fmt.Println(_requestErr)
        return
    }

    // Display the response if no error occurs.
    fmt.Println(response)
}

Java in asynchronous mode

import com.aliyun.ha3engine.async.AsyncClient;
import com.aliyun.ha3engine.async.models.BatchRequest;
import com.aliyun.ha3engine.async.models.QueryRequest;
import com.aliyun.ha3engine.async.models.SearchResponse;
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 org.something.util.VectorUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * Batch query
 */
public class BatchQuery {
    
    /**
     * The engine client of the OpenSearch Vector Search Edition instance.
     */
    private AsyncClient client;

    @Before
    public void clientInit() {
        // The username and password that are used to access the instance. You can view the username and password in the API Endpoint section of 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 batch() {
        try {
            QueryRequest queryRequest1 = QueryRequest.builder()
                    .tableName("test") // The name of the table to be queried.
                    .indexName("vector") // The name of the index to be queried.
                    .vector(Arrays.asList(0.62498F, 0.886408F, 0.586114F, 0.043189F)) // The vector data to be queried.
                    .outputFields(Arrays.asList("id", "name"))
                    .build();
            QueryRequest queryRequest2 = QueryRequest.builder()
                    .tableName("test1") // The name of the table to be queried.
                    .indexName("vector") // The name of the index to be queried.
                    .vector(Arrays.asList(0.62498F, 0.886408F, 0.586114F, 0.043189F)) // The vector data to be queried.
                    .outputFields(Arrays.asList("id", "title"))
                    .build();
            List<QueryRequest> queries = new ArrayList<>();
            queries.add(queryRequest1);
            queries.add(queryRequest2);
            BatchRequest request = BatchRequest.builder()
                    .queries(queries)
                    .build();

            CompletableFuture<SearchResponse> responseCompletableFuture = client.batchQuery(request);
            String responseBody = responseCompletableFuture.get().getBody();
            System.out.println("batch 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));
        }
    }
}

Hybrid query by using a single vector

You can perform hybrid queries by using dense vectors and sparse vectors. The following sample code provides examples on how to use SDKs to perform hybrid queries:

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.Arrays;
import java.util.Map;

public class Demo {

    /**
     * The engine client of the OpenSearch Vector Search Edition instance.
     */
    private Client client;

    @Before
    public void clientInit() throws Exception {
        /*
          Initialize the engine client.
         */
        Config config = new Config();

        // The name of the instance. You can view the instance name 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 view the username in the API Endpoint section of the Instance Details page.
        config.setAccessUserName("username");
        // The password. You can modify the password in the API Endpoint section of the Instance Details page.
        config.setAccessPassWord("password");
        // The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
        config.setEndpoint("ha-cn-i7*****605.public.ha.aliyuncs.com");

        client = new Client(config);
    }

    @Test
    public void query() throws Exception {
        try {
            QueryRequest queryRequest = new QueryRequest();
            queryRequest.setIndexName("index"); // The name of the index to be queried. Specify this parameter in SearchRequest or in each query.
            queryRequest.setNamespace("cate1"); // The namespace of the vector data to be queried.
            queryRequest.setVector(Arrays.asList(0.0001575F, 0.00682848F)); // The vector data to be queried. The data is of the ARRAY type.

            SparseData sparseData = new SparseData();
            sparseData.setCount(Arrays.asList(2)); // The number of elements in each sparse vector.
            sparseData.setIndices(Arrays.asList(983589459L, 1261855554L)); // The indexes of the elements in ascending order.
            sparseData.setValues(Arrays.asList(0.0001575F, 0.00682848F)); // The values of the elements in the same order as the indexes.

            queryRequest.setSparseData(sparseData);

            SearchResponse searchResponse = client.query(queryRequest);
            System.out.println(searchResponse.getBody());

        } 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, SparseData
from alibabacloud_ha3engine_vector.models import QueryRequest

config = Config(
    # The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
    endpoint="http://ha-cn-i7*****605.public.ha.aliyuncs.com",
    # The username. You can view the username in the API Endpoint section of the Instance Details page.
    access_user_name="username",
    # The password. You can modify the password in the API Endpoint section of the Instance Details page.
    access_pass_word="password")
client = Client(config)

def mix_query():
    sparse_data = SparseData(indices=[101, 203], values=[0.1, 0.5])
    request = QueryRequest(table_name="test",
                           index_name="embedding",
                           vector=[0.1, 0.2, 0.3, 0.4],
                           sparse_data=sparse_data,
                           include_vector = True,
                           top_k = 10)
    result = client.query(request)
    print(result.body)

if __name__ == "__main__":
    mix_query()

Go

package main

import (
	"fmt"
	"github.com/alibabacloud-go/tea-utils/v2/service"
	"github.com/alibabacloud-go/tea/tea"
	ha3engine "github.com/aliyun/alibabacloud-ha3-go-sdk/client"
	"sync"
)

func main() {

	// Create a client instance for sending requests.
	config := &ha3engine.Config{
		// The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
		Endpoint: tea.String("ha-cn-i7*****605.public.ha.aliyuncs.com"),
		// The username. You can view the username in the API Endpoint section of the Instance Details page.
		AccessUserName: tea.String("username"),
		// The password. You can modify the password in the API Endpoint section of the Instance Details page.
		AccessPassWord: tea.String("password"),
		RuntimeOptions: &service.RuntimeOptions{
			KeepAlive:    tea.Bool(true),
			MaxIdleConns: tea.Int(1000),
			ReadTimeout:  tea.Int(1000),
		},
	}

	// Initialize a client for sending requests.
	client, _clientErr := ha3engine.NewClient(config)

	// If an error occurs when the system creates the client, _clientErr and an error message are returned.
	if _clientErr != nil {
		fmt.Println(_clientErr)
		return
	}

	mixQuery(client)
}

func mixQuery(client *ha3engine.Client) {

	searchRequestModel := &ha3engine.QueryRequest{}
	searchRequestModel.SetTableName("test1")

	// Create an array of 32-bit floating-point numbers.
	ids := make([]*float32, 4)

	// Assign values to the elements of the array.
	ids[0] = tea.Float32(0.1)
	ids[1] = tea.Float32(0.2)
	ids[2] = tea.Float32(0.3)
	ids[3] = tea.Float32(0.4)

	searchRequestModel.SetVector(ids)
	count := make([]*int, 1)
	count[0] = tea.Int(2)

	indices := make([]*int64, 2)
	indices[0] = tea.Int64(1)
	indices[1] = tea.Int64(2)

	values := make([]*float32, 2)
	values[0] = tea.Float32(0.1)
	values[1] = tea.Float32(0.2)
	sparseDataha := &ha3engine.SparseData{
		Count:   count,
		Indices: indices,
		Values:  values,
	}
	searchRequestModel.SetSparseData(sparseDataha)

	response, _requestErr := client.Query(searchRequestModel)

	// If an error occurs when the system sends the request, _requestErr and an error message are returned.
	if _requestErr != nil {
		fmt.Println(_requestErr)
		return
	}

	// Display the response if no error occurs.
	fmt.Println(response)
}

Java in asynchronous mode

import com.aliyun.ha3engine.async.AsyncClient;
import com.aliyun.ha3engine.async.models.QueryRequest;
import com.aliyun.ha3engine.async.models.SearchResponse;
import com.aliyun.ha3engine.async.models.SparseData;
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.Arrays;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * Hybrid query
 */
public class MixQuery {
    /**
     * The engine client of the OpenSearch Vector Search Edition instance.
     */
    private AsyncClient client;

    @Before
    public void clientInit() {
        // The username and password that are used to access the instance. You can view the username and password in the API Endpoint section of 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 mixQuery() {
        try {
            QueryRequest queryRequest = QueryRequest.builder()
                    .tableName("test1") // The name of the table to be queried.
                    .indexName("vector") // The name of the index to be queried.
                    .vector(Arrays.asList(0.62498F, 0.886408F, 0.586114F, 0.043189F)) // The vector data to be queried.
                    .sparseData(SparseData.builder().count(Arrays.asList(2)).indices(Arrays.asList(1L,2L)).values(Arrays.asList(0.1F,0.2F)).build())// The sparse vectors.
                    .topK(10) // The number of results to be returned.
                    .includeVector(false) // Specifies whether to return the vector information in documents.
                    .build();
            
            CompletableFuture<SearchResponse> responseCompletableFuture = client.query(queryRequest);
            String responseBody = responseCompletableFuture.get().getBody();
            System.out.println("query 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));
        }
    }
}

Hybrid query by using multiple vectors

You can perform hybrid queries by using dense vectors and sparse vectors. The following sample code provides examples on how to use SDKs to perform hybrid queries:

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.Arrays;
import java.util.Map;

public class Demo {

    /**
     * The engine client of the OpenSearch Vector Search Edition instance.
     */
    private Client client;

    @Before
    public void clientInit() throws Exception {
        /*
          Initialize the engine client.
         */
        Config config = new Config();

        // The name of the instance. You can view the instance name 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 view the username in the API Endpoint section of the Instance Details page.
        config.setAccessUserName("username");
        // The password. You can modify the password in the API Endpoint section of the Instance Details page.
        config.setAccessPassWord("password");
        // The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
        config.setEndpoint("ha-cn-i7*****605.public.ha.aliyuncs.com");

        client = new Client(config);
    }

    @Test
    public void query() throws Exception {
        try {

            MultiQueryRequest multiQueryRequest = new MultiQueryRequest();

            multiQueryRequest.setTableName("main"); // The name of the table to be queried.
            multiQueryRequest.setTopK(50); // The number of results to be returned.
            multiQueryRequest.setOutputFields(Arrays.asList("content", "source", "instance_id")); // The fields to be returned.
            multiQueryRequest.setFilter("type='TEXT'"); // The filter expression.
            multiQueryRequest.setOrder("DESC");

            QueryRequest queryRequest = new QueryRequest();
            queryRequest.setIndexName("index"); // The name of the index to be queried. Specify this parameter in SearchRequest or in each query.
            queryRequest.setNamespace("cate1"); // The namespace of the vector data to be queried.
            queryRequest.setVector(Arrays.asList(0.0001575F, 0.00682848F)); // The vector data to be queried. The data is of the ARRAY type.

            SparseData sparseData = new SparseData();
            sparseData.setCount(Arrays.asList(2)); // The number of elements in each sparse vector.
            sparseData.setIndices(Arrays.asList(983589459L, 1261855554L)); // The indexes of the elements in ascending order.
            sparseData.setValues(Arrays.asList(0.0001575F, 0.00682848F)); // The values of the elements in the same order as the indexes.

            queryRequest.setSparseData(sparseData);
            multiQueryRequest.setQueries(Arrays.asList(queryRequest));

            SearchResponse searchResponse = client.multiQuery(multiQueryRequest);
            System.out.println(searchResponse.getBody());

        } 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, SparseData, MultiQueryRequest
from alibabacloud_ha3engine_vector.models import QueryRequest

config = Config(
    # The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
    endpoint="http://ha-cn-i7*****605.public.ha.aliyuncs.com",
    # The username. You can view the username in the API Endpoint section of the Instance Details page.
    access_user_name="username",
    # The password. You can modify the password in the API Endpoint section of the Instance Details page.
    access_pass_word="password")
client = Client(config)

def mix_multi_query():
    sd1 = SparseData(indices=[101, 203], values=[0.1, 0.5])
    sd2 = SparseData(indices=[200, 405, 502], values=[0.9, 0.5, 0.75])
    q1 = QueryRequest(vector=[0.1, 0.2, 0.3, 0.4], sparse_data=sd1)
    q2 = QueryRequest(vector=[0.4, 0.5, 0.6, 0.7], sparse_data=sd2)
    request = MultiQueryRequest(table_name="test",
                                queries=[q1, q2],
                                top_k=3,
                                include_vector=True)
    result = client.multi_query(request)
    print(result.body)

if __name__ == "__main__":
    mix_multi_query()

Go

package main

import (
	"fmt"
	"github.com/alibabacloud-go/tea-utils/v2/service"
	"github.com/alibabacloud-go/tea/tea"
	ha3engine "github.com/aliyun/alibabacloud-ha3-go-sdk/client"
	"sync"
)

func main() {

	// Create a client instance for sending requests.
	config := &ha3engine.Config{
		// The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
		Endpoint: tea.String("ha-cn-i7*****605.public.ha.aliyuncs.com"),
		// The username. You can view the username in the API Endpoint section of the Instance Details page.
		AccessUserName: tea.String("username"),
		// The password. You can modify the password in the API Endpoint section of the Instance Details page.
		AccessPassWord: tea.String("password"),
		RuntimeOptions: &service.RuntimeOptions{
			KeepAlive:    tea.Bool(true),
			MaxIdleConns: tea.Int(1000),
			ReadTimeout:  tea.Int(1000),
		},
	}

	// Initialize a client for sending requests.
	client, _clientErr := ha3engine.NewClient(config)

	// If an error occurs when the system creates the client, _clientErr and an error message are returned.
	if _clientErr != nil {
		fmt.Println(_clientErr)
		return
	}

	mixMultiVectorQuery(client)
}

func mixMultiVectorQuery(client *ha3engine.Client) {

	query1 := &ha3engine.QueryRequest{}
	// Create an array of 32-bit floating-point numbers.
	array := make([]*float32, 4)

	// Allocate a memory address to each value of the array and initialize the values.
	value1 := float32(1.23)
	value2 := float32(4.56)
	value3 := float32(7.89)
	value4 := float32(7.89)

	// Assign values to the elements of the array.
	array[0] = &value1
	array[1] = &value2
	array[2] = &value3
	array[3] = &value4

	count := make([]*int, 1)
	count[0] = tea.Int(2)
	indices := make([]*int64, 2)
	indices[0] = tea.Int64(1)
	indices[1] = tea.Int64(2)
	values := make([]*float32, 2)
	values[0] = tea.Float32(0.1)
	values[1] = tea.Float32(0.2)
	sparseData := &ha3engine.SparseData{
		Count:   count,
		Indices: indices,
		Values:  values,
	}

	query1.SetSparseData(sparseData)
	query1.SetVector(array)

	query2 := &ha3engine.QueryRequest{}
	query2.SetVector(array)

	request := &ha3engine.MultiQueryRequest{}

	request.SetTableName("test1")

	querys := make([]*ha3engine.QueryRequest, 2)
	querys[0] = query1
	querys[1] = query2
	request.SetQueries(querys)
	request.SetTopK(10)
	request.SetIncludeVector(false)
	request.SetOrder("DESC")

	response, _requestErr := client.MultiQuery(request)

	// If an error occurs when the system sends the request, _requestErr and an error message are returned.
	if _requestErr != nil {
		fmt.Println(_requestErr)
		return
	}

	// Display the response if no error occurs.
	fmt.Println(response)
}

Java in asynchronous mode

import com.aliyun.ha3engine.async.AsyncClient;
import com.aliyun.ha3engine.async.models.MultiQueryRequest;
import com.aliyun.ha3engine.async.models.QueryRequest;
import com.aliyun.ha3engine.async.models.SearchResponse;
import com.aliyun.ha3engine.async.models.SparseData;
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.Arrays;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * Query based on multiple vectors
 */
public class MixMultiVectorQuery {

    /**
     * The engine client of the OpenSearch Vector Search Edition instance.
     */
    private AsyncClient client;

    @Before
    public void clientInit() {
        // The username and password that are used to access the instance. You can view the username and password in the API Endpoint section of 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 mixMultiVectorQuery() {
        try {
            QueryRequest query1 = QueryRequest.builder()
                    .vector(Arrays.asList(0.62498F, 0.886408F, 0.586114F, 0.043189F))
                    .sparseData(SparseData.builder().count(Arrays.asList(2)).indices(Arrays.asList(1L,2L)).values(Arrays.asList(0.1F,0.2F)).build())// The sparse vectors.
                    .namespace("space_a")
                    .build();
            QueryRequest query2 = QueryRequest.builder()
                    .vector(Arrays.asList(0.62498F, 0.886408F, 0.586114F, 0.043189F))
                    .sparseData(SparseData.builder().count(Arrays.asList(2)).indices(Arrays.asList(1L,2L)).values(Arrays.asList(0.1F,0.2F)).build())// The sparse vectors.
                    .namespace("space_b")
                    .build();
            MultiQueryRequest request = MultiQueryRequest.builder()
                    .tableName("test2")// The name of the table to be queried.
                    .topK(3)
                    .includeVector(false)// Specifies whether to return the vector information in documents.
                    .queries(Arrays.asList(query1, query2))// The filter conditions of the namespaces to be queried.
                    .build();
            
            CompletableFuture<SearchResponse> responseCompletableFuture = client.multiQuery(request);
            String responseBody = responseCompletableFuture.get().getBody();
            System.out.println("query 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));
        }
    }
}
Important

To perform queries based on multiple vectors, make sure that the vectors are of the same type. For example, you can perform queries based on multiple dense vectors or dense-sparse vectors.

Hybrid query based on vectors and text

Java

import java.util.Arrays;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
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;

/**
 * Hybrid query based on vectors and text
 **/
public class VectorSearchService {

    /**
     * The engine client of the OpenSearch Vector Search Edition instance.
     */
    private Client client;


    @Before
    public void clientInit() throws Exception {
        /*
          Initialize the engine client.
         */
        Config config = new Config();

        // The name of the instance. You can view the instance name 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 view the username in the API Endpoint section of the Instance Details page.
        config.setAccessUserName("username");
        // The password. You can modify the password in the API Endpoint section of the Instance Details page.
        config.setAccessPassWord("password");
        // The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
        config.setEndpoint("ha-cn-i7*****605.public.ha.aliyuncs.com");

        client = new Client(config);
    }

    @Test
    public void search() throws Exception {
        try {
            // Construct the query request object.
            SearchRequest request = new SearchRequest();
            request.setTableName("test1");// Required. The name of the table to be queried.
            // The parameters for k-nearest neighbors (k-NN) queries.
            QueryRequest queryRequest = new QueryRequest();
            queryRequest.setIndexName("vector");// The name of the index to be queried.
            queryRequest.setTopK(10);
            queryRequest.setVector(Arrays.asList(0.62498F, 0.886408F, 0.586114F, 0.043189F));// The vector data to be queried.
            // The sparse vectors.
            SparseData sparseData = new SparseData();
            sparseData.setIndices(Arrays.asList(1L, 2L, 3L, 4L));
            sparseData.setValues(Arrays.asList(0.0001575F, 0.00682848F, 0.008745F, 0.4583474F));
            queryRequest.setSparseData(sparseData);
            request.setKnn(queryRequest);
            // Construct text query parameters.
            TextQuery textQuery = new TextQuery();
            textQuery.setQueryString("content:'Publish'");
            request.setText(textQuery);
            request.setSize(10);
            request.setOutputFields(Arrays.asList("id", "content", "name"));

            SearchResponse searchResponse = client.search(request);
            System.out.println("Result:\n" + searchResponse.getBody());
        } 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, SearchRequest, TextQuery
from alibabacloud_ha3engine_vector.models import QueryRequest

config = Config(
    # The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
    endpoint="http://ha-cn-i7*****605.public.ha.aliyuncs.com",
    # The username. You can view the username in the API Endpoint section of the Instance Details page.
    access_user_name="username",
    # The password. You can modify the password in the API Endpoint section of the Instance Details page.
    access_pass_word="password")
client = Client(config)

def search():
    knn = QueryRequest(index_name="vector",
                       vector=[0.62498, 0.886408, 0.586114, 0.043189],
                       include_vector=True,
                       top_k=10)
    text = TextQuery(query_string="content:'Publish'")
    request = SearchRequest(table_name="test1",
                            size=10,
                            output_fields=["id", "content", "name"],
                            knn=knn,
                            text=text)
    result = client.search(request)
    print(result.body)

if __name__ == "__main__":
    search()

Go

package main

import (
	"fmt"
	"github.com/alibabacloud-go/tea-utils/v2/service"
	"github.com/alibabacloud-go/tea/tea"
	ha3engine "github.com/aliyun/alibabacloud-ha3-go-sdk/client"
	"sync"
)

func main() {

	// Create a client instance for sending requests.
	config := &ha3engine.Config{
		// The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
		Endpoint: tea.String("ha-cn-i7*****605.public.ha.aliyuncs.com"),
		// The username. You can view the username in the API Endpoint section of the Instance Details page.
		AccessUserName: tea.String("username"),
		// The password. You can modify the password in the API Endpoint section of the Instance Details page.
		AccessPassWord: tea.String("password"),
		RuntimeOptions: &service.RuntimeOptions{
			KeepAlive:    tea.Bool(true),
			MaxIdleConns: tea.Int(1000),
			ReadTimeout:  tea.Int(1000),
		},
	}

	// Initialize a client for sending requests.
	client, _clientErr := ha3engine.NewClient(config)

	// If an error occurs when the system creates the client, _clientErr and an error message are returned.
	if _clientErr != nil {
		fmt.Println(_clientErr)
		return
	}

	search(client)
}
func search(client *ha3engine.Client) {

	searchRequestModel := &ha3engine.SearchRequest{}
	searchRequestModel.SetTableName("test1")

	knn := &ha3engine.QueryRequest{}

	// Assign values to the elements of the array.
	ids := make([]*float32, 4)
	ids[0] = tea.Float32(0.1)
	ids[1] = tea.Float32(0.2)
	ids[2] = tea.Float32(0.3)
	ids[3] = tea.Float32(0.4)
	knn.SetVector(ids)
	
	text := &ha3engine.TextQuery{}
	text.SetQueryString("content:'Publish'")

	searchRequestModel.SetText(text)
	searchRequestModel.SetKnn(knn)

	response, _requestErr := client.Search(searchRequestModel)

	// If an error occurs when the system sends the request, _requestErr and an error message are returned.
	if _requestErr != nil {
		fmt.Println(_requestErr)
		return
	}

	// Display the response if no error occurs.
	fmt.Println(response)
}

Java in asynchronous mode

import com.aliyun.ha3engine.async.AsyncClient;
import com.aliyun.ha3engine.async.models.QueryRequest;
import com.aliyun.ha3engine.async.models.SearchRequest;
import com.aliyun.ha3engine.async.models.SearchResponse;
import com.aliyun.ha3engine.async.models.TextQuery;
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.Arrays;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class VectorTextMixSearch {

    /**
     * The engine client of the OpenSearch Vector Search Edition instance.
     */
    private AsyncClient client;

    @Before
    public void clientInit() {
        // The username and password that are used to access the instance. You can view the username and password in the API Endpoint section of 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 search() {
        try {
            SearchRequest request = SearchRequest.builder()
                    .tableName("test1")// The name of the table to be queried.
                    .size(10)// The number of results to be returned.
                    .knn(QueryRequest.builder()
                            .vector(Arrays.asList(0.1f, 0.2f, 0.3f,0.4f))// The vector data to be queried.
                            .namespace("space_a")// The name of the namespace to be queried.
                            .build())// The parameters for k-NN queries.
                    .text(TextQuery.builder()
                            .queryString("content:'Content'")// The query condition.
                            .build())
                    .outputFields(Arrays.asList("name", "age", "content"))// The fields to be returned.
                    .build();

            CompletableFuture<SearchResponse> responseCompletableFuture = client.search(request);
            String responseBody = responseCompletableFuture.get().getBody();

            System.out.println("search 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));
        }
    }
}

Additional information

  • For information about the response to a request, see Response parameters.

  • Do not run the go get github.com/aliyun/alibabacloud-ha3-go-sdk command to pull dependencies. The SDK dependencies for OpenSearch Vector Search Edition and OpenSearch Retrieval Engine Edition are classified into the same tag in GitHub. You must specify the corresponding edition based on the instance edition when you pull dependencies.