查询数据

本文档介绍如何使用Java异步、Java、Python语言进行向量检索、预测检索以及包含过滤条件的检索。

相关依赖

Java异步

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

Java

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

Python

#Requires: Python >=3.6
pip install alibabacloud-ha3engine-vector

Go

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

参数说明

Java、Python SDK中都需要配置如下4个必要参数(endpointinstance_id、access_user_name、access_pass_word):

  • endpoint:私网/公网域名

可在实例详情页中网络信息和API入口查看:

image.png

开启公网访问后可以在本地通过公网域名(包含public的域名)调用实例,可以参考添加白名单配置访问的白名单IP。

若有ECS机器,可通过配置相同的交换机通过API域名调用实例。

  • instance_id:实例ID

image.png

  • access_user_name:用户名

  • access_pass_word:密码

用户名和密码可以在实例详情页中API入口处进行查看:(密码是购买实例时设置的,可以修改)

image.png

向量检索

简单检索

Java异步

package com.aliyun.ha3engine;

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

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;

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

    public static void main(String[] args) throws Exception {
        try {
            // 配置实例的用户名密码, 可在实例详情页>网络信息 查看
            AsyncConfigInfoProvider provider = AsyncConfigInfoProvider.create("username", "password");

            // 初始化异步客户端
            AsyncClient client = AsyncClient.builder()
                    .credentialsProvider(provider)
                    .overrideConfiguration(
                            ClientOverrideConfiguration.create()
                                    //公网域名, 实例名可在实例详情页>API入口 查看
                                    .setEndpointOverride("ha-cn-***********.public.ha.aliyuncs.com")
                            .setProtocol("http")
                    ).build();
            QueryRequest queryRequest = QueryRequest.builder()
                    .tableName("table_name") // 查询的表名
                    .indexName("index_name") // 查询的索引名
                    .vector(Arrays.asList(0.0001575F, 0.00682848F)) // 查询的向量数据
                    .topK(10) // 返回个数
                    .includeVector(true) //是否返回文档中的向量信息
                    .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.getMessage());
            Map<String, Object> abc = e.getData();
            System.out.println(com.aliyun.teautil.Common.toJSONString(abc));
        }
    }
}

Java

package com.aliyun.ha3engine.demo;

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 {

    /**
     * client,暂时支持查询操作
     */
    private Client client;

    @Before
    public void clientInit() throws Exception {

        /*
          初始化问天引擎client
         */
        Config config = new Config();

        //  实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
        config.setInstanceId("ha-cn-i7*****605");

        // 用户名,可在实例详情页>网络信息 查看
        config.setAccessUserName("username");

        // 密码,可在实例详情页>网络信息 修改
        config.setAccessPassWord("password");

        //私网域名调用请填写endpoint,如需要公网域名调用,请打开如下注释
        config.setEndpoint("ha-cn-i7*****605.public.ha.aliyuncs.com");
        //公网ip调用填写httpProxy,如需要公网IP调用,请打开如下注释
        //        config.setHttpProxy("http://116.62.XXX.XXX:80");

        client = new Client(config);

    }

    @Test
    public void query() throws Exception {
        try {
            QueryRequest request = new QueryRequest();
            request.setTableName("gist"); // 必填项, 查询的表名
            request.setVector(Arrays.asList(0.183762561f, 0.738372617f)); // 必填项, 查询的向量数据
            request.setTopK(100); // 非必填项, 返回个数
            request.setIncludeVector(true); // 非必填项, 是否返回文档中的向量信息
            request.setSearchParams("{\\\"qc.searcher.scan_ratio\\\":0.01}");
            SearchResponse searchResponse = client.query(request);
            System.out.println("搜索结果:\n" + searchResponse.getBody());
        } catch (TeaException e) {
            System.out.println(e.getCode());
            System.out.println(e.getMessage());
            Map<String, Object> abc = e.getData();
            System.out.println(com.aliyun.teautil.Common.toJSONString(abc));
        }
    }

}

Python

from alibabacloud_ha3engine_vector.client import Client
from alibabacloud_ha3engine_vector.models import Config
from alibabacloud_ha3engine_vector.models import QueryRequest

config = Config(
        #  私网域名调用请填写endpoint
        endpoint="ha-cn-7mz2ougaw02.ha.aliyuncs.com",
        #  实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
        instance_id="ha-cn-7mz2ougaw02",
        #  用户名,可在实例详情页>网络信息 查看
        access_user_name="user",
        #  密码,可在实例详情页>网络信息 修改
        access_pass_word="111111")
client = Client(config)

