全部產品
Search
文件中心

OpenSearch:查詢資料

更新時間:Jul 13, 2024

本文檔介紹如何使用Java非同步、Java、Python語言進行向量檢索、預測檢索、以及包含過濾條件的檢索。

相關依賴

Java非同步

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

Java

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

Python

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

Go

go get github.com/aliyun/alibabacloud-ha3-go-sdk@v1.1.3-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下,拉取依賴的時候需根據執行個體的版本拉取對應依賴