request = QueryRequest(table_name="gist",
                        vector=[0.1, 0.2, 0.3],
                      include_vector=True,
                      search_params="{\\\"qc.searcher.scan_ratio\\\":0.01}",
                      top_k=10)
result = client.query(request)

Go

package main

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

func main() {

	// 创建请求客户端配置
	config := &ha3engine.Config{
		// 私网域名或者公网域名
		Endpoint: tea.String("<Endpoint>"),
		//  实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
		InstanceId: tea.String("<InstanceId>"),
		// 用户名,可在实例详情页>网络信息 查看
		AccessUserName: tea.String("<AccessUserName>"),
		// 密码,可在实例详情页>网络信息 修改
		AccessPassWord: tea.String("<AccessPassWord>"),
	}

	// 初始化一个client, 用以发送请求.
	client, _clientErr := ha3engine.NewClient(config)

	// 如果 NewClient 过程中出现异常. 则 返回 _clientErr 且输出 错误信息.
	if _clientErr != nil {
		fmt.Println(_clientErr)
		return
	}

	query(client)
}

/**
 *	简单查询
 */
func query(client *ha3engine.Client) {

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

	// 创建一个指向float32的指针数组
	array := make([]*float32, 3)

	// 分配内存地址并初始化值
	value1 := float32(1.23)
	value2 := float32(4.56)
	value3 := float32(7.89)

	// 给指针数组元素赋值
	array[0] = &value1
	array[1] = &value2
	array[2] = &value3

	searchRequestModel.SetVector(array)

	response, _requestErr := client.Query(searchRequestModel)

	// 如果 发送请求 过程中出现异常. 则 返回 _requestErr 且输出 错误信息.
	if _requestErr != nil {
		fmt.Println(_requestErr)
		return
	}

	// 输出正常返回的 response 内容.
	fmt.Println(response)

}

多向量检索

支持一次检索中包含多个向量,检索结果会合并排序,返回最优的topK。

Java

package com.aliyun.ha3engine.demo;

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 {

    /**
     * 问天引擎client,暂时支持查询操作
     */
    private Client client;

    @Before
    public void clientInit() throws Exception {

        /*
          初始化问天引擎client
         */
        Config config = new Config();

        //  实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
        config.setInstanceId("ha-cn-i7*****605");

        // 用户名,可在实例详情页>网络信息 查看
        config.setAccessUserName("username");

        // 密码,可在实例详情页>网络信息 修改
        config.setAccessPassWord("password");

        //私网域名调用请填写endpoint,如需要公网域名调用,请打开如下注释
        config.setEndpoint("ha-cn-i7*****605.public.ha.aliyuncs.com");
        //公网ip调用填写httpProxy,如需要公网IP调用,请打开如下注释
//        config.setHttpProxy("http://116.62.XXX.XXX:80");

        client = new Client(config);

    }

    @Test
    public void query() throws Exception {
        try {
            QueryRequest request = new QueryRequest();
            request.setTableName("gist"); // 必填项, 查询的表名
            request.setVector(Arrays.asList(0.1f, 0.2f, 0.3f, 0.4f)); // 必填项, 查询的向量数据
            request.setVectorCount(2); // 非必填项, vector字段中包含的向量个数
            request.setTopK(100); // 非必填项, 返回个数
            request.setIncludeVector(true); // 非必填项, 是否返回文档中的向量信息
            request.setOutputFields(new ArrayList<String>()); // 非必填项, 需要返回值的字段列表
            request.setFilter("a>10"); // 非必填项, 过滤表达式
            request.setSearchParams("{\\\"qc.searcher.scan_ratio\\\":0.01}");
            SearchResponse searchResponse = client.query(request);
            System.out.println("搜索结果:\n" + searchResponse.getBody());
        } catch (TeaException e) {
            System.out.println(e.getCode());
            System.out.println(e.getMessage());
            Map<String, Object> abc = e.getData();
            System.out.println(com.aliyun.teautil.Common.toJSONString(abc));
        }
    }

}

Python

from alibabacloud_ha3engine_vector.client import Client
from alibabacloud_ha3engine_vector.models import Config
from alibabacloud_ha3engine_vector.models import QueryRequest

config = Config(
        # 私网域名调用请填写endpoint
        endpoint="ha-cn-7mz2ougaw02.ha.aliyuncs.com",
        # 实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
        instance_id="ha-cn-7mz2ougaw02",
        # 用户名,可在实例详情页>网络信息 查看
        access_user_name="user",
        # 密码,可在实例详情页>网络信息 修改
        access_pass_word="111111")
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",
                        search_params="{\\\"qc.searcher.scan_ratio\\\":0.01}",
                        output_fields=["a", "b"],
                        top_k=10)
result = client.query(request)

namespace检索

OpenSearch向量检索版支持通过namespace将索引进行分区,为向量索引配置namespace后,可在指定namespace内查询,因此不同的查询请求可以搜索不同的索引子集。

注意:设置namespace后,查询时必须指定namespace。

package com.aliyun.ha3engine.demo;

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 {

    /**
     * 问天引擎client,暂时支持查询操作
     */
    private Client client;

    @Before
    public void clientInit() throws Exception {

        /*
          初始化问天引擎client
         */
        Config config = new Config();

        //  实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
        config.setInstanceId("ha-cn-i7*****605");

        // 用户名,可在实例详情页>网络信息 查看
        config.setAccessUserName("username");

        // 密码,可在实例详情页>网络信息 修改
        config.setAccessPassWord("password");

        //私网域名调用请填写endpoint,如需要公网域名调用,请打开如下注释
        config.setEndpoint("ha-cn-i7*****605.public.ha.aliyuncs.com");
        //公网ip调用填写httpProxy,如需要公网IP调用,请打开如下注释
//        config.setHttpProxy("http://116.62.XXX.XXX:80");

        client = new Client(config);

    }

    @Test
    public void query() throws Exception {
        try {
            QueryRequest request = new QueryRequest();
            request.setTableName("gist"); // 必填项, 查询的表名
            request.setVector(Arrays.asList(0.183762561f, 0.738372617f)); // 必填项, 查询的向量数据
            request.setNamespace("namespace"); // 非必填项, 查询向量的空间
            request.setTopK(100); // 非必填项, 返回个数
            request.setIncludeVector(true); // 非必填项, 是否返回文档中的向量信息
            request.setSearchParams("{\\\"qc.searcher.scan_ratio\\\":0.01}");
            SearchResponse searchResponse = client.query(request);
            System.out.println("搜索结果:\n" + searchResponse.getBody());
        } catch (TeaException e) {
            System.out.println(e.getCode());
            System.out.println(e.getMessage());
            Map<String, Object> abc = e.getData();
            System.out.println(com.aliyun.teautil.Common.toJSONString(abc));
        }
    }

}
from alibabacloud_ha3engine_vector.client import Client
from alibabacloud_ha3engine_vector.models import Config
from alibabacloud_ha3engine_vector.models import QueryRequest

config = Config(
        # 私网域名调用请填写endpoint
        endpoint="ha-cn-7mz2ougaw02.ha.aliyuncs.com",
        # 实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
        instance_id="ha-cn-7mz2ougaw02",
        # 用户名,可在实例详情页>网络信息 查看
        access_user_name="user",
        # 密码,可在实例详情页>网络信息 修改
        access_pass_word="111111")
client = Client(config)

request = QueryRequest(table_name="t1",
                        namespace="space_b",
                        vector=[0.1, 0.2],
                        include_vector=True,
                        search_params="{\\\"qc.searcher.scan_ratio\\\":0.01}",
                        top_k=10)
result = client.query(request)

多namepsace检索

为向量索引配置namespace后,支持在多个namespace中查询。

package com.aliyun.ha3engine.demo;

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 {

    /**
     * 问天引擎client,暂时支持查询操作
     */
    private Client client;

    @Before
    public void clientInit() throws Exception {

        /*
          初始化问天引擎client
         */
        Config config = new Config();

        //  实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
        config.setInstanceId("ha-cn-i7*****605");

        // 用户名,可在实例详情页>网络信息 查看
        config.setAccessUserName("username");

        // 密码,可在实例详情页>网络信息 修改
        config.setAccessPassWord("password");

        //私网域名调用请填写endpoint,如需要公网域名调用,请打开如下注释
        config.setEndpoint("ha-cn-i7*****605.public.ha.aliyuncs.com");
        //公网ip调用填写httpProxy,如需要公网IP调用,请打开如下注释
//        config.setHttpProxy("http://116.62.XXX.XXX:80");

        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 Query();
            query1.setVector(Arrays.asList(0.1f, 0.2f, 0.3f));
            query1.setNamespace("space_a");

            QueryRequest query2 = new Query();
            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("搜索结果:\n" + searchResponse.getBody());
        } catch (TeaException e) {
            System.out.println(e.getCode());
            System.out.println(e.getMessage());
            Map<String, Object> abc = e.getData();
            System.out.println(com.aliyun.teautil.Common.toJSONString(abc));
        }
    }

}
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(
        # 私网域名调用请填写endpoint
        endpoint="ha-cn-7mz2ougaw02.ha.aliyuncs.com",
        # 实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
        instance_id="ha-cn-7mz2ougaw02",
        # 用户名,可在实例详情页>网络信息 查看
        access_user_name="user",
        # 密码,可在实例详情页>网络信息 修改
        access_pass_word="111111")
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)

包含过滤条件的检索

支持使用filter表达式,指定过滤条件进行查询。

package com.aliyun.ha3engine.demo;

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 {

    /**
     * 问天引擎client,暂时支持查询操作
     */
    private Client client;

    @Before
    public void clientInit() throws Exception {

        /*
          初始化问天引擎client
         */
        Config config = new Config();

        //  实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
        config.setInstanceId("ha-cn-i7*****605");

        // 用户名,可在实例详情页>网络信息 查看
        config.setAccessUserName("username");

        // 密码,可在实例详情页>网络信息 修改
        config.setAccessPassWord("password");

        //私网域名调用请填写endpoint,如需要公网域名调用,请打开如下注释
        config.setEndpoint("ha-cn-i7*****605.public.ha.aliyuncs.com");
        //公网ip调用填写httpProxy,如需要公网IP调用,请打开如下注释
//        config.setHttpProxy("http://116.62.XXX.XXX:80");

        client = new Client(config);

    }

    @Test
    public void query() throws Exception {
        try {
            QueryRequest request = new QueryRequest();
            request.setTableName("gist"); // 必填项, 查询的表名
            request.setVector(Arrays.asList(0.183762561f, 0.738372617f)); // 必填项, 查询的向量数据
            request.setTopK(100); // 非必填项, 返回个数
            request.setIncludeVector(true); // 非必填项, 是否返回文档中的向量信息
            request.setOutputFields(new ArrayList<String>()); // 非必填项, 需要返回值的字段列表
            request.setFilter("a>10"); // 非必填项, 过滤表达式
            request.setSearchParams("{\\\"qc.searcher.scan_ratio\\\":0.01}");
            SearchResponse searchResponse = client.query(request);
            System.out.println("搜索结果:\n" + searchResponse.getBody());
        } catch (TeaException e) {
            System.out.println(e.getCode());
            System.out.println(e.getMessage());
            Map<String, Object> abc = e.getData();
            System.out.println(com.aliyun.teautil.Common.toJSONString(abc));
        }
    }

}
from alibabacloud_ha3engine_vector.client import Client
from alibabacloud_ha3engine_vector.models import Config
from alibabacloud_ha3engine_vector.models import QueryRequest

config = Config(
        # 私网域名调用请填写endpoint
        endpoint="ha-cn-7mz2ougaw02.ha.aliyuncs.com",
        # 实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
        instance_id="ha-cn-7mz2ougaw02",
        # 用户名,可在实例详情页>网络信息 查看
        access_user_name="user",
        # 密码,可在实例详情页>网络信息 修改
        access_pass_word="111111")
client = Client(config)

request = QueryRequest(table_name="t1",
                        vector=[0.1, 0.2],
                        include_vector=True,
                        filter="a > 10",
                        output_fields=["a", "b"],
                        search_params="{\\\"qc.searcher.scan_ratio\\\":0.01}",
                        top_k=10)
result = client.query(request)

filter使用可参考filter表达式

主键检索

Java异步

package com.aliyun.ha3engine;

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

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;

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

    public static void main(String[] args) throws Exception {
        try {
            // 配置实例的用户名密码, 可在实例详情页>网络信息 查看
            AsyncConfigInfoProvider provider = AsyncConfigInfoProvider.create("username", "password");

            // 初始化异步客户端
            AsyncClient client = AsyncClient.builder()
                    .credentialsProvider(provider)
                    .overrideConfiguration(
                            ClientOverrideConfiguration.create()
                                    //公网域名, 实例名可在实例详情页>API入口 查看
                                    .setEndpointOverride("ha-cn-***********.public.ha.aliyuncs.com")
                            .setProtocol("http")
                    ).build();
            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.getMessage());
            Map<String, Object> abc = e.getData();
            System.out.println(com.aliyun.teautil.Common.toJSONString(abc));
        }
    }
}

Java

package com.aliyun.ha3engine.demo;

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 {

    /**
     * 问天引擎client,暂时支持查询操作
     */
    private Client client;

    @Before
    public void clientInit() throws Exception {

        /*
          初始化问天引擎client
         */
        Config config = new Config();

        //  实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
        config.setInstanceId("ha-cn-i7*****605");

        // 用户名,可在实例详情页>网络信息 查看
        config.setAccessUserName("username");

        // 密码,可在实例详情页>网络信息 修改
        config.setAccessPassWord("password");

        //私网域名调用请填写endpoint,如需要公网域名调用,请打开如下注释
        config.setEndpoint("ha-cn-i7*****605.public.ha.aliyuncs.com");
        //公网ip调用填写httpProxy,如需要公网IP调用,请打开如下注释
//        config.setHttpProxy("http://116.62.XXX.XXX:80");

        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("搜索结果:\n" + searchResponse.getBody());
        } catch (TeaException e) {
            System.out.println(e.getCode());
            System.out.println(e.getMessage());
            Map<String, Object> abc = e.getData();
            System.out.println(com.aliyun.teautil.Common.toJSONString(abc));
        }
    }

}

Python

from alibabacloud_ha3engine_vector.client import Client
from alibabacloud_ha3engine_vector.models import Config
from alibabacloud_ha3engine_vector.models import FetchRequest

config = Config(
        # 私网域名调用请填写endpoint
        endpoint="ha-cn-7mz2ougaw02.ha.aliyuncs.com",
        # 实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
        instance_id="ha-cn-7mz2ougaw02",
        # 用户名,可在实例详情页>网络信息 查看
        access_user_name="user",
        # 密码,可在实例详情页>网络信息 修改
        access_pass_word="111111")
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/tea"
	ha3engine "github.com/aliyun/alibabacloud-ha3-go-sdk/client"
)

func main() {

	// 创建请求客户端配置
	config := &ha3engine.Config{
		// 私网域名或者公网域名
		Endpoint: tea.String("<Endpoint>"),
		//  实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
		InstanceId: tea.String("<InstanceId>"),
		// 用户名,可在实例详情页>网络信息 查看
		AccessUserName: tea.String("<AccessUserName>"),
		// 密码,可在实例详情页>网络信息 修改
		AccessPassWord: tea.String("<AccessPassWord>"),
	}

	// 初始化一个client, 用以发送请求.
	client, _clientErr := ha3engine.NewClient(config)

	// 如果 NewClient 过程中出现异常. 则 返回 _clientErr 且输出 错误信息.
	if _clientErr != nil {
		fmt.Println(_clientErr)
		return
	}

	fetch(client)
}


/**
 *	主键查询
 */
func fetch(client *ha3engine.Client) {

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

	// 给指针数组元素赋值
	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)

	// 如果 发送请求 过程中出现异常. 则 返回 _requestErr 且输出 错误信息.
	if _requestErr != nil {
		fmt.Println(_requestErr)
		return
	}

	// 输出正常返回的 response 内容.
	fmt.Println(response)

}

预测查询

预测查询是用户通过向量检索版将文本或图片生成向量后,通过文本或图片进行向量化检索时用到的查询方式,SDK说明如下:

Java异步

package com.aliyun.ha3engine;

import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

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;

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

    public static void main(String[] args) throws Exception {
        try {
            // 配置实例的用户名密码, 可在实例详情页>网络信息 查看
            AsyncConfigInfoProvider provider = AsyncConfigInfoProvider.create("username", "password");

            // 初始化异步客户端
            AsyncClient client = AsyncClient.builder()
                    .credentialsProvider(provider)
                    .overrideConfiguration(
                            ClientOverrideConfiguration.create()
                                    //公网域名, 实例名可在实例详情页>API入口 查看
                                    .setEndpointOverride("ha-cn-***********.public.ha.aliyuncs.com")
                            .setProtocol("http")
                    ).build();
            QueryRequest queryRequest = QueryRequest.builder()
                    .tableName("table_name") // 查询的表名
                    .indexName("index_name") // 查询的索引名
                    .topK(10) // 返回个数
                    .includeVector(true) // 是否返回文档中的向量信息
                    .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.getMessage());
            Map<String, Object> abc = e.getData();
            System.out.println(com.aliyun.teautil.Common.toJSONString(abc));
        }
    }
}

Java

package com.aliyun.ha3engine.demo;

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 {

    /**
     * client,暂时支持查询操作
     */
    private Client client;

    @Before
    public void clientInit() throws Exception {

        /*
          初始化client
         */
        Config config = new Config();

        //  实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
        config.setInstanceId("ha-cn-i7*****605");

        // 用户名,可在实例详情页>网络信息 查看
        config.setAccessUserName("username");

        // 密码,可在实例详情页>网络信息 修改
        config.setAccessPassWord("password");

        //私网域名调用请填写endpoint,如需要公网域名调用,请打开如下注释
        config.setEndpoint("ha-cn-i7*****605.public.ha.aliyuncs.com");
        //公网ip调用填写httpProxy,如需要公网IP调用,请打开如下注释
        //        config.setHttpProxy("http://116.62.XXX.XXX:80");

        client = new Client(config);
    }

    @Test
    public void query() throws Exception {
        try {
            QueryRequest request = new QueryRequest();
            request.setTableName("gist"); // 必填项, 查询的表名
            request.setContent("小孩"); // 必填项,查询的内容
            request.setModal("text"); // 必填项,用于向量化查询词
            request.setTopK(100); // 非必填项, 返回个数
    request.setSearchParams("{\\\"qc.searcher.scan_ratio\\\":0.01}");
            SearchResponse searchResponse = client.inferenceQuery(request);
            System.out.println("搜索结果:\n" + searchResponse.getBody());
        } catch (TeaException e) {
            System.out.println(e.getCode());
            System.out.println(e.getMessage());
            Map<String, Object> abc = e.getData();
            System.out.println(com.aliyun.teautil.Common.toJSONString(abc));
        }
    }
}

Python

from alibabacloud_ha3engine_vector.client import Client
from alibabacloud_ha3engine_vector.models import Config
from alibabacloud_ha3engine_vector.models import QueryRequest

config = Config(
    # 私网域名调用请填写endpoint
    endpoint="ha-cn-7mz2ougaw02.ha.aliyuncs.com",
    # 实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
    instance_id="ha-cn-7mz2ougaw02",
    # 用户名,可在实例详情页>网络信息 查看
    access_user_name="user",
    # 密码,可在实例详情页>网络信息 修改
    access_pass_word="111111")
client = Client(config)

request = QueryRequest(table_name="gist",
                       content="小孩",
                       modal="text",
                       search_params="{\\\"qc.searcher.scan_ratio\\\":0.01}",
                       top_k=10)
result = client.inference_query(request)

Go

package main

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

func main() {

	// 创建请求客户端配置
	config := &ha3engine.Config{
		// 私网域名或者公网域名
		Endpoint: tea.String("<Endpoint>"),
		//  实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
		InstanceId: tea.String("<InstanceId>"),
		// 用户名,可在实例详情页>网络信息 查看
		AccessUserName: tea.String("<AccessUserName>"),
		// 密码,可在实例详情页>网络信息 修改
		AccessPassWord: tea.String("<AccessPassWord>"),
	}

	// 初始化一个client, 用以发送请求.
	client, _clientErr := ha3engine.NewClient(config)

	// 如果 NewClient 过程中出现异常. 则 返回 _clientErr 且输出 错误信息.
	if _clientErr != nil {
		fmt.Println(_clientErr)
		return
	}

	inferenceQuery(client)
}

/**
 *	预测查询
 */
func inferenceQuery(client *ha3engine.Client) {

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

	// 创建一个指向float32的指针数组
	array := make([]*float32, 3)

	// 分配内存地址并初始化值
	value1 := float32(1.23)
	value2 := float32(4.56)
	value3 := float32(7.89)

	// 给指针数组元素赋值
	array[0] = &value1
	array[1] = &value2
	array[2] = &value3

	searchRequestModel.SetVector(array)

	response, _requestErr := client.InferenceQuery(searchRequestModel)

	// 如果 发送请求 过程中出现异常. 则 返回 _requestErr 且输出 错误信息.
	if _requestErr != nil {
		fmt.Println(_requestErr)
		return
	}

	// 输出正常返回的 response 内容.
	fmt.Println(response)

}

参数说明

  • queryRequest中的content表示需要查询的内容,文本向量化场景则输入文本,图片向量化场景可以输入文本或者base64编码后的图片

  • queryRequest中的modal表示查询的方式,其中text表示文本向量化就是以文搜文,图片向量化就是以文搜图

批量查询

Java异步

package com.aliyun.ha3engine;

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

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;

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

    public static void main(String[] args) throws Exception {
        try {
            // 配置实例的用户名密码, 可在实例详情页>网络信息 查看
            AsyncConfigInfoProvider provider = AsyncConfigInfoProvider.create("username", "password");

            // 初始化异步客户端
            AsyncClient client = AsyncClient.builder()
                    .credentialsProvider(provider)
                    .overrideConfiguration(
                            ClientOverrideConfiguration.create()
                                    //公网域名, 实例名可在实例详情页>API入口 查看
                                    .setEndpointOverride("ha-cn-***********.public.ha.aliyuncs.com")
                            .setProtocol("http")
                    ).build();

            QueryRequest queryRequest = QueryRequest.builder()
                    .indexName("content_vec") // 查询的索引名
                    .namespace("150086193") // 查询向量的空间
                    .vector(Arrays.asList(0.0001575F, 0.00682848F)) // 查询的向量数据
                    .build();
            MultiQueryRequest multiQueryRequest = MultiQueryRequest.builder()
                    .tableName("hybrid") // 查询的表名
                    .topK(10) // 返回个数
                    .outputFields(Arrays.asList("content", "source", "instance_id")) // 需要返回值的字段列表
                    .filter("type='TEXT'") // 过滤表达式
                    .order("DESC") // 排序顺序
                    .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.getMessage());
            Map<String, Object> abc = e.getData();
            System.out.println(com.aliyun.teautil.Common.toJSONString(abc));
        }
    }
}

Go

package main

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

func main() {

	// 创建请求客户端配置
	config := &ha3engine.Config{
		// 私网域名或者公网域名
		Endpoint: tea.String("<Endpoint>"),
		//  实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
		InstanceId: tea.String("<InstanceId>"),
		// 用户名,可在实例详情页>网络信息 查看
		AccessUserName: tea.String("<AccessUserName>"),
		// 密码,可在实例详情页>网络信息 修改
		AccessPassWord: tea.String("<AccessPassWord>"),
	}

	// 初始化一个client, 用以发送请求.
	client, _clientErr := ha3engine.NewClient(config)

	// 如果 NewClient 过程中出现异常. 则 返回 _clientErr 且输出 错误信息.
	if _clientErr != nil {
		fmt.Println(_clientErr)
		return
	}

	multiQuery(client)
}

/**
 *	批量查询
 */
func multiQuery(client *ha3engine.Client) {

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

	// 创建一个指向float32的指针数组
	array := make([]*float32, 3)

	// 分配内存地址并初始化值
	value1 := float32(1.23)
	value2 := float32(4.56)
	value3 := float32(7.89)

	// 给指针数组元素赋值
	array[0] = &value1
	array[1] = &value2
	array[2] = &value3

	query.SetVector(array)

	request := &ha3engine.MultiQueryRequest{}

	request.SetTableName("api")

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

	response, _requestErr := client.MultiQuery(request)

	// 如果 发送请求 过程中出现异常. 则 返回 _requestErr 且输出 错误信息.
	if _requestErr != nil {
		fmt.Println(_requestErr)
		return
	}

	// 输出正常返回的 response 内容.
	fmt.Println(response)

}

混合查询(单向量)

混排查询是用户通过稠密向量和稀疏向量混合查询的方式,SDK说明如下:

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 {

    /**
     * 问天引擎client,暂时支持查询操作
     */
    private Client client;

    @Before
    public void clientInit() throws Exception {

        /*
          初始化问天引擎client
         */
        Config config = new Config();

        //  实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
        config.setInstanceId("ha-cn-***********");

        // 用户名,可在实例详情页>网络信息 查看
        config.setAccessUserName("xxx");

        // 密码,可在实例详情页>网络信息 修改
        config.setAccessPassWord("xxx");

        //私网域名调用请填写endpoint,如需要公网域名调用,请打开如下注释
        config.setEndpoint("ha-cn-**********.public.ha.aliyuncs.com");
        //公网ip调用填写httpProxy,如需要公网IP调用,请打开如下注释
//		config.setHttpProxy("http://120.27.XXX.XX:80");
        client = new Client(config);
    }
    
    @Test
    public void query() throws Exception {
        try {
            QueryRequest queryRequest = new QueryRequest();
            queryRequest.setIndexName("vector"); // 查询的索引名 必须在SearchRequest中指定或者在每个Query中指定
            queryRequest.setNamespace("xxx"); // 查询向量的空间
            queryRequest.setVector(Arrays.asList(0.0001575F, 0.00682848F)); // 查询的向量数据,多个向量可以平铺开

            SparseData sparseData = new SparseData();
            sparseData.setCount(Arrays.asList(2)); // 每个稀疏向量中包含的元素个数
            sparseData.setIndices(Arrays.asList(983589459L, 1261855554L)); // 元素下标(需要从小到大排序)
            sparseData.setValues(Arrays.asList(0.0001575F, 0.00682848F)); // 元素值(与下标一一对应)

            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> abc = e.getData();
            System.out.println(com.aliyun.teautil.Common.toJSONString(abc));
        }
    }
}
from alibabacloud_ha3engine_vector.client import Client
from alibabacloud_ha3engine_vector.models import Config
from alibabacloud_ha3engine_vector.models import SparseData
from alibabacloud_ha3engine_vector.models import QueryRequest

config = Config(
    # 私网域名调用请填写endpoint
    endpoint="ha-***",
    # 实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
    instance_id="ha-***",
    # 用户名,可在实例详情页>网络信息 查看
    access_user_name="xxx",
    # 密码,可在实例详情页>网络信息 修改
    access_pass_word="xxx")
client = Client(config)
sparseData = SparseData(indices=[101, 203], values=[0.1, 0.5])
request = QueryRequest(table_name="t1",
                       index_name="vec_index",
                       vector=[0.1, 0.2, 0.3, 0.4],
                       sparse_data=sparseData
                       include_vector=True,
                       output_fields=["a", "b"],
                       top_k=10)
result = client.query(request)

混合查询(多向量)

混排查询是用户通过稠密向量和稀疏向量混合查询的方式,SDK说明如下:

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 {

    /**
     * 问天引擎client,暂时支持查询操作
     */
    private Client client;

    @Before
    public void clientInit() throws Exception {

        /*
          初始化问天引擎client
         */
        Config config = new Config();

        //  实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
        config.setInstanceId("ha-cn-***********");

        // 用户名,可在实例详情页>网络信息 查看
        config.setAccessUserName("xxx");

        // 密码,可在实例详情页>网络信息 修改
        config.setAccessPassWord("xxx");

        //私网域名调用请填写endpoint,如需要公网域名调用,请打开如下注释
        config.setEndpoint("ha-cn-**********.public.ha.aliyuncs.com");
        //公网ip调用填写httpProxy,如需要公网IP调用,请打开如下注释
//      config.setHttpProxy("http://120.27.XXX.XX:80");

        client = new Client(config);
    }

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

            MultiQueryRequest multiQueryRequest = new MultiQueryRequest();

            multiQueryRequest.setTableName("main"); //查询的表名
            multiQueryRequest.setTopK(50); // 最终返回的结果个数
            multiQueryRequest.setOutputFields(Arrays.asList("content", "source", "instance_id")); // 需要返回值的字段列表
            multiQueryRequest.setFilter("type='TEXT'"); // 过滤表达式
            multiQueryRequest.setOrder("DESC");

            QueryRequest queryRequest = new QueryRequest();
            queryRequest.setIndexName("vector"); // 查询的索引名 必须在SearchRequest中指定或者在每个Query中指定
            queryRequest.setNamespace("xxx"); // 查询向量的空间
            queryRequest.setVector(Arrays.asList(0.0001575F, 0.00682848F)); // 查询的向量数据,多个向量可以平铺开

            SparseData sparseData = new SparseData();
            sparseData.setCount(Arrays.asList(2)); // 每个稀疏向量中包含的元素个数
            sparseData.setIndices(Arrays.asList(983589459L, 1261855554L)); // 元素下标(需要从小到大排序)
            sparseData.setValues(Arrays.asList(0.0001575F, 0.00682848F)); // 元素值(与下标一一对应)

            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> abc = e.getData();
            System.out.println(com.aliyun.teautil.Common.toJSONString(abc));
        }
    }
}
from alibabacloud_ha3engine_vector.client import Client
from alibabacloud_ha3engine_vector.models import Config
from alibabacloud_ha3engine_vector.models import SparseData
from alibabacloud_ha3engine_vector.models import QueryRequest
from alibabacloud_ha3engine_vector.models import MultiQueryRequest

config = Config(
        # 私网域名调用请填写endpoint
        endpoint="ha-***",
        # 实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
        instance_id="ha-***",
        # 用户名,可在实例详情页>网络信息 查看
        access_user_name="xxx",
        # 密码,可在实例详情页>网络信息 修改
        access_pass_word="xxx")
client = Client(config)
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], namespace="space_a", sparse_data=sd1)
q2 = QueryRequest(vector=[0.4, 0.5, 0.6], namespace="space_b", sparse_data=sd2)
request = MultiQueryRequest(table_name = "gist", 
                                 queries=[q1, q2], 
                                 top_k=3, 
                                 include_vector=True)
result = client.multi_query(request)
重要

多向量查询需要多个向量类型相同,如都是稠密向量检索,或者都是混排检索

说明

  • 请求的相应结果可参考流量API说明>查询数据对应的查询方式中的返回参数

  • 不要使用go get github.com/aliyun/alibabacloud-ha3-go-sdk命令拉取git依赖,必须后面指定版本,因为向量检索版和召回引擎版的tag都在同一个github下,拉取依赖的时候需根据实例的版本拉取对应依赖